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 | /** |
||
| 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) { |
||
| 29 | |||
| 30 | /** |
||
| 31 | * AttributeMatcher constructor. |
||
| 32 | * @param \Hamcrest\Matcher|string $attributeNameMatcher |
||
| 33 | */ |
||
| 34 | public function __construct($attributeNameMatcher) |
||
| 40 | |||
| 41 | /** |
||
| 42 | * @param Matcher|mixed $value |
||
| 43 | * @return AttributeMatcher |
||
| 44 | */ |
||
| 45 | public function havingValue($value) |
||
| 53 | |||
| 54 | 2 | public function describeTo(Description $description) |
|
| 71 | |||
| 72 | /** |
||
| 73 | * @param \DOMElement $item |
||
| 74 | * @param Description $mismatchDescription |
||
| 75 | * |
||
| 76 | * @return bool |
||
| 77 | */ |
||
| 78 | 6 | protected function matchesSafelyWithDiagnosticDescription($item, Description $mismatchDescription) |
|
| 124 | |||
| 125 | } |
||
| 126 |
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.