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 |
||
| 22 | class DoctrineDataSource extends FilterableDataSource implements IDataSource |
||
| 23 | { |
||
| 24 | |||
| 25 | /** |
||
| 26 | * Event called when datagrid data is loaded. |
||
| 27 | * @var callable[] |
||
| 28 | */ |
||
| 29 | public $onDataLoaded; |
||
| 30 | |||
| 31 | /** |
||
| 32 | * @var QueryBuilder |
||
| 33 | */ |
||
| 34 | protected $data_source; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * @var array |
||
| 38 | */ |
||
| 39 | protected $aggregations = []; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * @var string |
||
| 43 | */ |
||
| 44 | protected $primary_key; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * @var string |
||
| 48 | */ |
||
| 49 | protected $root_alias; |
||
| 50 | |||
| 51 | /** |
||
| 52 | * @var int |
||
| 53 | */ |
||
| 54 | protected $placeholder; |
||
| 55 | |||
| 56 | |||
| 57 | /** |
||
| 58 | * @param QueryBuilder $data_source |
||
| 59 | * @param string $primary_key |
||
| 60 | */ |
||
| 61 | public function __construct(QueryBuilder $data_source, $primary_key) |
||
| 67 | |||
| 68 | |||
| 69 | /** |
||
| 70 | * @return \Doctrine\ORM\Query |
||
| 71 | */ |
||
| 72 | public function getQuery() |
||
| 76 | |||
| 77 | |||
| 78 | /** |
||
| 79 | * @param string $column |
||
| 80 | * @return string |
||
| 81 | */ |
||
| 82 | private function checkAliases($column) |
||
| 95 | |||
| 96 | |||
| 97 | /******************************************************************************** |
||
| 98 | * IDataSource implementation * |
||
| 99 | ********************************************************************************/ |
||
| 100 | |||
| 101 | |||
| 102 | /** |
||
| 103 | * Get count of data |
||
| 104 | * @return int |
||
| 105 | */ |
||
| 106 | public function getCount() |
||
| 110 | |||
| 111 | |||
| 112 | /** |
||
| 113 | * Get the data |
||
| 114 | * @return array |
||
| 115 | */ |
||
| 116 | public function getData() |
||
| 126 | |||
| 127 | |||
| 128 | /** |
||
| 129 | * Filter data - get one row |
||
| 130 | * @param array $condition |
||
| 131 | * @return static |
||
| 132 | */ |
||
| 133 | public function filterOne(array $condition) |
||
| 146 | |||
| 147 | |||
| 148 | /** |
||
| 149 | * Filter by date |
||
| 150 | * @param Filter\FilterDate $filter |
||
| 151 | */ |
||
| 152 | public function applyFilterDate(Filter\FilterDate $filter) |
||
| 168 | |||
| 169 | |||
| 170 | /** |
||
| 171 | * Filter by date range |
||
| 172 | * @param Filter\FilterDateRange $filter |
||
| 173 | */ |
||
| 174 | public function applyFilterDateRange(Filter\FilterDateRange $filter) |
||
| 200 | |||
| 201 | |||
| 202 | /** |
||
| 203 | * Filter by range |
||
| 204 | * @param Filter\FilterRange $filter |
||
| 205 | */ |
||
| 206 | public function applyFilterRange(Filter\FilterRange $filter) |
||
| 224 | |||
| 225 | |||
| 226 | /** |
||
| 227 | * Filter by keyword |
||
| 228 | * @param Filter\FilterText $filter |
||
| 229 | */ |
||
| 230 | public function applyFilterText(Filter\FilterText $filter) |
||
| 258 | |||
| 259 | |||
| 260 | /** |
||
| 261 | * Filter by multi select value |
||
| 262 | * @param Filter\FilterMultiSelect $filter |
||
| 263 | */ |
||
| 264 | public function applyFilterMultiSelect(Filter\FilterMultiSelect $filter) |
||
| 274 | |||
| 275 | |||
| 276 | /** |
||
| 277 | * Filter by select value |
||
| 278 | * @param Filter\FilterSelect $filter |
||
| 279 | */ |
||
| 280 | public function applyFilterSelect(Filter\FilterSelect $filter) |
||
| 291 | |||
| 292 | |||
| 293 | /** |
||
| 294 | * Apply limit and offset on data |
||
| 295 | * @param int $offset |
||
| 296 | * @param int $limit |
||
| 297 | * @return static |
||
| 298 | */ |
||
| 299 | public function limit($offset, $limit) |
||
| 305 | |||
| 306 | |||
| 307 | /** |
||
| 308 | * Sort data |
||
| 309 | * @param Sorting $sorting |
||
| 310 | * @return static |
||
| 311 | */ |
||
| 312 | View Code Duplication | public function sort(Sorting $sorting) |
|
| 341 | |||
| 342 | |||
| 343 | /** |
||
| 344 | * Get unique int value for each instance class (self) |
||
| 345 | * @return int |
||
| 346 | */ |
||
| 347 | public function getPlaceholder() |
||
| 351 | |||
| 352 | /** |
||
| 353 | * @param string $aggregation_type |
||
| 354 | * @param string $column |
||
| 355 | * @return mixed |
||
| 356 | */ |
||
| 357 | public function addAggregationColumn($aggregation_type, $column) |
||
| 361 | |||
| 362 | /** |
||
| 363 | * get aggregation row |
||
| 364 | * @return array |
||
| 365 | */ |
||
| 366 | public function getAggregationData() |
||
| 375 | } |
||
| 376 |
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..