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 |
||
20 | class AbstractManager |
||
21 | { |
||
22 | use TranslatorAwareTrait; |
||
23 | |||
24 | /** |
||
25 | * Store the factory instance for the current class. |
||
26 | * |
||
27 | * @var FactoryInterface |
||
28 | */ |
||
29 | protected $modelFactory; |
||
30 | |||
31 | /** |
||
32 | * Store the collection loader for the current class. |
||
33 | * |
||
34 | * @var CollectionLoader |
||
35 | */ |
||
36 | protected $collectionLoader; |
||
37 | |||
38 | /** |
||
39 | * @var object $adminConfig The admin config object model. |
||
40 | */ |
||
41 | protected $adminConfig; |
||
42 | |||
43 | /** |
||
44 | * NewsManager constructor. |
||
45 | * @param array $data The Data. |
||
46 | * @throws Exception When $data index is not set. |
||
47 | */ |
||
48 | public function __construct(array $data) |
||
74 | |||
75 | /** |
||
76 | * Set an object model factory. |
||
77 | * |
||
78 | * @param FactoryInterface $factory The model factory, to create objects. |
||
79 | * @return self |
||
80 | */ |
||
81 | protected function setModelFactory(FactoryInterface $factory) |
||
87 | |||
88 | /** |
||
89 | * Retrieve the object model factory. |
||
90 | * |
||
91 | * @throws RuntimeException If the model factory was not previously set. |
||
92 | * @return FactoryInterface |
||
93 | */ |
||
94 | View Code Duplication | public function modelFactory() |
|
104 | |||
105 | /** |
||
106 | * Set a model collection loader. |
||
107 | * |
||
108 | * @param CollectionLoader $loader The collection loader. |
||
109 | * @return self |
||
110 | */ |
||
111 | protected function setCollectionLoader(CollectionLoader $loader) |
||
117 | |||
118 | /** |
||
119 | * Retrieve the model collection loader. |
||
120 | * |
||
121 | * @throws RuntimeException If the collection loader was not previously set. |
||
122 | * @return CollectionLoader |
||
123 | */ |
||
124 | View Code Duplication | public function collectionLoader() |
|
134 | |||
135 | /** |
||
136 | * @param mixed $adminConfig The admin configuration. |
||
137 | * @return self |
||
138 | */ |
||
139 | public function setAdminConfig($adminConfig) |
||
145 | |||
146 | /** |
||
147 | * @return mixed |
||
148 | */ |
||
149 | public function adminConfig() |
||
153 | } |
||
154 |
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.