Skip to content

Commit 80f5474

Browse files
committed
Fixed some code quality issues found by scrutinizer
1 parent a4eae37 commit 80f5474

File tree

5 files changed

+44
-44
lines changed

5 files changed

+44
-44
lines changed

src/PHPHtmlParser/Dom.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -445,7 +445,7 @@ protected function parse()
445445
if ( ! $node->getTag()->isSelfClosing()) {
446446
$activeNode = $node;
447447
}
448-
} else if ($this->options->whitespaceTextNode or
448+
} else if ($this->options->whitespaceTextNode ||
449449
trim($str) != ''
450450
) {
451451
// we found text we care about
@@ -501,7 +501,7 @@ protected function parseTag()
501501
$node = new HtmlNode($tag);
502502

503503
// attributes
504-
while ($this->content->char() != '>' and
504+
while ($this->content->char() != '>' &&
505505
$this->content->char() != '/') {
506506
$space = $this->content->skipByToken('blank', true);
507507
if (empty($space)) {

src/PHPHtmlParser/Dom/AbstractNode.php

Lines changed: 28 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -402,12 +402,8 @@ public function isDescendant($id)
402402
*/
403403
public function isAncestor($id)
404404
{
405-
if ( ! is_null($this->parent)) {
406-
if ($this->parent->id() == $id) {
407-
return true;
408-
}
409-
410-
return $this->parent->isAncestor($id);
405+
if ( ! is_null($this->getAncestor($id))) {
406+
return true;
411407
}
412408

413409
return false;
@@ -639,30 +635,8 @@ public function get_display_size()
639635
$attributes[$match[1]] = $match[2];
640636
}
641637

642-
// If there is a width in the style attributes:
643-
if (isset($attributes['width']) and $width == -1) {
644-
// check that the last two characters are px (pixels)
645-
if (strtolower(substr($attributes['width'], -2)) == 'px') {
646-
$proposed_width = substr($attributes['width'], 0, -2);
647-
// Now make sure that it's an integer and not something stupid.
648-
if (filter_var($proposed_width, FILTER_VALIDATE_INT)) {
649-
$width = $proposed_width;
650-
}
651-
}
652-
}
653-
654-
// If there is a width in the style attributes:
655-
if (isset($attributes['height']) and $height == -1) {
656-
// check that the last two characters are px (pixels)
657-
if (strtolower(substr($attributes['height'], -2)) == 'px') {
658-
$proposed_height = substr($attributes['height'], 0, -2);
659-
// Now make sure that it's an integer and not something stupid.
660-
if (filter_var($proposed_height, FILTER_VALIDATE_INT)) {
661-
$height = $proposed_height;
662-
}
663-
}
664-
}
665-
638+
$width = $this->getLength($attributes, $width, 'width');
639+
$height = $this->getLength($attributes, $width, 'height');
666640
}
667641

668642
$result = [
@@ -673,6 +647,30 @@ public function get_display_size()
673647
return $result;
674648
}
675649

650+
/**
651+
* If there is a length in the style attributes use it.
652+
*
653+
* @param array $attributes
654+
* @param int $length
655+
* @param string $key
656+
* @return int
657+
*/
658+
protected function getLength(array $attributes, $length, $key)
659+
{
660+
if (isset($attributes[$key]) && $length == -1) {
661+
// check that the last two characters are px (pixels)
662+
if (strtolower(substr($attributes[$key], -2)) == 'px') {
663+
$proposed_length = substr($attributes[$key], 0, -2);
664+
// Now make sure that it's an integer and not something stupid.
665+
if (filter_var($proposed_length, FILTER_VALIDATE_INT)) {
666+
$length = $proposed_length;
667+
}
668+
}
669+
}
670+
671+
return $length;
672+
}
673+
676674
/**
677675
* Gets the inner html of this node.
678676
*

src/PHPHtmlParser/Dom/HtmlNode.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ public function text($lookInChildren = false)
160160
$node = $child['node'];
161161
if ($node instanceof TextNode) {
162162
$text .= $child['node']->text;
163-
} elseif ($lookInChildren and
163+
} elseif ($lookInChildren &&
164164
$node instanceof HtmlNode
165165
) {
166166
$text .= $node->text($lookInChildren);

src/PHPHtmlParser/Dom/Tag.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ public function getAttribute($key)
195195
return null;
196196
}
197197
$value = $this->attr[$key]['value'];
198-
if (is_string($value) AND ! is_null($this->encode)) {
198+
if (is_string($value) && ! is_null($this->encode)) {
199199
// convert charset
200200
$this->attr[$key]['value'] = $this->encode->convert($value);
201201
}

src/PHPHtmlParser/Selector.php

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ protected function parseSelectorString($selector)
128128
}
129129

130130
// check for elements that do not have a specified attribute
131-
if (isset($key[0]) AND $key[0] == '!') {
131+
if (isset($key[0]) && $key[0] == '!') {
132132
$key = substr($key, 1);
133133
$noKey = true;
134134
}
@@ -166,13 +166,15 @@ protected function parseSelectorString($selector)
166166
protected function seek(array $nodes, array $rule, array $options)
167167
{
168168
// XPath index
169-
if ( ! empty($rule['tag']) AND ! empty($rule['key']) AND
169+
if (count($rule['tag']) > 0 &&
170+
count($rule['key']) > 0 &&
170171
is_numeric($rule['key'])
171172
) {
172173
$count = 0;
173174
/** @var AbstractNode $node */
174175
foreach ($nodes as $node) {
175-
if ($rule['tag'] == '*' OR $rule['tag'] == $node->getTag()->name()) {
176+
if ($rule['tag'] == '*' ||
177+
$rule['tag'] == $node->getTag()->name()) {
176178
++$count;
177179
if ($count == $rule['key']) {
178180
// found the node we wanted
@@ -198,7 +200,7 @@ protected function seek(array $nodes, array $rule, array $options)
198200
$child = $node->firstChild();
199201
while ( ! is_null($child)) {
200202
// wild card, grab all
201-
if ($rule['tag'] == '*' AND is_null($rule['key'])) {
203+
if ($rule['tag'] == '*' && is_null($rule['key'])) {
202204
$return[] = $child;
203205
try {
204206
$child = $node->nextChild($child->id());
@@ -211,21 +213,21 @@ protected function seek(array $nodes, array $rule, array $options)
211213

212214
$pass = true;
213215
// check tag
214-
if ( ! empty($rule['tag']) AND $rule['tag'] != $child->getTag()->name() AND
216+
if ( ! empty($rule['tag']) && $rule['tag'] != $child->getTag()->name() &&
215217
$rule['tag'] != '*'
216218
) {
217219
// child failed tag check
218220
$pass = false;
219221
}
220222

221223
// check key
222-
if ($pass AND ! is_null($rule['key'])) {
224+
if ($pass && ! is_null($rule['key'])) {
223225
if ($rule['noKey']) {
224226
if ( ! is_null($child->getAttribute($rule['key']))) {
225227
$pass = false;
226228
}
227229
} else {
228-
if ($rule['key'] != 'plaintext' and
230+
if ($rule['key'] != 'plaintext' &&
229231
is_null($child->getAttribute($rule['key']))
230232
) {
231233
$pass = false;
@@ -234,8 +236,8 @@ protected function seek(array $nodes, array $rule, array $options)
234236
}
235237

236238
// compare values
237-
if ($pass and ! is_null($rule['key']) and
238-
! is_null($rule['value']) and $rule['value'] != '*'
239+
if ($pass && ! is_null($rule['key']) &&
240+
! is_null($rule['value']) && $rule['value'] != '*'
239241
) {
240242
if ($rule['key'] == 'plaintext') {
241243
// plaintext search
@@ -248,7 +250,7 @@ protected function seek(array $nodes, array $rule, array $options)
248250
$check = $this->match($rule['operator'], $rule['value'], $nodeValue);
249251

250252
// handle multiple classes
251-
if ( ! $check and $rule['key'] == 'class') {
253+
if ( ! $check && $rule['key'] == 'class') {
252254
$childClasses = explode(' ', $child->getAttribute('class'));
253255
foreach ($childClasses as $class) {
254256
if ( ! empty($class)) {

0 commit comments

Comments
 (0)