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 |
||
12 | class InMemoryStorage implements Storage |
||
13 | { |
||
14 | /** |
||
15 | * The data collection. |
||
16 | * |
||
17 | * @var array |
||
18 | */ |
||
19 | private $data; |
||
20 | |||
21 | /** |
||
22 | * Constructor. |
||
23 | * |
||
24 | * @param mixed $data The data |
||
25 | */ |
||
26 | public function __construct($data) |
||
30 | |||
31 | /** |
||
32 | * {@inheritdoc} |
||
33 | */ |
||
34 | View Code Duplication | public function findAll($limit, $offset) |
|
47 | |||
48 | /** |
||
49 | * {@inheritdoc} |
||
50 | */ |
||
51 | View Code Duplication | public function query(array $criteria, $limit, $offset) |
|
69 | |||
70 | /** |
||
71 | * {@inheritdoc} |
||
72 | */ |
||
73 | public function properties() |
||
81 | |||
82 | /** |
||
83 | * {@inheritdoc} |
||
84 | */ |
||
85 | public function size() |
||
89 | |||
90 | /** |
||
91 | * Paginates the given collection of data. |
||
92 | * |
||
93 | * @param array $data The collection data |
||
94 | * @param int $limit Logs per page |
||
95 | * @param int $offset The number of the page |
||
96 | * |
||
97 | * @return array |
||
98 | */ |
||
99 | private function paginate($data, $limit, $offset) |
||
103 | |||
104 | /** |
||
105 | * Compares the given two items. |
||
106 | * |
||
107 | * @param mixed $item1 The first item |
||
108 | * @param mixed $item2 The second item |
||
109 | * |
||
110 | * @return int |
||
111 | */ |
||
112 | View Code Duplication | private function sort($item1, $item2) |
|
130 | } |
||
131 |
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..