From 80f547470835aa776def6e5629ce1cf1d9dfbb05 Mon Sep 17 00:00:00 2001 From: Gilles Paquette Date: Sun, 20 Mar 2016 14:41:27 -0400 Subject: [PATCH] Fixed some code quality issues found by scrutinizer --- src/PHPHtmlParser/Dom.php | 4 +- src/PHPHtmlParser/Dom/AbstractNode.php | 58 +++++++++++++------------- src/PHPHtmlParser/Dom/HtmlNode.php | 2 +- src/PHPHtmlParser/Dom/Tag.php | 2 +- src/PHPHtmlParser/Selector.php | 22 +++++----- 5 files changed, 44 insertions(+), 44 deletions(-) diff --git a/src/PHPHtmlParser/Dom.php b/src/PHPHtmlParser/Dom.php index 3a58b32..35e4013 100644 --- a/src/PHPHtmlParser/Dom.php +++ b/src/PHPHtmlParser/Dom.php @@ -445,7 +445,7 @@ protected function parse() if ( ! $node->getTag()->isSelfClosing()) { $activeNode = $node; } - } else if ($this->options->whitespaceTextNode or + } else if ($this->options->whitespaceTextNode || trim($str) != '' ) { // we found text we care about @@ -501,7 +501,7 @@ protected function parseTag() $node = new HtmlNode($tag); // attributes - while ($this->content->char() != '>' and + while ($this->content->char() != '>' && $this->content->char() != '/') { $space = $this->content->skipByToken('blank', true); if (empty($space)) { diff --git a/src/PHPHtmlParser/Dom/AbstractNode.php b/src/PHPHtmlParser/Dom/AbstractNode.php index 9ae7365..bd31bbd 100644 --- a/src/PHPHtmlParser/Dom/AbstractNode.php +++ b/src/PHPHtmlParser/Dom/AbstractNode.php @@ -402,12 +402,8 @@ public function isDescendant($id) */ public function isAncestor($id) { - if ( ! is_null($this->parent)) { - if ($this->parent->id() == $id) { - return true; - } - - return $this->parent->isAncestor($id); + if ( ! is_null($this->getAncestor($id))) { + return true; } return false; @@ -639,30 +635,8 @@ public function get_display_size() $attributes[$match[1]] = $match[2]; } - // If there is a width in the style attributes: - if (isset($attributes['width']) and $width == -1) { - // check that the last two characters are px (pixels) - if (strtolower(substr($attributes['width'], -2)) == 'px') { - $proposed_width = substr($attributes['width'], 0, -2); - // Now make sure that it's an integer and not something stupid. - if (filter_var($proposed_width, FILTER_VALIDATE_INT)) { - $width = $proposed_width; - } - } - } - - // If there is a width in the style attributes: - if (isset($attributes['height']) and $height == -1) { - // check that the last two characters are px (pixels) - if (strtolower(substr($attributes['height'], -2)) == 'px') { - $proposed_height = substr($attributes['height'], 0, -2); - // Now make sure that it's an integer and not something stupid. - if (filter_var($proposed_height, FILTER_VALIDATE_INT)) { - $height = $proposed_height; - } - } - } - + $width = $this->getLength($attributes, $width, 'width'); + $height = $this->getLength($attributes, $width, 'height'); } $result = [ @@ -673,6 +647,30 @@ public function get_display_size() return $result; } + /** + * If there is a length in the style attributes use it. + * + * @param array $attributes + * @param int $length + * @param string $key + * @return int + */ + protected function getLength(array $attributes, $length, $key) + { + if (isset($attributes[$key]) && $length == -1) { + // check that the last two characters are px (pixels) + if (strtolower(substr($attributes[$key], -2)) == 'px') { + $proposed_length = substr($attributes[$key], 0, -2); + // Now make sure that it's an integer and not something stupid. + if (filter_var($proposed_length, FILTER_VALIDATE_INT)) { + $length = $proposed_length; + } + } + } + + return $length; + } + /** * Gets the inner html of this node. * diff --git a/src/PHPHtmlParser/Dom/HtmlNode.php b/src/PHPHtmlParser/Dom/HtmlNode.php index 4e126c6..5a53a84 100644 --- a/src/PHPHtmlParser/Dom/HtmlNode.php +++ b/src/PHPHtmlParser/Dom/HtmlNode.php @@ -160,7 +160,7 @@ public function text($lookInChildren = false) $node = $child['node']; if ($node instanceof TextNode) { $text .= $child['node']->text; - } elseif ($lookInChildren and + } elseif ($lookInChildren && $node instanceof HtmlNode ) { $text .= $node->text($lookInChildren); diff --git a/src/PHPHtmlParser/Dom/Tag.php b/src/PHPHtmlParser/Dom/Tag.php index 02c19cb..cca31d4 100644 --- a/src/PHPHtmlParser/Dom/Tag.php +++ b/src/PHPHtmlParser/Dom/Tag.php @@ -195,7 +195,7 @@ public function getAttribute($key) return null; } $value = $this->attr[$key]['value']; - if (is_string($value) AND ! is_null($this->encode)) { + if (is_string($value) && ! is_null($this->encode)) { // convert charset $this->attr[$key]['value'] = $this->encode->convert($value); } diff --git a/src/PHPHtmlParser/Selector.php b/src/PHPHtmlParser/Selector.php index c577445..47bc90c 100644 --- a/src/PHPHtmlParser/Selector.php +++ b/src/PHPHtmlParser/Selector.php @@ -128,7 +128,7 @@ protected function parseSelectorString($selector) } // check for elements that do not have a specified attribute - if (isset($key[0]) AND $key[0] == '!') { + if (isset($key[0]) && $key[0] == '!') { $key = substr($key, 1); $noKey = true; } @@ -166,13 +166,15 @@ protected function parseSelectorString($selector) protected function seek(array $nodes, array $rule, array $options) { // XPath index - if ( ! empty($rule['tag']) AND ! empty($rule['key']) AND + if (count($rule['tag']) > 0 && + count($rule['key']) > 0 && is_numeric($rule['key']) ) { $count = 0; /** @var AbstractNode $node */ foreach ($nodes as $node) { - if ($rule['tag'] == '*' OR $rule['tag'] == $node->getTag()->name()) { + if ($rule['tag'] == '*' || + $rule['tag'] == $node->getTag()->name()) { ++$count; if ($count == $rule['key']) { // found the node we wanted @@ -198,7 +200,7 @@ protected function seek(array $nodes, array $rule, array $options) $child = $node->firstChild(); while ( ! is_null($child)) { // wild card, grab all - if ($rule['tag'] == '*' AND is_null($rule['key'])) { + if ($rule['tag'] == '*' && is_null($rule['key'])) { $return[] = $child; try { $child = $node->nextChild($child->id()); @@ -211,7 +213,7 @@ protected function seek(array $nodes, array $rule, array $options) $pass = true; // check tag - if ( ! empty($rule['tag']) AND $rule['tag'] != $child->getTag()->name() AND + if ( ! empty($rule['tag']) && $rule['tag'] != $child->getTag()->name() && $rule['tag'] != '*' ) { // child failed tag check @@ -219,13 +221,13 @@ protected function seek(array $nodes, array $rule, array $options) } // check key - if ($pass AND ! is_null($rule['key'])) { + if ($pass && ! is_null($rule['key'])) { if ($rule['noKey']) { if ( ! is_null($child->getAttribute($rule['key']))) { $pass = false; } } else { - if ($rule['key'] != 'plaintext' and + if ($rule['key'] != 'plaintext' && is_null($child->getAttribute($rule['key'])) ) { $pass = false; @@ -234,8 +236,8 @@ protected function seek(array $nodes, array $rule, array $options) } // compare values - if ($pass and ! is_null($rule['key']) and - ! is_null($rule['value']) and $rule['value'] != '*' + if ($pass && ! is_null($rule['key']) && + ! is_null($rule['value']) && $rule['value'] != '*' ) { if ($rule['key'] == 'plaintext') { // plaintext search @@ -248,7 +250,7 @@ protected function seek(array $nodes, array $rule, array $options) $check = $this->match($rule['operator'], $rule['value'], $nodeValue); // handle multiple classes - if ( ! $check and $rule['key'] == 'class') { + if ( ! $check && $rule['key'] == 'class') { $childClasses = explode(' ', $child->getAttribute('class')); foreach ($childClasses as $class) { if ( ! empty($class)) {