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 |
||
23 | class ResultCollection implements \Iterator, \Countable |
||
24 | { |
||
25 | /** |
||
26 | * @var Result[] |
||
27 | */ |
||
28 | private $results; |
||
29 | |||
30 | /** |
||
31 | * Constructor. |
||
32 | * |
||
33 | * @param Result[] ...$results |
||
34 | */ |
||
35 | 6 | public function __construct(Result ...$results) |
|
39 | |||
40 | /** |
||
41 | * Find the result by phone |
||
42 | * |
||
43 | * @param Phone $phone |
||
44 | * |
||
45 | * @return Result |
||
46 | * |
||
47 | * @throws ResultNotFoundException |
||
48 | */ |
||
49 | 2 | public function findByPhone(Phone $phone): Result |
|
62 | |||
63 | /** |
||
64 | * Get successfully results |
||
65 | * |
||
66 | * @return ResultCollection |
||
67 | */ |
||
68 | View Code Duplication | public function getSuccessfullyResults(): ResultCollection |
|
76 | |||
77 | /** |
||
78 | * Get failed results |
||
79 | * |
||
80 | * @return ResultCollection |
||
81 | */ |
||
82 | View Code Duplication | public function getFailedResults(): ResultCollection |
|
90 | |||
91 | /** |
||
92 | * {@inheritdoc} |
||
93 | */ |
||
94 | 2 | public function current(): Result |
|
98 | |||
99 | /** |
||
100 | * {@inheritdoc} |
||
101 | */ |
||
102 | 2 | public function next(): void |
|
106 | |||
107 | /** |
||
108 | * {@inheritdoc} |
||
109 | */ |
||
110 | 1 | public function key() |
|
114 | |||
115 | /** |
||
116 | * {@inheritdoc} |
||
117 | */ |
||
118 | 2 | public function valid(): bool |
|
122 | |||
123 | /** |
||
124 | * {@inheritdoc} |
||
125 | */ |
||
126 | 2 | public function rewind(): void |
|
130 | |||
131 | /** |
||
132 | * {@inheritdoc} |
||
133 | */ |
||
134 | 3 | public function count(): int |
|
138 | } |
||
139 |
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..