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 |
||
| 21 | class DoctrineDataSource extends FilterableDataSource implements IDataSource |
||
| 22 | { |
||
| 23 | |||
| 24 | /** |
||
| 25 | * Event called when datagrid data is loaded. |
||
| 26 | * @var callable[] |
||
| 27 | */ |
||
| 28 | public $onDataLoaded; |
||
| 29 | |||
| 30 | /** |
||
| 31 | * @var QueryBuilder |
||
| 32 | */ |
||
| 33 | protected $data_source; |
||
| 34 | |||
| 35 | /** |
||
| 36 | * @var array |
||
| 37 | */ |
||
| 38 | protected $aggregations = []; |
||
| 39 | |||
| 40 | /** |
||
| 41 | * @var string |
||
| 42 | */ |
||
| 43 | protected $primary_key; |
||
| 44 | |||
| 45 | /** |
||
| 46 | * @var string |
||
| 47 | */ |
||
| 48 | protected $root_alias; |
||
| 49 | |||
| 50 | /** |
||
| 51 | * @var int |
||
| 52 | */ |
||
| 53 | protected $placeholder; |
||
| 54 | |||
| 55 | |||
| 56 | /** |
||
| 57 | * @param QueryBuilder $data_source |
||
| 58 | * @param string $primary_key |
||
| 59 | */ |
||
| 60 | public function __construct(QueryBuilder $data_source, $primary_key) |
||
| 61 | { |
||
| 62 | $this->placeholder = count($data_source->getParameters()); |
||
| 63 | $this->data_source = $data_source; |
||
| 64 | $this->primary_key = $primary_key; |
||
| 65 | } |
||
| 66 | |||
| 67 | |||
| 68 | /** |
||
| 69 | * @return \Doctrine\ORM\Query |
||
| 70 | */ |
||
| 71 | public function getQuery() |
||
| 75 | |||
| 76 | |||
| 77 | /** |
||
| 78 | * @param string $column |
||
| 79 | * @return string |
||
| 80 | */ |
||
| 81 | private function checkAliases($column) |
||
| 94 | |||
| 95 | |||
| 96 | /******************************************************************************** |
||
| 97 | * IDataSource implementation * |
||
| 98 | ********************************************************************************/ |
||
| 99 | |||
| 100 | |||
| 101 | /** |
||
| 102 | * Get count of data |
||
| 103 | * @return int |
||
| 104 | */ |
||
| 105 | public function getCount() |
||
| 109 | |||
| 110 | |||
| 111 | /** |
||
| 112 | * Get the data |
||
| 113 | * @return array |
||
| 114 | */ |
||
| 115 | public function getData() |
||
| 125 | |||
| 126 | |||
| 127 | /** |
||
| 128 | * Filter data - get one row |
||
| 129 | * @param array $condition |
||
| 130 | * @return static |
||
| 131 | */ |
||
| 132 | public function filterOne(array $condition) |
||
| 145 | |||
| 146 | |||
| 147 | /** |
||
| 148 | * Filter by date |
||
| 149 | * @param Filter\FilterDate $filter |
||
| 150 | */ |
||
| 151 | public function applyFilterDate(Filter\FilterDate $filter) |
||
| 167 | |||
| 168 | |||
| 169 | /** |
||
| 170 | * Filter by date range |
||
| 171 | * @param Filter\FilterDateRange $filter |
||
| 172 | */ |
||
| 173 | public function applyFilterDateRange(Filter\FilterDateRange $filter) |
||
| 199 | |||
| 200 | |||
| 201 | /** |
||
| 202 | * Filter by range |
||
| 203 | * @param Filter\FilterRange $filter |
||
| 204 | */ |
||
| 205 | public function applyFilterRange(Filter\FilterRange $filter) |
||
| 223 | |||
| 224 | |||
| 225 | /** |
||
| 226 | * Filter by keyword |
||
| 227 | * @param Filter\FilterText $filter |
||
| 228 | */ |
||
| 229 | public function applyFilterText(Filter\FilterText $filter) |
||
| 257 | |||
| 258 | |||
| 259 | /** |
||
| 260 | * Filter by multi select value |
||
| 261 | * @param Filter\FilterMultiSelect $filter |
||
| 262 | */ |
||
| 263 | public function applyFilterMultiSelect(Filter\FilterMultiSelect $filter) |
||
| 273 | |||
| 274 | |||
| 275 | /** |
||
| 276 | * Filter by select value |
||
| 277 | * @param Filter\FilterSelect $filter |
||
| 278 | */ |
||
| 279 | public function applyFilterSelect(Filter\FilterSelect $filter) |
||
| 290 | |||
| 291 | |||
| 292 | /** |
||
| 293 | * Apply limit and offset on data |
||
| 294 | * @param int $offset |
||
| 295 | * @param int $limit |
||
| 296 | * @return static |
||
| 297 | */ |
||
| 298 | public function limit($offset, $limit) |
||
| 304 | |||
| 305 | |||
| 306 | /** |
||
| 307 | * Sort data |
||
| 308 | * @param Sorting $sorting |
||
| 309 | * @return static |
||
| 310 | */ |
||
| 311 | View Code Duplication | public function sort(Sorting $sorting) |
|
| 340 | |||
| 341 | |||
| 342 | /** |
||
| 343 | * Get unique int value for each instance class (self) |
||
| 344 | * @return int |
||
| 345 | */ |
||
| 346 | public function getPlaceholder() |
||
| 350 | |||
| 351 | /** |
||
| 352 | * @param string $aggregation_type |
||
| 353 | * @param string $column |
||
| 354 | * @return mixed |
||
| 355 | */ |
||
| 356 | public function addAggregationColumn($aggregation_type, $column) |
||
| 360 | |||
| 361 | /** |
||
| 362 | * get aggregation row |
||
| 363 | * @return array |
||
| 364 | */ |
||
| 365 | public function getAggregationData() |
||
| 369 | } |
||
| 370 |
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..