Skip to content

Commit a78054f

Browse files
committed
Cleaned up code
1 parent 8f43f08 commit a78054f

File tree

7 files changed

+46
-12
lines changed

7 files changed

+46
-12
lines changed

.travis.yml

+2-2
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ script:
1414
- mkdir -p build/logs
1515
- php vendor/bin/phpunit --coverage-clover build/logs/clover.xml
1616

17-
after_script:
18-
- travis_retry php vendor/bin/coveralls
17+
after_success:
18+
- travis_retry php vendor/bin/php-coveralls -v
1919
- wget https://scrutinizer-ci.com/ocular.phar
2020
- php ocular.phar code-coverage:upload --format=php-clover build/logs/clover.xml
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace PHPHtmlParser\Contracts;
6+
7+
use PHPHtmlParser\Dom;
8+
use PHPHtmlParser\Options;
9+
use Psr\Http\Client\ClientInterface;
10+
use Psr\Http\Message\RequestInterface;
11+
12+
interface DomInterface
13+
{
14+
public function loadFromFile(string $file, ?Options $options = null): Dom;
15+
16+
public function loadFromUrl(string $url, ?Options $options, ?ClientInterface $client = null, ?RequestInterface $request = null): Dom;
17+
18+
public function loadStr(string $str, ?Options $options = null): Dom;
19+
20+
public function setOptions(Options $options): Dom;
21+
22+
public function find(string $selector, int $nth = null);
23+
}

src/PHPHtmlParser/Dom.php

+3-2
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
use GuzzleHttp\Psr7\Request;
88
use Http\Adapter\Guzzle6\Client;
9+
use PHPHtmlParser\Contracts\DomInterface;
910
use PHPHtmlParser\Dom\AbstractNode;
1011
use PHPHtmlParser\Dom\Collection;
1112
use PHPHtmlParser\Dom\HtmlNode;
@@ -27,7 +28,7 @@
2728
/**
2829
* Class Dom.
2930
*/
30-
class Dom
31+
class Dom implements DomInterface
3132
{
3233
/**
3334
* Contains the root node of this dom tree.
@@ -450,7 +451,7 @@ private function parse(): void
450451
$activeNode = $this->root;
451452
while ($activeNode !== null) {
452453
if ($activeNode && $activeNode->tag->name() === 'script'
453-
&& $this->options->isCleanupInput() != true
454+
&& $this->options->isCleanupInput() !== true
454455
) {
455456
$str = $this->content->copyUntil('</');
456457
} else {

src/PHPHtmlParser/Dom/InnerNode.php

+11
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ public function countChildren(): int
9898
*
9999
* @throws ChildNotFoundException
100100
* @throws CircularException
101+
* @throws LogicalException
101102
*/
102103
public function addChild(AbstractNode $child, int $before = -1): bool
103104
{
@@ -158,6 +159,10 @@ public function addChild(AbstractNode $child, int $before = -1): bool
158159

159160
// add the child
160161
$combination = \array_combine($keys, $children);
162+
if ($combination === false) {
163+
// The number of elements for each array isn't equal or if the arrays are empty.
164+
throw new LogicalException('array combine failed during add child method call.');
165+
}
161166
$this->children = $combination;
162167

163168
// tell child I am the new parent
@@ -300,6 +305,8 @@ public function isChild(int $id): bool
300305
/**
301306
* Removes the child with id $childId and replace it with the new child
302307
* $newChild.
308+
*
309+
* @throws LogicalException
303310
*/
304311
public function replaceChild(int $childId, AbstractNode $newChild): void
305312
{
@@ -312,6 +319,10 @@ public function replaceChild(int $childId, AbstractNode $newChild): void
312319
$index = \array_search($childId, $keys, true);
313320
$keys[$index] = $newChild->id();
314321
$combination = \array_combine($keys, $this->children);
322+
if ($combination === false) {
323+
// The number of elements for each array isn't equal or if the arrays are empty.
324+
throw new LogicalException('array combine failed during replace child method call.');
325+
}
315326
$this->children = $combination;
316327
$this->children[$newChild->id()] = [
317328
'prev' => $oldChild['prev'],

src/PHPHtmlParser/Selector/Seeker.php

+6-5
Original file line numberDiff line numberDiff line change
@@ -63,12 +63,12 @@ public function seek(array $nodes, RuleDTO $rule, array $options): array
6363
}
6464

6565
$pass = $this->checkTag($rule, $child);
66-
if ($pass && $rule->getKey() != null) {
66+
if ($pass && $rule->getKey() !== null) {
6767
$pass = $this->checkKey($rule, $child);
6868
}
6969
if ($pass &&
70-
$rule->getKey() != null &&
71-
$rule->getValue() != null &&
70+
$rule->getKey() !== null &&
71+
$rule->getValue() !== null &&
7272
$rule->getValue() != '*'
7373
) {
7474
$pass = $this->checkComparison($rule, $child);
@@ -238,8 +238,9 @@ private function checkNodeValue(
238238
): bool {
239239
$check = false;
240240
if (
241-
$rule->getValue() != null &&
242-
\is_string($rule->getValue())
241+
$rule->getValue() !== null &&
242+
\is_string($rule->getValue()) &&
243+
$nodeValue !== null
243244
) {
244245
$check = $this->match($rule->getOperator(), $rule->getValue(), $nodeValue);
245246
}

src/PHPHtmlParser/Selector/Selector.php

-2
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,6 @@ public function __construct(string $selector, ?ParserInterface $parser = null, ?
4848

4949
/**
5050
* Returns the selectors that where found in __construct.
51-
*
52-
* @return array
5351
*/
5452
public function getParsedSelectorCollectionDTO(): ParsedSelectorCollectionDTO
5553
{

tests/Selector/SeekerTest.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public function testSeekReturnEmptyArray()
1919
'alterNext' => false,
2020
]);
2121
$seeker = new Seeker();
22-
$results = $seeker->seek([], $ruleDTO, [], false);
22+
$results = $seeker->seek([], $ruleDTO, []);
2323
$this->assertCount(0, $results);
2424
}
2525
}

0 commit comments

Comments
 (0)