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:
Complex classes like Model often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Model, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 23 | class Model implements ModelAbstractorContract |
||
| 24 | { |
||
| 25 | use ConfigurationReader; |
||
| 26 | use ModelFields; |
||
| 27 | use HandleFiles; |
||
| 28 | |||
| 29 | protected $dbal; |
||
| 30 | protected $relationFactory; |
||
| 31 | protected $fieldFactory; |
||
| 32 | protected $generator; |
||
| 33 | |||
| 34 | protected $model; |
||
| 35 | protected $config; |
||
| 36 | |||
| 37 | protected $slug; |
||
| 38 | protected $name; |
||
| 39 | protected $instance; |
||
| 40 | |||
| 41 | 17 | public function __construct( |
|
| 42 | $config, |
||
| 43 | AbstractionLayer $dbal, |
||
| 44 | RelationFactoryContract $relationFactory, |
||
| 45 | FieldFactoryContract $fieldFactory, |
||
| 46 | FormGenerator $generator |
||
| 47 | ) { |
||
| 48 | 17 | if (is_array($config)) { |
|
| 49 | 17 | $this->model = $config['model']; |
|
| 50 | 17 | $this->config = $config; |
|
| 51 | 17 | } else { |
|
| 52 | $this->model = $config; |
||
| 53 | $this->config = []; |
||
| 54 | } |
||
| 55 | |||
| 56 | 17 | $this->dbal = $dbal; |
|
| 57 | 17 | $this->relationFactory = $relationFactory; |
|
| 58 | 17 | $this->fieldFactory = $fieldFactory; |
|
| 59 | 17 | $this->generator = $generator; |
|
| 60 | 17 | } |
|
| 61 | |||
| 62 | 3 | public function setSlug($slug) |
|
| 68 | |||
| 69 | 2 | public function setName($name) |
|
| 75 | |||
| 76 | 5 | public function setInstance($instance) |
|
| 82 | |||
| 83 | public function getSlug() |
||
| 87 | |||
| 88 | public function getName() |
||
| 92 | |||
| 93 | 1 | public function getModel() |
|
| 97 | |||
| 98 | public function getInstance() |
||
| 102 | |||
| 103 | public function isSoftDeletes() |
||
| 107 | |||
| 108 | 8 | public function getColumns($action, $withForeignKeys = false) |
|
| 109 | { |
||
| 110 | 8 | $tableColumns = $this->dbal->getTableColumns(); |
|
| 111 | |||
| 112 | 8 | $filteredColumns = []; |
|
| 113 | 8 | foreach ($tableColumns as $name => $column) { |
|
| 114 | 8 | $filteredColumns[str_replace('`', '', $name)] = $column; |
|
| 115 | 8 | } |
|
| 116 | 8 | $tableColumns = $filteredColumns; |
|
| 117 | |||
| 118 | |||
| 119 | 8 | $foreignKeysName = []; |
|
| 120 | 8 | if ($withForeignKeys === false) { |
|
| 121 | 8 | $foreignKeys = $this->dbal->getTableForeignKeys(); |
|
| 122 | |||
| 123 | 8 | foreach ($foreignKeys as $foreignKey) { |
|
| 124 | foreach ($foreignKey->getColumns() as $columnName) { |
||
| 125 | $foreignKeysName[] = $columnName; |
||
| 126 | } |
||
| 127 | 8 | } |
|
| 128 | 8 | } |
|
| 129 | |||
| 130 | 8 | $customDisplayedColumns = $this->getConfigValue($action, 'display'); |
|
| 131 | 8 | $customHiddenColumns = $this->getConfigValue($action, 'hide') ? : []; |
|
| 132 | |||
| 133 | 8 | $columns = array(); |
|
| 134 | 8 | if (! empty($customDisplayedColumns) && is_array($customDisplayedColumns)) { |
|
| 135 | 8 | foreach ($customDisplayedColumns as $customColumn) { |
|
| 136 | 8 | if (! array_key_exists($customColumn, $tableColumns)) { |
|
| 137 | throw new AbstractorException("Column " . $customColumn . " does not exist on " . $this->getModel()); |
||
| 138 | } |
||
| 139 | |||
| 140 | 8 | $columns[$customColumn] = $tableColumns[$customColumn]; |
|
| 141 | 8 | } |
|
| 142 | 8 | } else { |
|
| 143 | foreach ($tableColumns as $name => $column) { |
||
| 144 | if (in_array($name, $customHiddenColumns)) { |
||
| 145 | continue; |
||
| 146 | } |
||
| 147 | |||
| 148 | if (in_array($name, $foreignKeysName)) { |
||
| 149 | continue; |
||
| 150 | } |
||
| 151 | |||
| 152 | $columns[$name] = $column; |
||
| 153 | } |
||
| 154 | } |
||
| 155 | |||
| 156 | 8 | return $columns; |
|
| 157 | } |
||
| 158 | |||
| 159 | /** |
||
| 160 | * @return \Illuminate\Support\Collection |
||
| 161 | */ |
||
| 162 | 4 | public function getRelations() |
|
| 163 | { |
||
| 164 | 4 | $configRelations = $this->getConfigValue('relations'); |
|
| 165 | |||
| 166 | 4 | $relations = collect(); |
|
| 167 | |||
| 168 | 4 | if (! empty($configRelations)) { |
|
| 169 | 4 | foreach ($configRelations as $relationName => $configRelation) { |
|
| 170 | 4 | if (is_int($relationName)) { |
|
| 171 | $relationName = $configRelation; |
||
| 172 | } |
||
| 173 | |||
| 174 | 4 | $config = []; |
|
| 175 | 4 | if ($configRelation !== $relationName) { |
|
| 176 | 4 | if (! is_array($configRelation)) { |
|
| 177 | $config['type'] = $configRelation; |
||
| 178 | } else { |
||
| 179 | 4 | $config = $configRelation; |
|
| 180 | } |
||
| 181 | 4 | } |
|
| 182 | |||
| 183 | /** @var Relation $relation */ |
||
| 184 | 4 | $relation = $this->relationFactory->setModel($this->instance) |
|
| 185 | 4 | ->setConfig($config) |
|
| 186 | 4 | ->get($relationName); |
|
| 187 | |||
| 188 | 4 | $secondaryRelations = $relation->getSecondaryRelations(); |
|
| 189 | |||
| 190 | |||
| 191 | 4 | if (! $secondaryRelations->isEmpty()) { |
|
| 192 | 1 | $relations->put($relationName, |
|
| 193 | 1 | collect(['relation' => $relation, 'secondaryRelations' => $secondaryRelations])); |
|
| 194 | 1 | } else { |
|
| 195 | 3 | $relations->put($relationName, $relation); |
|
| 196 | } |
||
| 197 | |||
| 198 | 4 | } |
|
| 199 | 4 | } |
|
| 200 | |||
| 201 | 4 | return $relations; |
|
| 202 | } |
||
| 203 | |||
| 204 | /** |
||
| 205 | * @param string|null $arrayKey |
||
| 206 | * @return array |
||
| 207 | * @throws AbstractorException |
||
| 208 | */ |
||
| 209 | 2 | View Code Duplication | public function getListFields($arrayKey = 'main') |
|
|
|||
| 210 | { |
||
| 211 | 2 | $columns = $this->getColumns('list'); |
|
| 212 | |||
| 213 | 2 | $fieldsPresentation = $this->getConfigValue('fields_presentation') ? : []; |
|
| 214 | |||
| 215 | 2 | $fields = array(); |
|
| 216 | 2 | foreach ($columns as $name => $column) { |
|
| 217 | 2 | $presentation = null; |
|
| 218 | 2 | if (array_key_exists($name, $fieldsPresentation)) { |
|
| 219 | 2 | $presentation = $fieldsPresentation[$name]; |
|
| 220 | 2 | } |
|
| 221 | |||
| 222 | $config = [ |
||
| 223 | 2 | 'name' => $name, |
|
| 224 | 2 | 'presentation' => $presentation, |
|
| 225 | 2 | 'form_type' => null, |
|
| 226 | 2 | 'validation' => null, |
|
| 227 | 'functions' => null |
||
| 228 | 2 | ]; |
|
| 229 | |||
| 230 | 2 | $fields[$arrayKey][] = $this->fieldFactory |
|
| 231 | 2 | ->setColumn($column) |
|
| 232 | 2 | ->setConfig($config) |
|
| 233 | 2 | ->get(); |
|
| 234 | 2 | } |
|
| 235 | |||
| 236 | 2 | return $fields; |
|
| 237 | } |
||
| 238 | |||
| 239 | /** |
||
| 240 | * @param string|null $arrayKey |
||
| 241 | * @return array |
||
| 242 | * @throws AbstractorException |
||
| 243 | */ |
||
| 244 | 2 | View Code Duplication | public function getDetailFields($arrayKey = 'main') |
| 245 | { |
||
| 246 | 2 | $columns = $this->getColumns('detail'); |
|
| 247 | |||
| 248 | 2 | $fieldsPresentation = $this->getConfigValue('fields_presentation') ? : []; |
|
| 249 | |||
| 250 | 2 | $fields = array(); |
|
| 251 | 2 | foreach ($columns as $name => $column) { |
|
| 252 | 2 | $presentation = null; |
|
| 253 | 2 | if (array_key_exists($name, $fieldsPresentation)) { |
|
| 254 | 2 | $presentation = $fieldsPresentation[$name]; |
|
| 255 | 2 | } |
|
| 256 | |||
| 257 | $config = [ |
||
| 258 | 2 | 'name' => $name, |
|
| 259 | 2 | 'presentation' => $presentation, |
|
| 260 | 2 | 'form_type' => null, |
|
| 261 | 2 | 'validation' => null, |
|
| 262 | 'functions' => null |
||
| 263 | 2 | ]; |
|
| 264 | |||
| 265 | 2 | $fields[$arrayKey][] = $this->fieldFactory |
|
| 266 | 2 | ->setColumn($column) |
|
| 267 | 2 | ->setConfig($config) |
|
| 268 | 2 | ->get(); |
|
| 269 | 2 | } |
|
| 270 | |||
| 271 | 2 | return $fields; |
|
| 272 | } |
||
| 273 | |||
| 274 | /** |
||
| 275 | * @param bool|null $withForeignKeys |
||
| 276 | * @param string|null $arrayKey |
||
| 277 | * @return array |
||
| 278 | * @throws AbstractorException |
||
| 279 | */ |
||
| 280 | 4 | public function getEditFields($withForeignKeys = false, $arrayKey = 'main') |
|
| 281 | { |
||
| 282 | 4 | $columns = $this->getColumns('edit', $withForeignKeys); |
|
| 283 | |||
| 284 | 4 | $this->readConfig('edit'); |
|
| 285 | |||
| 286 | 4 | $fields = array(); |
|
| 287 | 4 | foreach ($columns as $name => $column) { |
|
| 288 | 4 | if (! in_array($name, $this->getReadOnlyColumns())) { |
|
| 289 | 4 | $presentation = null; |
|
| 290 | 4 | if (array_key_exists($name, $this->fieldsPresentation)) { |
|
| 291 | $presentation = $this->fieldsPresentation[$name]; |
||
| 292 | } |
||
| 293 | |||
| 294 | $config = [ |
||
| 295 | 4 | 'name' => $name, |
|
| 296 | 4 | 'presentation' => $presentation, |
|
| 297 | 4 | 'form_type' => null, |
|
| 298 | 4 | 'validation' => null, |
|
| 299 | 'functions' => null |
||
| 300 | 4 | ]; |
|
| 301 | |||
| 302 | 4 | $config = $this->setConfig($config, $name); |
|
| 303 | |||
| 304 | 4 | $field = $this->fieldFactory |
|
| 305 | 4 | ->setColumn($column) |
|
| 306 | 4 | ->setConfig($config) |
|
| 307 | 4 | ->get(); |
|
| 308 | |||
| 309 | 4 | if (! empty($this->instance) && ! empty($this->instance->getAttribute($name))) { |
|
| 310 | $field->setValue($this->instance->getAttribute($name)); |
||
| 311 | } |
||
| 312 | |||
| 313 | 4 | $fields[$arrayKey][$name] = $field; |
|
| 314 | |||
| 315 | 4 | View Code Duplication | if (! empty($config['form_type']) && $config['form_type'] === 'file') { |
| 316 | 4 | $field = $this->fieldFactory |
|
| 317 | 4 | ->setColumn($column) |
|
| 318 | 4 | ->setConfig([ |
|
| 319 | 4 | 'name' => $name . '__delete', |
|
| 320 | 4 | 'presentation' => null, |
|
| 321 | 4 | 'form_type' => 'checkbox', |
|
| 322 | 4 | 'no_validate' => true, |
|
| 323 | 'functions' => null |
||
| 324 | 4 | ]) |
|
| 325 | 4 | ->get(); |
|
| 326 | 4 | $fields[$arrayKey][$name . '__delete'] = $field; |
|
| 327 | 4 | } |
|
| 328 | 4 | } |
|
| 329 | 4 | } |
|
| 330 | |||
| 331 | 4 | return $fields; |
|
| 332 | } |
||
| 333 | |||
| 334 | 4 | protected function getReadOnlyColumns() |
|
| 342 | |||
| 343 | /** |
||
| 344 | * @param string $action |
||
| 345 | * @return ElementInterface |
||
| 346 | */ |
||
| 347 | 2 | public function getForm($action) |
|
| 354 | |||
| 355 | /** |
||
| 356 | * @param array $requestForm |
||
| 357 | * @return mixed |
||
| 358 | */ |
||
| 359 | 1 | public function persist(Request $request) |
|
| 360 | { |
||
| 433 | |||
| 434 | /** |
||
| 435 | * @return array |
||
| 436 | */ |
||
| 437 | 1 | public function getValidationRules() |
|
| 441 | } |
||
| 442 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.