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 |
||
| 16 | final class DoctrineCollectionDataSource extends FilterableDataSource implements IDataSource |
||
| 17 | { |
||
| 18 | |||
| 19 | /** |
||
| 20 | * @var Collection |
||
| 21 | */ |
||
| 22 | private $data_source; |
||
| 23 | |||
| 24 | /** |
||
| 25 | * @var string |
||
| 26 | */ |
||
| 27 | private $primary_key; |
||
| 28 | |||
| 29 | /** |
||
| 30 | * @var Criteria |
||
| 31 | */ |
||
| 32 | private $criteria; |
||
| 33 | |||
| 34 | |||
| 35 | /** |
||
| 36 | * @param Collection $collection |
||
| 37 | * @param string $primary_key |
||
| 38 | */ |
||
| 39 | public function __construct(Collection $collection, $primary_key) |
||
| 45 | |||
| 46 | |||
| 47 | /** |
||
| 48 | * @return Collection |
||
| 49 | */ |
||
| 50 | private function getFilteredCollection() |
||
| 54 | |||
| 55 | |||
| 56 | /******************************************************************************** |
||
| 57 | * IDataSource implementation * |
||
| 58 | ********************************************************************************/ |
||
| 59 | |||
| 60 | |||
| 61 | /** |
||
| 62 | * Get count of data |
||
| 63 | * @return int |
||
| 64 | */ |
||
| 65 | public function getCount() |
||
| 69 | |||
| 70 | |||
| 71 | /** |
||
| 72 | * Get the data |
||
| 73 | * @return array |
||
| 74 | */ |
||
| 75 | public function getData() |
||
| 79 | |||
| 80 | |||
| 81 | /** |
||
| 82 | * Filter data - get one row |
||
| 83 | * @param array $condition |
||
| 84 | * @return static |
||
| 85 | */ |
||
| 86 | public function filterOne(array $condition) |
||
| 95 | |||
| 96 | |||
| 97 | /** |
||
| 98 | * Filter by date |
||
| 99 | * @param Filter\FilterDate $filter |
||
| 100 | * @return void |
||
| 101 | */ |
||
| 102 | public function applyFilterDate(Filter\FilterDate $filter) |
||
| 114 | |||
| 115 | |||
| 116 | /** |
||
| 117 | * Filter by date range |
||
| 118 | * @param Filter\FilterDateRange $filter |
||
| 119 | * @return void |
||
| 120 | */ |
||
| 121 | public function applyFilterDateRange(Filter\FilterDateRange $filter) |
||
| 143 | |||
| 144 | |||
| 145 | /** |
||
| 146 | * Filter by range |
||
| 147 | * @param Filter\FilterRange $filter |
||
| 148 | * @return void |
||
| 149 | */ |
||
| 150 | public function applyFilterRange(Filter\FilterRange $filter) |
||
| 164 | |||
| 165 | |||
| 166 | /** |
||
| 167 | * Filter by keyword |
||
| 168 | * @param Filter\FilterText $filter |
||
| 169 | * @return void |
||
| 170 | */ |
||
| 171 | public function applyFilterText(Filter\FilterText $filter) |
||
| 190 | |||
| 191 | |||
| 192 | /** |
||
| 193 | * Filter by multi select value |
||
| 194 | * @param Filter\FilterMultiSelect $filter |
||
| 195 | * @return void |
||
| 196 | */ |
||
| 197 | public function applyFilterMultiSelect(Filter\FilterMultiSelect $filter) |
||
| 204 | |||
| 205 | |||
| 206 | /** |
||
| 207 | * Filter by select value |
||
| 208 | * @param Filter\FilterSelect $filter |
||
| 209 | * @return void |
||
| 210 | */ |
||
| 211 | public function applyFilterSelect(Filter\FilterSelect $filter) |
||
| 218 | |||
| 219 | |||
| 220 | /** |
||
| 221 | * Apply limit and offset on data |
||
| 222 | * @param int $offset |
||
| 223 | * @param int $limit |
||
| 224 | * @return static |
||
| 225 | */ |
||
| 226 | public function limit($offset, $limit) |
||
| 232 | |||
| 233 | |||
| 234 | /** |
||
| 235 | * Sort data |
||
| 236 | * @param Sorting $sorting |
||
| 237 | * @return static |
||
| 238 | */ |
||
| 239 | public function sort(Sorting $sorting) |
||
| 260 | |||
| 261 | } |
||
| 262 |
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.