From a78054fec726427e723bd31fcf10f21403e5e4d7 Mon Sep 17 00:00:00 2001 From: Gilles Paquette Date: Wed, 15 Jul 2020 03:56:54 +0000 Subject: [PATCH] Cleaned up code --- .travis.yml | 4 ++-- src/PHPHtmlParser/Contracts/DomInterface.php | 23 ++++++++++++++++++++ src/PHPHtmlParser/Dom.php | 5 +++-- src/PHPHtmlParser/Dom/InnerNode.php | 11 ++++++++++ src/PHPHtmlParser/Selector/Seeker.php | 11 +++++----- src/PHPHtmlParser/Selector/Selector.php | 2 -- tests/Selector/SeekerTest.php | 2 +- 7 files changed, 46 insertions(+), 12 deletions(-) create mode 100644 src/PHPHtmlParser/Contracts/DomInterface.php diff --git a/.travis.yml b/.travis.yml index 9ffb252..a7abcac 100755 --- a/.travis.yml +++ b/.travis.yml @@ -14,7 +14,7 @@ script: - mkdir -p build/logs - php vendor/bin/phpunit --coverage-clover build/logs/clover.xml -after_script: - - travis_retry php vendor/bin/coveralls +after_success: + - travis_retry php vendor/bin/php-coveralls -v - wget https://scrutinizer-ci.com/ocular.phar - php ocular.phar code-coverage:upload --format=php-clover build/logs/clover.xml diff --git a/src/PHPHtmlParser/Contracts/DomInterface.php b/src/PHPHtmlParser/Contracts/DomInterface.php new file mode 100644 index 0000000..b803f8f --- /dev/null +++ b/src/PHPHtmlParser/Contracts/DomInterface.php @@ -0,0 +1,23 @@ +root; while ($activeNode !== null) { if ($activeNode && $activeNode->tag->name() === 'script' - && $this->options->isCleanupInput() != true + && $this->options->isCleanupInput() !== true ) { $str = $this->content->copyUntil('children = $combination; // tell child I am the new parent @@ -300,6 +305,8 @@ public function isChild(int $id): bool /** * Removes the child with id $childId and replace it with the new child * $newChild. + * + * @throws LogicalException */ public function replaceChild(int $childId, AbstractNode $newChild): void { @@ -312,6 +319,10 @@ public function replaceChild(int $childId, AbstractNode $newChild): void $index = \array_search($childId, $keys, true); $keys[$index] = $newChild->id(); $combination = \array_combine($keys, $this->children); + if ($combination === false) { + // The number of elements for each array isn't equal or if the arrays are empty. + throw new LogicalException('array combine failed during replace child method call.'); + } $this->children = $combination; $this->children[$newChild->id()] = [ 'prev' => $oldChild['prev'], diff --git a/src/PHPHtmlParser/Selector/Seeker.php b/src/PHPHtmlParser/Selector/Seeker.php index ca92cb2..523aa42 100644 --- a/src/PHPHtmlParser/Selector/Seeker.php +++ b/src/PHPHtmlParser/Selector/Seeker.php @@ -63,12 +63,12 @@ public function seek(array $nodes, RuleDTO $rule, array $options): array } $pass = $this->checkTag($rule, $child); - if ($pass && $rule->getKey() != null) { + if ($pass && $rule->getKey() !== null) { $pass = $this->checkKey($rule, $child); } if ($pass && - $rule->getKey() != null && - $rule->getValue() != null && + $rule->getKey() !== null && + $rule->getValue() !== null && $rule->getValue() != '*' ) { $pass = $this->checkComparison($rule, $child); @@ -238,8 +238,9 @@ private function checkNodeValue( ): bool { $check = false; if ( - $rule->getValue() != null && - \is_string($rule->getValue()) + $rule->getValue() !== null && + \is_string($rule->getValue()) && + $nodeValue !== null ) { $check = $this->match($rule->getOperator(), $rule->getValue(), $nodeValue); } diff --git a/src/PHPHtmlParser/Selector/Selector.php b/src/PHPHtmlParser/Selector/Selector.php index 4c45da0..7179ee1 100755 --- a/src/PHPHtmlParser/Selector/Selector.php +++ b/src/PHPHtmlParser/Selector/Selector.php @@ -48,8 +48,6 @@ public function __construct(string $selector, ?ParserInterface $parser = null, ? /** * Returns the selectors that where found in __construct. - * - * @return array */ public function getParsedSelectorCollectionDTO(): ParsedSelectorCollectionDTO { diff --git a/tests/Selector/SeekerTest.php b/tests/Selector/SeekerTest.php index 4e2d9e4..a5106e9 100644 --- a/tests/Selector/SeekerTest.php +++ b/tests/Selector/SeekerTest.php @@ -19,7 +19,7 @@ public function testSeekReturnEmptyArray() 'alterNext' => false, ]); $seeker = new Seeker(); - $results = $seeker->seek([], $ruleDTO, [], false); + $results = $seeker->seek([], $ruleDTO, []); $this->assertCount(0, $results); } }