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   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 74
Duplicated Lines 14.86 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 71.43%

Importance

Changes 0
Metric Value
dl 11
loc 74
ccs 25
cts 35
cp 0.7143
rs 10
c 0
b 0
f 0
wmc 13
lcom 1
cbo 4

4 Methods

Rating   Name   Duplication   Size   Complexity  
A havingChild() 0 3 1
A __construct() 0 4 1
A describeTo() 10 10 3
C matchesSafelyWithDiagnosticDescription() 0 31 8

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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