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

AttributeMatcher::withAttribute()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 3
ccs 0
cts 2
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
8
class AttributeMatcher extends TagMatcher {
9
10
	/**
11
	 * @var Matcher|string
12
	 */
13
	private $attributeNameMatcher;
14
15
	/**
16
	 * @var Matcher|string|null
17
	 */
18
	private $valueMatcher;
19
20
	/**
21
	 * @param Matcher|string $attributeName
22
	 *
23
	 * @return self
24
	 */
25
	public static function withAttribute( $attributeName ) {
26
		return new static( $attributeName );
27
	}
28
29
	/**
30
	 * @param Matcher|string $attributeNameMatcher
31
	 */
32
	public function __construct( $attributeNameMatcher ) {
33
		parent::__construct();
34
35
		$this->attributeNameMatcher = $attributeNameMatcher;
36
	}
37
38
	/**
39
	 * @param Matcher|string $value
40
	 *
41
	 * @return AttributeMatcher
42
	 */
43
	public function havingValue( $value ) {
44
		// TODO: Throw exception if value is set
45
		$result = clone $this;
46
		$result->valueMatcher = $value;
47
48
		return $result;
49
	}
50
51 2
	public function describeTo( Description $description ) {
52 2
		$description->appendText( 'with attribute ' );
53 2
		if ( $this->attributeNameMatcher instanceof Matcher ) {
54
			$description->appendDescriptionOf( $this->attributeNameMatcher );
55
		} else {
56 2
			$description->appendValue( $this->attributeNameMatcher );
57
		}
58 2
		if ( $this->valueMatcher !== null ) {
59 1
			$description->appendText( ' having value ' );
60 1
			if ( $this->attributeNameMatcher instanceof Matcher ) {
61
				$description->appendDescriptionOf( $this->valueMatcher );
0 ignored issues
show
Bug introduced by
It seems like $this->valueMatcher 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...
62
			} else {
63 1
				$description->appendValue( $this->valueMatcher );
64
			}
65 1
		}
66 2
	}
67
68
	/**
69
	 * @param \DOMElement $item
70
	 * @param Description $mismatchDescription
71
	 *
72
	 * @return bool
73
	 */
74 6
	protected function matchesSafelyWithDiagnosticDescription( $item, Description $mismatchDescription ) {
75 6
		if ( $this->valueMatcher === null ) {
76 2
			if ( $this->attributeNameMatcher instanceof Matcher ) {
77
				/** @var \DOMAttr $attribute */
78
				foreach ( $item->attributes as $attribute ) {
79
					if ( $this->attributeNameMatcher->matches( $attribute->name ) ) {
80
						return true;
81
					}
82
				}
83
			} else {
84 2
				return $item->hasAttribute( $this->attributeNameMatcher );
85
			}
86
		} else {
87 4
			if ( $this->attributeNameMatcher instanceof Matcher ) {
88
				if ( $this->valueMatcher instanceof Matcher ) {
89
					/** @var \DOMAttr $attribute */
90 View Code Duplication
					foreach ( $item->attributes as $attribute ) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
91
						if ( $this->attributeNameMatcher->matches( $attribute->name )
92
							&& $this->valueMatcher->matches( $attribute->value )
93
						) {
94
							return true;
95
						}
96
					}
97
				} else {
98
					/** @var \DOMAttr $attribute */
99 View Code Duplication
					foreach ( $item->attributes as $attribute ) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
100
						if ( $this->attributeNameMatcher->matches( $attribute->name )
101
							&& $attribute->value === $this->valueMatcher
102
						) {
103
							return true;
104
						}
105
					}
106
				}
107
			} else {
108 4
				if ( $this->valueMatcher instanceof Matcher ) {
109
					return $item->hasAttribute( $this->attributeNameMatcher )
110
						&& $this->valueMatcher->matches( $item->getAttribute( $this->attributeNameMatcher ) );
111
				} else {
112 4
					return $item->getAttribute( $this->attributeNameMatcher ) === $this->valueMatcher;
113
				}
114
			}
115
		}
116
117
		return false;
118
	}
119
120
}
121