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 |
||
16 | abstract class Relation implements RelationAbstractorContract |
||
17 | { |
||
18 | use CheckRelationConfig; |
||
19 | use ConfigurationReader; |
||
20 | use ModelFields; |
||
21 | |||
22 | const DISPLAY_TYPE_TAB = 'tab'; |
||
23 | const DISPLAY_TYPE_INLINE = 'inline'; |
||
24 | |||
25 | protected $name; |
||
26 | protected $slug; |
||
27 | protected $presentation; |
||
28 | protected $type; |
||
29 | /** |
||
30 | * @var Model |
||
31 | */ |
||
32 | protected $relatedModel; |
||
33 | /** |
||
34 | * @var EloquentRelation |
||
35 | */ |
||
36 | protected $eloquentRelation; |
||
37 | /** |
||
38 | * @var FieldFactoryContract |
||
39 | */ |
||
40 | protected $fieldFactory; |
||
41 | protected $modelManager; |
||
42 | /** @var ModelAbstractor */ |
||
43 | protected $modelAbstractor; |
||
44 | /** |
||
45 | * @var array |
||
46 | */ |
||
47 | protected $config; |
||
48 | |||
49 | 35 | public function __construct(array $config, ModelManager $modelManager, Model $model, EloquentRelation $eloquentRelation, FieldFactoryContract $fieldFactory) |
|
78 | |||
79 | 1 | public function addSecondaryRelationFields(array $fields) |
|
90 | |||
91 | public function getName() |
||
95 | |||
96 | /** |
||
97 | * return null|string. |
||
98 | */ |
||
99 | public function getDisplay() |
||
105 | |||
106 | 4 | View Code Duplication | public function getPresentation() |
118 | |||
119 | 3 | public function getType() |
|
123 | |||
124 | public function getModelAbstractor() |
||
128 | |||
129 | /** |
||
130 | * @return Collection |
||
131 | */ |
||
132 | 4 | public function getSecondaryRelations() |
|
136 | |||
137 | /** |
||
138 | * @param Model $relatedModel |
||
139 | * |
||
140 | * @return Relation |
||
141 | */ |
||
142 | public function setRelatedModel(Model $relatedModel) |
||
148 | } |
||
149 |
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.