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 |
||
23 | class DoctrineDataSource extends FilterableDataSource implements IDataSource |
||
24 | { |
||
25 | |||
26 | /** |
||
27 | * Event called when datagrid data is loaded. |
||
28 | * @var callable[] |
||
29 | */ |
||
30 | public $onDataLoaded; |
||
31 | |||
32 | /** |
||
33 | * @var QueryBuilder |
||
34 | */ |
||
35 | protected $data_source; |
||
36 | |||
37 | /** |
||
38 | * @var array |
||
39 | */ |
||
40 | protected $aggregations = []; |
||
41 | |||
42 | /** |
||
43 | * @var string |
||
44 | */ |
||
45 | protected $primary_key; |
||
46 | |||
47 | /** |
||
48 | * @var string |
||
49 | */ |
||
50 | protected $root_alias; |
||
51 | |||
52 | /** |
||
53 | * @var int |
||
54 | */ |
||
55 | protected $placeholder; |
||
56 | |||
57 | |||
58 | /** |
||
59 | * @param QueryBuilder $data_source |
||
60 | * @param string $primary_key |
||
61 | */ |
||
62 | public function __construct(QueryBuilder $data_source, $primary_key) |
||
63 | { |
||
64 | $this->placeholder = count($data_source->getParameters()); |
||
65 | $this->data_source = $data_source; |
||
66 | $this->primary_key = $primary_key; |
||
67 | } |
||
68 | |||
69 | |||
70 | /** |
||
71 | * @return \Doctrine\ORM\Query |
||
72 | */ |
||
73 | public function getQuery() |
||
74 | { |
||
75 | return $this->data_source->getQuery(); |
||
76 | } |
||
77 | |||
78 | |||
79 | /** |
||
80 | * @param string $column |
||
81 | * @return string |
||
82 | */ |
||
83 | private function checkAliases($column) |
||
84 | { |
||
85 | if (Strings::contains($column, ".")) { |
||
86 | return $column; |
||
87 | } |
||
88 | |||
89 | if (!isset($this->root_alias)) { |
||
90 | $this->root_alias = $this->data_source->getRootAliases(); |
||
|
|||
91 | $this->root_alias = current($this->root_alias); |
||
92 | } |
||
93 | |||
94 | return $this->root_alias.'.'.$column; |
||
95 | } |
||
96 | |||
97 | |||
98 | /******************************************************************************** |
||
99 | * IDataSource implementation * |
||
100 | ********************************************************************************/ |
||
101 | |||
102 | |||
103 | /** |
||
104 | * Get count of data |
||
105 | * @return int |
||
106 | */ |
||
107 | public function getCount() |
||
108 | { |
||
109 | return (new Paginator($this->getQuery()))->count(); |
||
110 | } |
||
111 | |||
112 | |||
113 | /** |
||
114 | * Get the data |
||
115 | * @return array |
||
116 | */ |
||
117 | public function getData() |
||
118 | { |
||
119 | $iterator = (new Paginator($this->getQuery()))->getIterator(); |
||
120 | |||
121 | $data = iterator_to_array($iterator); |
||
122 | |||
123 | $this->onDataLoaded($data); |
||
124 | |||
125 | return $data; |
||
126 | } |
||
127 | |||
128 | |||
129 | /** |
||
130 | * Filter data - get one row |
||
131 | * @param array $condition |
||
132 | * @return static |
||
133 | */ |
||
134 | public function filterOne(array $condition) |
||
135 | { |
||
136 | $p = $this->getPlaceholder(); |
||
137 | |||
138 | View Code Duplication | foreach ($condition as $column => $value) { |
|
139 | $c = $this->checkAliases($column); |
||
140 | |||
141 | $this->data_source->andWhere("$c = ?$p") |
||
142 | ->setParameter($p, $value); |
||
143 | } |
||
144 | |||
145 | return $this; |
||
146 | } |
||
147 | |||
148 | |||
149 | /** |
||
150 | * Filter by date |
||
151 | * @param Filter\FilterDate $filter |
||
152 | */ |
||
153 | public function applyFilterDate(Filter\FilterDate $filter) |
||
154 | { |
||
155 | $p1 = $this->getPlaceholder(); |
||
156 | $p2 = $this->getPlaceholder(); |
||
157 | |||
158 | foreach ($filter->getCondition() as $column => $value) { |
||
159 | $date = DateTimeHelper::tryConvertToDateTime($value, [$filter->getPhpFormat()]); |
||
160 | $c = $this->checkAliases($column); |
||
161 | |||
162 | $this->data_source |
||
163 | ->andWhere("$c >= ?$p1") |
||
164 | ->andWhere("$c <= ?$p2") |
||
165 | ->setParameter($p1, $date->format('Y-m-d 00:00:00')) |
||
166 | ->setParameter($p2, $date->format('Y-m-d 23:59:59')); |
||
167 | } |
||
168 | } |
||
169 | |||
170 | |||
171 | /** |
||
172 | * Filter by date range |
||
173 | * @param Filter\FilterDateRange $filter |
||
174 | */ |
||
175 | public function applyFilterDateRange(Filter\FilterDateRange $filter) |
||
176 | { |
||
177 | $conditions = $filter->getCondition(); |
||
178 | $c = $this->checkAliases($filter->getColumn()); |
||
179 | |||
180 | $value_from = $conditions[$filter->getColumn()]['from']; |
||
181 | $value_to = $conditions[$filter->getColumn()]['to']; |
||
182 | |||
183 | View Code Duplication | if ($value_from) { |
|
184 | $date_from = DateTimeHelper::tryConvertToDate($value_from, [$filter->getPhpFormat()]); |
||
185 | $date_from->setTime(0, 0, 0); |
||
186 | |||
187 | $p = $this->getPlaceholder(); |
||
188 | |||
189 | $this->data_source->andWhere("$c >= ?$p")->setParameter($p, $date_from->format('Y-m-d H:i:s')); |
||
190 | } |
||
191 | |||
192 | View Code Duplication | if ($value_to) { |
|
193 | $date_to = DateTimeHelper::tryConvertToDate($value_to, [$filter->getPhpFormat()]); |
||
194 | $date_to->setTime(23, 59, 59); |
||
195 | |||
196 | $p = $this->getPlaceholder(); |
||
197 | |||
198 | $this->data_source->andWhere("$c <= ?$p")->setParameter($p, $date_to->format('Y-m-d H:i:s')); |
||
199 | } |
||
200 | } |
||
201 | |||
202 | |||
203 | /** |
||
204 | * Filter by range |
||
205 | * @param Filter\FilterRange $filter |
||
206 | */ |
||
207 | public function applyFilterRange(Filter\FilterRange $filter) |
||
225 | |||
226 | |||
227 | /** |
||
228 | * Filter by keyword |
||
229 | * @param Filter\FilterText $filter |
||
230 | */ |
||
231 | public function applyFilterText(Filter\FilterText $filter) |
||
259 | |||
260 | |||
261 | /** |
||
262 | * Filter by multi select value |
||
263 | * @param Filter\FilterMultiSelect $filter |
||
264 | */ |
||
265 | public function applyFilterMultiSelect(Filter\FilterMultiSelect $filter) |
||
275 | |||
276 | |||
277 | /** |
||
278 | * Filter by select value |
||
279 | * @param Filter\FilterSelect $filter |
||
280 | */ |
||
281 | public function applyFilterSelect(Filter\FilterSelect $filter) |
||
292 | |||
293 | |||
294 | /** |
||
295 | * Apply limit and offset on data |
||
296 | * @param int $offset |
||
297 | * @param int $limit |
||
298 | * @return static |
||
299 | */ |
||
300 | public function limit($offset, $limit) |
||
306 | |||
307 | |||
308 | /** |
||
309 | * Sort data |
||
310 | * @param Sorting $sorting |
||
311 | * @return static |
||
312 | */ |
||
313 | View Code Duplication | public function sort(Sorting $sorting) |
|
342 | |||
343 | |||
344 | /** |
||
345 | * Get unique int value for each instance class (self) |
||
346 | * @return int |
||
347 | */ |
||
348 | public function getPlaceholder() |
||
352 | |||
353 | /** |
||
354 | * @param string $aggregation_type |
||
355 | * @param string $column |
||
356 | * @return mixed |
||
357 | */ |
||
358 | public function addAggregationColumn($aggregation_type, $column) |
||
359 | { |
||
360 | $this->aggregations[$column] = $aggregation_type; |
||
362 | |||
363 | /** |
||
364 | * get aggregation row |
||
365 | * @return array |
||
366 | */ |
||
367 | public function getAggregationData() |
||
378 | } |
||
379 |
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..