Skip to content

Commit

Permalink
Fixed some code quality issues found by scrutinizer
Browse files Browse the repository at this point in the history
  • Loading branch information
paquettg committed Mar 20, 2016
1 parent a4eae37 commit 80f5474
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 44 deletions.
4 changes: 2 additions & 2 deletions src/PHPHtmlParser/Dom.php
Expand Up @@ -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
Expand Down Expand Up @@ -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)) {
Expand Down
58 changes: 28 additions & 30 deletions src/PHPHtmlParser/Dom/AbstractNode.php
Expand Up @@ -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;
Expand Down Expand Up @@ -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 = [
Expand All @@ -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.
*
Expand Down
2 changes: 1 addition & 1 deletion src/PHPHtmlParser/Dom/HtmlNode.php
Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion src/PHPHtmlParser/Dom/Tag.php
Expand Up @@ -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);
}
Expand Down
22 changes: 12 additions & 10 deletions src/PHPHtmlParser/Selector.php
Expand Up @@ -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;
}
Expand Down Expand Up @@ -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
Expand All @@ -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());
Expand All @@ -211,21 +213,21 @@ 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
$pass = false;
}

// 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;
Expand All @@ -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
Expand All @@ -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)) {
Expand Down

0 comments on commit 80f5474

Please sign in to comment.