Skip to content

Commit

Permalink
Cleaned up code
Browse files Browse the repository at this point in the history
  • Loading branch information
paquettg committed Jul 15, 2020
1 parent 8f43f08 commit a78054f
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 12 deletions.
4 changes: 2 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
23 changes: 23 additions & 0 deletions src/PHPHtmlParser/Contracts/DomInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

declare(strict_types=1);

namespace PHPHtmlParser\Contracts;

use PHPHtmlParser\Dom;
use PHPHtmlParser\Options;
use Psr\Http\Client\ClientInterface;
use Psr\Http\Message\RequestInterface;

interface DomInterface
{
public function loadFromFile(string $file, ?Options $options = null): Dom;

public function loadFromUrl(string $url, ?Options $options, ?ClientInterface $client = null, ?RequestInterface $request = null): Dom;

public function loadStr(string $str, ?Options $options = null): Dom;

public function setOptions(Options $options): Dom;

public function find(string $selector, int $nth = null);
}
5 changes: 3 additions & 2 deletions src/PHPHtmlParser/Dom.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use GuzzleHttp\Psr7\Request;
use Http\Adapter\Guzzle6\Client;
use PHPHtmlParser\Contracts\DomInterface;
use PHPHtmlParser\Dom\AbstractNode;
use PHPHtmlParser\Dom\Collection;
use PHPHtmlParser\Dom\HtmlNode;
Expand All @@ -27,7 +28,7 @@
/**
* Class Dom.
*/
class Dom
class Dom implements DomInterface
{
/**
* Contains the root node of this dom tree.
Expand Down Expand Up @@ -450,7 +451,7 @@ private function parse(): void
$activeNode = $this->root;
while ($activeNode !== null) {
if ($activeNode && $activeNode->tag->name() === 'script'
&& $this->options->isCleanupInput() != true
&& $this->options->isCleanupInput() !== true
) {
$str = $this->content->copyUntil('</');
} else {
Expand Down
11 changes: 11 additions & 0 deletions src/PHPHtmlParser/Dom/InnerNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ public function countChildren(): int
*
* @throws ChildNotFoundException
* @throws CircularException
* @throws LogicalException
*/
public function addChild(AbstractNode $child, int $before = -1): bool
{
Expand Down Expand Up @@ -158,6 +159,10 @@ public function addChild(AbstractNode $child, int $before = -1): bool

// add the child
$combination = \array_combine($keys, $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 add child method call.');
}
$this->children = $combination;

// tell child I am the new parent
Expand Down Expand Up @@ -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
{
Expand All @@ -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'],
Expand Down
11 changes: 6 additions & 5 deletions src/PHPHtmlParser/Selector/Seeker.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
}
Expand Down
2 changes: 0 additions & 2 deletions src/PHPHtmlParser/Selector/Selector.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down
2 changes: 1 addition & 1 deletion tests/Selector/SeekerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

0 comments on commit a78054f

Please sign in to comment.