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 |
||
7 | class LabelCollection implements \Countable |
||
8 | { |
||
9 | /** |
||
10 | * @var Label[] |
||
11 | */ |
||
12 | private $labels; |
||
13 | |||
14 | /** |
||
15 | * @param Label[] $labels |
||
16 | */ |
||
17 | public function __construct(array $labels = []) |
||
32 | |||
33 | /** |
||
34 | * @param Label $label |
||
35 | * @return LabelCollection |
||
36 | */ |
||
37 | public function with(Label $label) |
||
47 | |||
48 | /** |
||
49 | * @param Label $label |
||
50 | * @return LabelCollection |
||
51 | */ |
||
52 | View Code Duplication | public function without(Label $label) |
|
63 | |||
64 | /** |
||
65 | * @inheritdoc |
||
66 | */ |
||
67 | public function count() |
||
71 | |||
72 | /** |
||
73 | * @param Label $label |
||
74 | * @return bool |
||
75 | */ |
||
76 | View Code Duplication | public function contains(Label $label) |
|
87 | |||
88 | /** |
||
89 | * @return Label[] |
||
90 | */ |
||
91 | public function asArray() |
||
95 | |||
96 | /** |
||
97 | * @param LabelCollection $labelCollectionToMerge |
||
98 | * @return LabelCollection |
||
99 | */ |
||
100 | public function merge(LabelCollection $labelCollectionToMerge) |
||
118 | |||
119 | /** |
||
120 | * @param LabelCollection $labelCollection |
||
121 | * @return LabelCollection |
||
122 | */ |
||
123 | public function intersect(LabelCollection $labelCollection) |
||
134 | |||
135 | /** |
||
136 | * @param callable $filterFunction |
||
137 | * @return LabelCollection |
||
138 | */ |
||
139 | public function filter(callable $filterFunction) |
||
145 | |||
146 | /** |
||
147 | * @param string[] $strings |
||
148 | * @return LabelCollection |
||
149 | */ |
||
150 | public static function fromStrings(array $strings) |
||
164 | |||
165 | /** |
||
166 | * @param CultureFeed_Cdb_Data_Keyword[] $keywords |
||
167 | * @return LabelCollection |
||
168 | */ |
||
169 | public static function fromKeywords($keywords) |
||
183 | |||
184 | /** |
||
185 | * @return string[] |
||
186 | */ |
||
187 | public function toStrings() |
||
198 | } |
||
199 |
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..