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 |
||
11 | class FileManager implements IFileManager |
||
12 | { |
||
13 | /** |
||
14 | * @var IFileReader |
||
15 | */ |
||
16 | private $fileReader; |
||
17 | |||
18 | /** |
||
19 | * @var string |
||
20 | */ |
||
21 | private $primaryKey; |
||
22 | |||
23 | /** |
||
24 | * @param IFileReader $fileReader |
||
25 | * @param string $primaryKey |
||
26 | */ |
||
27 | public function __construct(IFileReader $fileReader, $primaryKey = '') |
||
32 | |||
33 | /** |
||
34 | * @param mixed $id |
||
35 | * @return bool |
||
36 | */ |
||
37 | public function has($id) |
||
41 | |||
42 | /** |
||
43 | * @param mixed $id |
||
44 | * @return mixed|null |
||
45 | */ |
||
46 | public function get($id) |
||
53 | |||
54 | /** |
||
55 | * @param array $ids |
||
56 | * @return mixed |
||
|
|||
57 | */ |
||
58 | View Code Duplication | public function getList(array $ids) |
|
67 | |||
68 | /** |
||
69 | * @param callable $filter |
||
70 | * @return mixed |
||
71 | */ |
||
72 | View Code Duplication | public function find(callable $filter = null) |
|
81 | |||
82 | /** |
||
83 | * @return mixed |
||
84 | */ |
||
85 | public function getAll() |
||
96 | |||
97 | /** |
||
98 | * @param mixed $dataList |
||
99 | * @return int |
||
100 | */ |
||
101 | public function set($dataList) |
||
105 | |||
106 | /** |
||
107 | * @param mixed $data |
||
108 | * @return int |
||
109 | */ |
||
110 | public function add($data) |
||
121 | |||
122 | /** |
||
123 | * @param mixed $data |
||
124 | * @return mixed |
||
125 | */ |
||
126 | private function retrieveKey($data) |
||
145 | |||
146 | /** |
||
147 | * @param mixed $dataList |
||
148 | * @return int |
||
149 | */ |
||
150 | public function addList($dataList) |
||
158 | |||
159 | /** |
||
160 | * @param mixed $id |
||
161 | * @param mixed $data |
||
162 | * @return int |
||
163 | */ |
||
164 | public function modify($id, $data) |
||
173 | |||
174 | /** |
||
175 | * @param mixed $dataList |
||
176 | * @return int |
||
177 | */ |
||
178 | public function modifyList($dataList) |
||
186 | |||
187 | /** |
||
188 | * @param mixed $id |
||
189 | * @return int |
||
190 | */ |
||
191 | public function remove($id) |
||
197 | |||
198 | /** |
||
199 | * @param array $ids |
||
200 | * @return int |
||
201 | */ |
||
202 | public function removeList(array $ids) |
||
210 | |||
211 | /** |
||
212 | * @return int |
||
213 | */ |
||
214 | public function removeAll() |
||
218 | |||
219 | /** |
||
220 | * Getter of $fileReader |
||
221 | * |
||
222 | * @return IFileReader |
||
223 | */ |
||
224 | protected function getFileReader() |
||
228 | |||
229 | /** |
||
230 | * Getter of $primaryKey |
||
231 | * |
||
232 | * @return string |
||
233 | */ |
||
234 | protected function getPrimaryKey() |
||
238 | |||
239 | /** |
||
240 | * Setter of $primaryKey |
||
241 | * |
||
242 | * @param string $primaryKey |
||
243 | */ |
||
244 | private function setPrimaryKey($primaryKey) |
||
248 | |||
249 | /** |
||
250 | * Setter of $fileReader |
||
251 | * |
||
252 | * @param IFileReader $fileReader |
||
253 | */ |
||
254 | private function setFileReader(IFileReader $fileReader) |
||
258 | } |
||
259 |
This check looks for the generic type
array
as a return type and suggests a more specific type. This type is inferred from the actual code.