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 | /** |
||
| 37 | * @param Collection $collection |
||
| 38 | * @param string $primary_key |
||
| 39 | */ |
||
| 40 | public function __construct(Collection $collection, $primary_key) |
||
| 46 | |||
| 47 | |||
| 48 | /** |
||
| 49 | * @return Collection |
||
| 50 | */ |
||
| 51 | private function getFilteredCollection() |
||
| 55 | |||
| 56 | |||
| 57 | /******************************************************************************** |
||
| 58 | * IDataSource implementation * |
||
| 59 | ********************************************************************************/ |
||
| 60 | |||
| 61 | |||
| 62 | /** |
||
| 63 | * Get count of data |
||
| 64 | * @return int |
||
| 65 | */ |
||
| 66 | public function getCount() |
||
| 70 | |||
| 71 | |||
| 72 | /** |
||
| 73 | * Get the data |
||
| 74 | * @return array |
||
| 75 | */ |
||
| 76 | public function getData() |
||
| 80 | |||
| 81 | |||
| 82 | /** |
||
| 83 | * Filter data - get one row |
||
| 84 | * @param array $condition |
||
| 85 | * @return static |
||
| 86 | */ |
||
| 87 | public function filterOne(array $condition) |
||
| 96 | |||
| 97 | |||
| 98 | /** |
||
| 99 | * Filter by date |
||
| 100 | * @param Filter\FilterDate $filter |
||
| 101 | * @return void |
||
| 102 | */ |
||
| 103 | public function applyFilterDate(Filter\FilterDate $filter) |
||
| 115 | |||
| 116 | |||
| 117 | /** |
||
| 118 | * Filter by date range |
||
| 119 | * @param Filter\FilterDateRange $filter |
||
| 120 | * @return void |
||
| 121 | */ |
||
| 122 | public function applyFilterDateRange(Filter\FilterDateRange $filter) |
||
| 144 | |||
| 145 | |||
| 146 | /** |
||
| 147 | * Filter by range |
||
| 148 | * @param Filter\FilterRange $filter |
||
| 149 | * @return void |
||
| 150 | */ |
||
| 151 | public function applyFilterRange(Filter\FilterRange $filter) |
||
| 165 | |||
| 166 | |||
| 167 | /** |
||
| 168 | * Filter by keyword |
||
| 169 | * @param Filter\FilterText $filter |
||
| 170 | * @return void |
||
| 171 | */ |
||
| 172 | public function applyFilterText(Filter\FilterText $filter) |
||
| 191 | |||
| 192 | |||
| 193 | /** |
||
| 194 | * Filter by multi select value |
||
| 195 | * @param Filter\FilterMultiSelect $filter |
||
| 196 | * @return void |
||
| 197 | */ |
||
| 198 | public function applyFilterMultiSelect(Filter\FilterMultiSelect $filter) |
||
| 205 | |||
| 206 | |||
| 207 | /** |
||
| 208 | * Filter by select value |
||
| 209 | * @param Filter\FilterSelect $filter |
||
| 210 | * @return void |
||
| 211 | */ |
||
| 212 | public function applyFilterSelect(Filter\FilterSelect $filter) |
||
| 219 | |||
| 220 | |||
| 221 | /** |
||
| 222 | * Apply limit and offset on data |
||
| 223 | * @param int $offset |
||
| 224 | * @param int $limit |
||
| 225 | * @return static |
||
| 226 | */ |
||
| 227 | public function limit($offset, $limit) |
||
| 233 | |||
| 234 | |||
| 235 | /** |
||
| 236 | * Sort data |
||
| 237 | * @param Sorting $sorting |
||
| 238 | * @return static |
||
| 239 | */ |
||
| 240 | public function sort(Sorting $sorting) |
||
| 261 | |||
| 262 | } |
||
| 263 |
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.