GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Passed
Pull Request — master (#9)
by no
09:19
created

ChildElementMatcher::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 1
dl 0
loc 4
ccs 0
cts 4
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace WMDE\HamcrestHtml;
4
5
use Hamcrest\Description;
6
use Hamcrest\Matcher;
7
use Hamcrest\TypeSafeDiagnosingMatcher;
8
9
class ChildElementMatcher extends TypeSafeDiagnosingMatcher {
10
11
	/**
12
	 * @var Matcher|string|null
13
	 */
14
	private $matcher;
15
16
	/**
17
	 * @param Matcher|string|null $elementMatcher
18
	 *
19
	 * @return self
20
	 */
21
	public static function havingChild( $elementMatcher = null ) {
22
		return new static( $elementMatcher );
23
	}
24
25
	/**
26
	 * @param Matcher|string|null $matcher
27
	 */
28
	public function __construct( $matcher = null ) {
29
		parent::__construct( \DOMNode::class );
30
		$this->matcher = $matcher;
31
	}
32
33 7 View Code Duplication
	public function describeTo( Description $description ) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
34 7
		$description->appendText( 'having child ' );
35 7
		if ( $this->matcher ) {
36 6
			if ( $this->matcher instanceof Matcher ) {
37 6
				$description->appendDescriptionOf( $this->matcher );
38 6
			} else {
39
				$description->appendValue( $this->matcher );
40
			}
41 6
		}
42 7
	}
43
44
	/**
45
	 * @param \DOMDocument|\DOMNode $item
46
	 * @param Description $mismatchDescription
47
	 *
48
	 * @return bool
49
	 */
50 17
	protected function matchesSafelyWithDiagnosticDescription( $item, Description $mismatchDescription ) {
51 17
		if ( $item instanceof \DOMDocument ) {
52 16
			$item = $item->documentElement->childNodes->item( 0 );
53
			/** @var \DOMElement $item */
54 16
		}
55 17
		$directChildren = $item->childNodes;
56
57 17
		if ( $directChildren->length === 0 ) {
58 1
			$mismatchDescription->appendText( 'having no children' );
59 1
			return false;
60
		}
61
62 16
		if ( !$this->matcher ) {
63
			return true;
64
		}
65
66 16
		if ( $this->matcher instanceof Matcher ) {
67 16
			foreach ( new XmlNodeRecursiveIterator( $directChildren ) as $child ) {
68 16
				if ( $this->matcher->matches( $child ) ) {
69 10
					return true;
70
				}
71 11
			}
72 6
		} else {
73
			if ( $item->getElementsByTagName( $this->matcher )->length !== 0 ) {
74
				return true;
75
			}
76
		}
77
78 6
		$mismatchDescription->appendText( 'having no children ' )->appendDescriptionOf( $this->matcher );
0 ignored issues
show
Bug introduced by
It seems like $this->matcher can also be of type string; however, Hamcrest\Description::appendDescriptionOf() does only seem to accept object<Hamcrest\SelfDescribing>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
79 6
		return false;
80
	}
81
82
}
83