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
02:29
created

AttributeMatcher   A

Complexity

Total Complexity 22

Size/Duplication

Total Lines 118
Duplicated Lines 11.86 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 30.91%

Importance

Changes 0
Metric Value
dl 14
loc 118
ccs 17
cts 55
cp 0.3091
rs 10
c 0
b 0
f 0
wmc 22
lcom 1
cbo 3

5 Methods

Rating   Name   Duplication   Size   Complexity  
A withAttribute() 0 3 1
A __construct() 0 6 1
A havingValue() 0 8 1
A describeTo() 0 17 4
C matchesSafelyWithDiagnosticDescription() 14 46 15

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