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 |
||
| 18 | class NextrasDataSource extends FilterableDataSource implements IDataSource |
||
| 19 | { |
||
| 20 | |||
| 21 | /** |
||
| 22 | * @var DbalCollection |
||
| 23 | */ |
||
| 24 | protected $data_source; |
||
| 25 | |||
| 26 | /** |
||
| 27 | * @var array |
||
| 28 | */ |
||
| 29 | protected $data = []; |
||
| 30 | |||
| 31 | /** |
||
| 32 | * @var string |
||
| 33 | */ |
||
| 34 | protected $primary_key; |
||
| 35 | |||
| 36 | |||
| 37 | /** |
||
| 38 | * @param ICollection $data_source |
||
| 39 | * @param string $primary_key |
||
| 40 | */ |
||
| 41 | public function __construct(ICollection $data_source, $primary_key) |
||
| 46 | |||
| 47 | |||
| 48 | /******************************************************************************** |
||
| 49 | * IDataSource implementation * |
||
| 50 | ********************************************************************************/ |
||
| 51 | |||
| 52 | |||
| 53 | /** |
||
| 54 | * Get count of data |
||
| 55 | * @return int |
||
| 56 | */ |
||
| 57 | public function getCount() |
||
| 58 | { |
||
| 59 | return $this->data_source->countStored(); |
||
| 60 | } |
||
| 61 | |||
| 62 | /** |
||
| 63 | * Get the data |
||
| 64 | * @return array |
||
| 65 | */ |
||
| 66 | public function getData() |
||
| 67 | { |
||
| 68 | /** |
||
| 69 | * Paginator is better if the query uses ManyToMany associations |
||
| 70 | */ |
||
| 71 | return $this->data ?: $this->data_source->fetchAll(); |
||
| 72 | } |
||
| 73 | |||
| 74 | |||
| 75 | /** |
||
| 76 | * Filter data - get one row |
||
| 77 | * @param array $condition |
||
| 78 | * @return static |
||
| 79 | */ |
||
| 80 | public function filterOne(array $condition) |
||
| 81 | { |
||
| 82 | $cond = []; |
||
| 83 | foreach ($condition as $key => $value) { |
||
| 84 | $cond[$this->prepareColumn($key)] = $value; |
||
| 85 | } |
||
| 86 | $this->data_source = $this->data_source->findBy($cond); |
||
| 87 | |||
| 88 | return $this; |
||
| 89 | } |
||
| 90 | |||
| 91 | |||
| 92 | /** |
||
| 93 | * Filter by date |
||
| 94 | * @param Filter\FilterDate $filter |
||
| 95 | * @return static |
||
| 96 | */ |
||
| 97 | public function applyFilterDate(Filter\FilterDate $filter) |
||
| 98 | { |
||
| 99 | foreach ($filter->getCondition() as $column => $value) { |
||
| 100 | $date = \DateTime::createFromFormat($filter->getPhpFormat(), $value); |
||
| 101 | $date_end = clone $date; |
||
| 102 | |||
| 103 | $this->data_source = $this->data_source->findBy([ |
||
| 104 | $this->prepareColumn($column) . '>=' => $date->setTime(0, 0, 0), |
||
| 105 | $this->prepareColumn($column) . '<=' => $date_end->setTime(23, 59, 59) |
||
| 106 | ]); |
||
| 107 | } |
||
| 108 | |||
| 109 | return $this; |
||
| 110 | } |
||
| 111 | |||
| 112 | |||
| 113 | /** |
||
| 114 | * Filter by date range |
||
| 115 | * @param Filter\FilterDateRange $filter |
||
| 116 | * @return void |
||
| 117 | */ |
||
| 118 | public function applyFilterDateRange(Filter\FilterDateRange $filter) |
||
| 119 | { |
||
| 120 | $conditions = $filter->getCondition(); |
||
| 121 | |||
| 122 | $value_from = $conditions[$filter->getColumn()]['from']; |
||
| 123 | $value_to = $conditions[$filter->getColumn()]['to']; |
||
| 124 | |||
| 125 | $dataCondition = []; |
||
| 126 | if ($value_from) { |
||
| 127 | $date_from = \DateTime::createFromFormat($filter->getPhpFormat(), $value_from); |
||
| 128 | $dataCondition[$this->prepareColumn($filter->getColumn()) . '>='] = $date_from->setTime(0, 0, 0); |
||
| 129 | } |
||
| 130 | |||
| 131 | if ($value_to) { |
||
| 132 | $date_to = \DateTime::createFromFormat($filter->getPhpFormat(), $value_to); |
||
| 133 | $dataCondition[$this->prepareColumn($filter->getColumn()) . '<='] = $date_to->setTime(23, 59, 59); |
||
| 134 | } |
||
| 135 | |||
| 136 | if (!empty($dataCondition)) { |
||
| 137 | $this->data_source = $this->data_source->findBy($dataCondition); |
||
| 138 | } |
||
| 139 | } |
||
| 140 | |||
| 141 | |||
| 142 | /** |
||
| 143 | * Filter by range |
||
| 144 | * @param Filter\FilterRange $filter |
||
| 145 | * @return void |
||
| 146 | */ |
||
| 147 | public function applyFilterRange(Filter\FilterRange $filter) |
||
| 148 | { |
||
| 149 | $conditions = $filter->getCondition(); |
||
| 150 | |||
| 151 | $value_from = $conditions[$filter->getColumn()]['from']; |
||
| 152 | $value_to = $conditions[$filter->getColumn()]['to']; |
||
| 153 | |||
| 154 | $dataCondition = []; |
||
| 155 | |||
| 156 | if ($value_from) { |
||
| 157 | $dataCondition[$this->prepareColumn($filter->getColumn()) . '>='] = $value_from; |
||
| 158 | } |
||
| 159 | |||
| 160 | if ($value_to) { |
||
| 161 | $dataCondition[$this->prepareColumn($filter->getColumn()) . '<='] = $value_to; |
||
| 162 | } |
||
| 163 | |||
| 164 | if (!empty($dataCondition)) { |
||
| 165 | $this->data_source = $this->data_source->findBy($dataCondition); |
||
| 166 | } |
||
| 167 | } |
||
| 168 | |||
| 169 | |||
| 170 | /** |
||
| 171 | * Filter by keyword |
||
| 172 | * @param Filter\FilterText $filter |
||
| 173 | * @return void |
||
| 174 | */ |
||
| 175 | public function applyFilterText(Filter\FilterText $filter) |
||
| 176 | { |
||
| 177 | $condition = $filter->getCondition(); |
||
| 178 | $expr = '('; |
||
| 179 | $params = []; |
||
| 180 | |||
| 181 | foreach ($condition as $column => $value) { |
||
| 182 | View Code Duplication | if ($filter->hasSplitWordsSearch() === FALSE) { |
|
| 183 | $words = [$value]; |
||
| 184 | } else { |
||
| 185 | $words = explode(' ', $value); |
||
| 186 | } |
||
| 187 | |||
| 188 | foreach ($words as $word) { |
||
| 189 | $expr .= "%column LIKE %s OR "; |
||
| 190 | $params[] = $column; |
||
| 191 | $params[] = "%$word%"; |
||
| 192 | } |
||
| 193 | } |
||
| 194 | |||
| 195 | $expr = preg_replace('/ OR $/', ')', $expr); |
||
| 196 | |||
| 197 | array_unshift($params, $expr); |
||
| 198 | |||
| 199 | call_user_func_array([$this->data_source->getQueryBuilder(), 'andWhere'], $params); |
||
| 200 | } |
||
| 201 | |||
| 202 | |||
| 203 | /** |
||
| 204 | * Filter by multi select value |
||
| 205 | * @param Filter\FilterMultiSelect $filter |
||
| 206 | * @return void |
||
| 207 | */ |
||
| 208 | public function applyFilterMultiSelect(Filter\FilterMultiSelect $filter) |
||
| 209 | { |
||
| 210 | $condition = $filter->getCondition(); |
||
| 211 | $values = $condition[$filter->getColumn()]; |
||
| 212 | $expr = '('; |
||
| 213 | |||
| 214 | foreach ($values as $value) { |
||
| 215 | $expr .= "%column = %any OR "; |
||
| 216 | $params[] = $filter->getColumn(); |
||
| 217 | $params[] = "$value"; |
||
| 218 | } |
||
| 219 | |||
| 220 | $expr = preg_replace('/ OR $/', ')', $expr); |
||
| 221 | |||
| 222 | array_unshift($params, $expr); |
||
| 223 | |||
| 224 | call_user_func_array([$this->data_source->getQueryBuilder(), 'andWhere'], $params); |
||
| 225 | } |
||
| 226 | |||
| 227 | |||
| 228 | /** |
||
| 229 | * Filter by select value |
||
| 230 | * @param Filter\FilterSelect $filter |
||
| 231 | * @return void |
||
| 232 | */ |
||
| 233 | public function applyFilterSelect(Filter\FilterSelect $filter) |
||
| 234 | { |
||
| 235 | $this->data_source = $this->data_source->findBy([$this->prepareColumn($filter->getColumn()) => $filter->getValue()]); |
||
| 236 | } |
||
| 237 | |||
| 238 | |||
| 239 | /** |
||
| 240 | * Apply limit and offset on data |
||
| 241 | * @param int $offset |
||
| 242 | * @param int $limit |
||
| 243 | * @return static |
||
| 244 | */ |
||
| 245 | public function limit($offset, $limit) |
||
| 251 | |||
| 252 | |||
| 253 | /** |
||
| 254 | * Sort data |
||
| 255 | * @param Sorting $sorting |
||
| 256 | * @return static |
||
| 257 | */ |
||
| 258 | public function sort(Sorting $sorting) |
||
| 289 | |||
| 290 | /** |
||
| 291 | * Adjust column from DataGrid 'foreignKey.column' to Nextras 'this->foreignKey->column' |
||
| 292 | * @param string $column |
||
| 293 | * @return string |
||
| 294 | */ |
||
| 295 | private function prepareColumn($column) { |
||
| 296 | if (Strings::contains($column, '.')) { |
||
| 297 | return 'this->' . str_replace('.', '->', $column); |
||
| 298 | } |
||
| 301 | |||
| 302 | } |
||
| 303 |
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a given class or a super-class is assigned to a property that is type hinted more strictly.
Either this assignment is in error or an instanceof check should be added for that assignment.