Skip to content

Commit

Permalink
Merge pull request #105 from PhpGt/102-nodelist
Browse files Browse the repository at this point in the history
Return null when out of bounds
  • Loading branch information
g105b authored Apr 17, 2018
2 parents 4f0c0a6 + e625c6b commit 636f6a4
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 14 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
/vendor
/.idea
/test/_coverage
/test/unit/_coverage
14 changes: 1 addition & 13 deletions src/NodeList.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,22 +85,11 @@ private function incrementKeyToNextElement() {
}

// ArrayAccess -----------------------------------------------------------------

/**
* Offset exists?
* @param integer $offset offset number
* @return boolean
*/
public function offsetExists($offset):bool {
return isset($offset, $this->domNodeList);
}

/**
* Returns the element in the offset position
* @param integer $offset offset number
* @return Element
*/
public function offsetGet($offset):Element {
public function offsetGet($offset):?Element {
return $this->item($offset);
}

Expand All @@ -113,7 +102,6 @@ public function offsetUnset($offset) {
}

// Countable -------------------------------------------------------------------

public function count():int {
return $this->length;
}
Expand Down
16 changes: 16 additions & 0 deletions test/unit/NodeListTest.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php
namespace Gt\Dom\Test;

use Gt\Dom\Element;
use Gt\Dom\HTMLDocument;
use Gt\Dom\NodeList;
use Gt\Dom\Test\Helper\Helper;
Expand All @@ -20,4 +21,19 @@ public function testNodeListFunctionsReturnGtObjects() {
"$key instance of " . gettype($object));
}
}

public function testNodeListIndexOutOfBounds() {
$document = new HTMLDocument(Helper::HTML_MORE);
$paragraphList = $document->getElementsByTagName("p");
$count = count($paragraphList);

for($i = 0; $i < $count; $i++) {
self::assertInstanceOf(
Element::class,
$paragraphList[$i]
);
}

self::assertNull($paragraphList[$count]);
}
}

0 comments on commit 636f6a4

Please sign in to comment.