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 |
||
17 | final class DoctrineCollectionDataSource extends FilterableDataSource implements IDataSource |
||
18 | { |
||
19 | |||
20 | /** |
||
21 | * @var Collection |
||
22 | */ |
||
23 | private $data_source; |
||
24 | |||
25 | /** |
||
26 | * @var string |
||
27 | */ |
||
28 | private $primary_key; |
||
29 | |||
30 | /** |
||
31 | * @var Criteria |
||
32 | */ |
||
33 | private $criteria; |
||
34 | |||
35 | /** |
||
36 | * @var Collection |
||
37 | */ |
||
38 | private $matched; |
||
39 | |||
40 | |||
41 | /** |
||
42 | * @param Collection $collection |
||
43 | * @param string $primary_key |
||
44 | */ |
||
45 | public function __construct(Collection $collection, $primary_key) |
||
51 | |||
52 | |||
53 | /** |
||
54 | * @return Collection |
||
55 | */ |
||
56 | private function getMatched() |
||
64 | |||
65 | |||
66 | /******************************************************************************** |
||
67 | * IDataSource implementation * |
||
68 | ********************************************************************************/ |
||
69 | |||
70 | |||
71 | /** |
||
72 | * Get count of data |
||
73 | * @return int |
||
74 | */ |
||
75 | public function getCount() |
||
79 | |||
80 | |||
81 | /** |
||
82 | * Get the data |
||
83 | * @return array |
||
84 | */ |
||
85 | public function getData() |
||
89 | |||
90 | |||
91 | /** |
||
92 | * Filter data - get one row |
||
93 | * @param array $condition |
||
94 | * @return static |
||
95 | */ |
||
96 | public function filterOne(array $condition) |
||
105 | |||
106 | |||
107 | /** |
||
108 | * Filter by date |
||
109 | * @param Filter\FilterDate $filter |
||
110 | * @return void |
||
111 | */ |
||
112 | public function applyFilterDate(Filter\FilterDate $filter) |
||
124 | |||
125 | |||
126 | /** |
||
127 | * Filter by date range |
||
128 | * @param Filter\FilterDateRange $filter |
||
129 | * @return void |
||
130 | */ |
||
131 | public function applyFilterDateRange(Filter\FilterDateRange $filter) |
||
153 | |||
154 | |||
155 | /** |
||
156 | * Filter by range |
||
157 | * @param Filter\FilterRange $filter |
||
158 | * @return void |
||
159 | */ |
||
160 | public function applyFilterRange(Filter\FilterRange $filter) |
||
174 | |||
175 | |||
176 | /** |
||
177 | * Filter by keyword |
||
178 | * @param Filter\FilterText $filter |
||
179 | * @return void |
||
180 | */ |
||
181 | public function applyFilterText(Filter\FilterText $filter) |
||
200 | |||
201 | |||
202 | /** |
||
203 | * Filter by multi select value |
||
204 | * @param Filter\FilterMultiSelect $filter |
||
205 | * @return void |
||
206 | */ |
||
207 | public function applyFilterMultiSelect(Filter\FilterMultiSelect $filter) |
||
214 | |||
215 | |||
216 | /** |
||
217 | * Filter by select value |
||
218 | * @param Filter\FilterSelect $filter |
||
219 | * @return void |
||
220 | */ |
||
221 | public function applyFilterSelect(Filter\FilterSelect $filter) |
||
228 | |||
229 | |||
230 | /** |
||
231 | * Apply limit and offset on data |
||
232 | * @param int $offset |
||
233 | * @param int $limit |
||
234 | * @return static |
||
235 | */ |
||
236 | public function limit($offset, $limit) |
||
242 | |||
243 | |||
244 | /** |
||
245 | * Sort data |
||
246 | * @param Sorting $sorting |
||
247 | * @return static |
||
248 | */ |
||
249 | public function sort(Sorting $sorting) |
||
270 | |||
271 | } |
||
272 |
This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.