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 | class DoctrineCollectionDataSource extends FilterableDataSource implements IDataSource |
||
18 | { |
||
19 | |||
20 | /** |
||
21 | * @var Collection |
||
22 | */ |
||
23 | protected $data_source; |
||
24 | |||
25 | /** |
||
26 | * @var string |
||
27 | */ |
||
28 | protected $primary_key; |
||
29 | |||
30 | /** |
||
31 | * @var Criteria |
||
32 | */ |
||
33 | protected $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 | protected function initialize() |
||
61 | |||
62 | |||
63 | /******************************************************************************** |
||
64 | * IDataSource implementation * |
||
65 | ********************************************************************************/ |
||
66 | |||
67 | |||
68 | /** |
||
69 | * Get count of data |
||
70 | * @return int |
||
71 | */ |
||
72 | public function getCount() |
||
78 | |||
79 | |||
80 | /** |
||
81 | * Get the data |
||
82 | * @return array |
||
83 | */ |
||
84 | public function getData() |
||
90 | |||
91 | |||
92 | /** |
||
93 | * Filter data - get one row |
||
94 | * @param array $condition |
||
95 | * @return static |
||
96 | */ |
||
97 | public function filterOne(array $condition) |
||
106 | |||
107 | |||
108 | /** |
||
109 | * Filter by date |
||
110 | * @param Filter\FilterDate $filter |
||
111 | * @return void |
||
112 | */ |
||
113 | public function applyFilterDate(Filter\FilterDate $filter) |
||
125 | |||
126 | |||
127 | /** |
||
128 | * Filter by date range |
||
129 | * @param Filter\FilterDateRange $filter |
||
130 | * @return void |
||
131 | */ |
||
132 | public function applyFilterDateRange(Filter\FilterDateRange $filter) |
||
154 | |||
155 | |||
156 | /** |
||
157 | * Filter by range |
||
158 | * @param Filter\FilterRange $filter |
||
159 | * @return void |
||
160 | */ |
||
161 | public function applyFilterRange(Filter\FilterRange $filter) |
||
175 | |||
176 | |||
177 | /** |
||
178 | * Filter by keyword |
||
179 | * @param Filter\FilterText $filter |
||
180 | * @return void |
||
181 | */ |
||
182 | public function applyFilterText(Filter\FilterText $filter) |
||
201 | |||
202 | |||
203 | /** |
||
204 | * Filter by multi select value |
||
205 | * @param Filter\FilterMultiSelect $filter |
||
206 | * @return void |
||
207 | */ |
||
208 | public function applyFilterMultiSelect(Filter\FilterMultiSelect $filter) |
||
215 | |||
216 | |||
217 | /** |
||
218 | * Filter by select value |
||
219 | * @param Filter\FilterSelect $filter |
||
220 | * @return void |
||
221 | */ |
||
222 | public function applyFilterSelect(Filter\FilterSelect $filter) |
||
229 | |||
230 | |||
231 | /** |
||
232 | * Apply limit and offset on data |
||
233 | * @param int $offset |
||
234 | * @param int $limit |
||
235 | * @return static |
||
236 | */ |
||
237 | public function limit($offset, $limit) |
||
243 | |||
244 | |||
245 | /** |
||
246 | * Sort data |
||
247 | * @param Sorting $sorting |
||
248 | * @return static |
||
249 | */ |
||
250 | public function sort(Sorting $sorting) |
||
271 | |||
272 | } |
||
273 |
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.