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 |
||
10 | abstract class BasicAttribute |
||
11 | { |
||
12 | /** @var mixed */ |
||
13 | private $value; |
||
14 | |||
15 | /** @var string */ |
||
16 | private $key; |
||
17 | |||
18 | /** |
||
19 | * BasicAttribute constructor. |
||
20 | * @param mixed $value |
||
21 | * @param string $key |
||
22 | */ |
||
23 | 13 | public function __construct($value, $key = '') |
|
29 | |||
30 | /** |
||
31 | * @return mixed |
||
32 | */ |
||
33 | 13 | public function getValue() |
|
37 | |||
38 | /** |
||
39 | * @param string $key |
||
40 | */ |
||
41 | 13 | private function setKey($key) |
|
45 | |||
46 | /** |
||
47 | * @return string |
||
48 | */ |
||
49 | 8 | public function getKey() |
|
53 | |||
54 | /** |
||
55 | * @return string |
||
56 | */ |
||
57 | 1 | public function __toString() |
|
61 | |||
62 | /** |
||
63 | * Prefix $tagName with the $prefix, if needed |
||
64 | * @param string $prefix |
||
|
|||
65 | * @param string $tagName |
||
66 | * @return string |
||
67 | */ |
||
68 | public function getPrefixedTagName($tagName, $prefix = null) |
||
75 | |||
76 | /** |
||
77 | * @param int $length |
||
78 | * @throws BpostInvalidLengthException |
||
79 | */ |
||
80 | 8 | public function validateLength($length) |
|
86 | |||
87 | /** |
||
88 | * @param array $allowedValues |
||
89 | * @throws BpostInvalidValueException |
||
90 | */ |
||
91 | 5 | public function validateChoice(array $allowedValues) |
|
97 | |||
98 | /** |
||
99 | * @param string $regexPattern |
||
100 | * @throws BpostInvalidPatternException |
||
101 | */ |
||
102 | 5 | public function validatePattern($regexPattern) |
|
108 | |||
109 | /** |
||
110 | * @return string |
||
111 | */ |
||
112 | protected abstract function getDefaultKey(); |
||
113 | |||
114 | /** |
||
115 | * @throws BpostLogicException |
||
116 | */ |
||
117 | public abstract function validate(); |
||
118 | |||
119 | } |
||
120 |
This check looks for
@param
annotations where the type inferred by our type inference engine differs from the declared type.It makes a suggestion as to what type it considers more descriptive.
Most often this is a case of a parameter that can be null in addition to its declared types.