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 |
||
15 | abstract class RepositoryExecutor extends AbstractExecutor |
||
16 | { |
||
17 | use RepositoryUserSetterTrait; |
||
18 | use IgnorableStepExecutorTrait; |
||
19 | |||
20 | /** |
||
21 | * Constant defining the default language code (used if not specified by the migration or on the command line) |
||
22 | */ |
||
23 | const DEFAULT_LANGUAGE_CODE = 'eng-GB'; |
||
24 | |||
25 | /** |
||
26 | * The default Admin user Id, used when no Admin user is specified |
||
27 | */ |
||
28 | const ADMIN_USER_ID = 14; |
||
29 | |||
30 | /** Used if not specified by the migration */ |
||
31 | const USER_CONTENT_TYPE = 'user'; |
||
32 | |||
33 | /** |
||
34 | * @var array $dsl The parsed DSL instruction array |
||
35 | */ |
||
36 | //protected $dsl; |
||
37 | |||
38 | /** @var array $context The context (configuration) for the execution of the current step */ |
||
39 | //protected $context; |
||
40 | |||
41 | /** |
||
42 | * The eZ Publish 5 API repository. |
||
43 | * |
||
44 | * @var \eZ\Publish\API\Repository\Repository |
||
45 | */ |
||
46 | protected $repository; |
||
47 | |||
48 | protected $configResolver; |
||
49 | |||
50 | /** @var ReferenceResolverBagInterface $referenceResolver */ |
||
51 | protected $referenceResolver; |
||
52 | |||
53 | // to redefine in subclasses if they don't support all methods, or if they support more... |
||
54 | protected $supportedActions = array( |
||
55 | 'create', 'update', 'delete' |
||
56 | ); |
||
57 | |||
58 | public function setRepository(Repository $repository) |
||
62 | |||
63 | public function setConfigResolver(ConfigResolverInterface $configResolver) |
||
67 | |||
68 | public function setReferenceResolver(ReferenceResolverBagInterface $referenceResolver) |
||
72 | |||
73 | public function execute(MigrationStep $step) |
||
110 | |||
111 | /** |
||
112 | * Method that each executor (subclass) has to implement. |
||
113 | * |
||
114 | * It is used to set references based on the DSL instructions executed in the current step, for later steps to reuse. |
||
115 | * |
||
116 | * @throws \InvalidArgumentException when trying to set a reference to an unsupported attribute. |
||
117 | * @param $object |
||
118 | * @return boolean |
||
119 | */ |
||
120 | abstract protected function setReferences($object, $step); |
||
121 | |||
122 | /** |
||
123 | * @param MigrationStep $step |
||
124 | * @return string |
||
125 | */ |
||
126 | protected function getLanguageCode($step) |
||
130 | |||
131 | /** |
||
132 | * @param array|null $context |
||
133 | * @return string |
||
134 | */ |
||
135 | protected function getLanguageCodeFromContext($context) |
||
148 | |||
149 | /** |
||
150 | * @param MigrationStep $step |
||
151 | * @return string |
||
152 | */ |
||
153 | protected function getUserContentType($step) |
||
157 | |||
158 | /** |
||
159 | * @param array $context |
||
160 | * @return string |
||
161 | */ |
||
162 | protected function getUserContentTypeFromContext($context) |
||
166 | |||
167 | /** |
||
168 | * @param array $context we have to return FALSE if that is set as adminUserLogin, whereas if NULL is set, we return the default admin |
||
169 | * @return int|string|false |
||
170 | */ |
||
171 | protected function getAdminUserIdentifierFromContext($context) |
||
179 | |||
180 | /** |
||
181 | * @param mixed $entity |
||
182 | * @param array $referencesDefinition |
||
183 | * @return array the same as $referencesDefinition, with the references already treated having been removed |
||
184 | */ |
||
185 | protected function setReferencesCommon($entity, $referencesDefinition) |
||
208 | |||
209 | /** |
||
210 | * @param AbstractCollection|mixed $entity |
||
211 | * @param array $referencesDefinition |
||
212 | * @return AbstractCollection|mixed |
||
213 | */ |
||
214 | protected function insureSingleEntity($entity, $referencesDefinition) |
||
234 | |||
235 | protected function getSelfName() |
||
243 | |||
244 | protected function getCollectionName($collection) |
||
252 | |||
253 | /** |
||
254 | * Courtesy code to avoid reimplementing it in every subclass |
||
255 | * @todo will be moved into the reference resolver classes |
||
256 | */ |
||
257 | View Code Duplication | protected function resolveReferencesRecursively($match) |
|
268 | } |
||
269 |
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.