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 |
||
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 ) { |
||
28 | |||
29 | /** |
||
30 | * @param Matcher|string $attributeNameMatcher |
||
31 | */ |
||
32 | public function __construct( $attributeNameMatcher ) { |
||
37 | |||
38 | /** |
||
39 | * @param Matcher|string $value |
||
40 | * |
||
41 | * @return AttributeMatcher |
||
42 | */ |
||
43 | public function havingValue( $value ) { |
||
50 | |||
51 | 2 | public function describeTo( Description $description ) { |
|
67 | |||
68 | /** |
||
69 | * @param \DOMElement $item |
||
70 | * @param Description $mismatchDescription |
||
71 | * |
||
72 | * @return bool |
||
73 | */ |
||
74 | 6 | protected function matchesSafelyWithDiagnosticDescription( $item, Description $mismatchDescription ) { |
|
119 | |||
120 | } |
||
121 |
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:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.