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 RepositoryExecutor 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 RepositoryExecutor, and based on these observations, apply Extract Interface, too.
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 get values for 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 a sinle element to extract reference values from |
||
118 | * @param array $referencesDefinitionsthe definitions of the references to extract |
||
|
|||
119 | * @return array key: the reference name (taken from $referencesDefinitions[n]['identifier'], value: the ref. value |
||
120 | */ |
||
121 | abstract protected function getReferencesValues($object, $referencesDefinitions); |
||
122 | |||
123 | /** |
||
124 | * @param MigrationStep $step |
||
125 | * @return string |
||
126 | */ |
||
127 | protected function getLanguageCode($step) |
||
131 | |||
132 | /** |
||
133 | * @param array|null $context |
||
134 | * @return string |
||
135 | */ |
||
136 | protected function getLanguageCodeFromContext($context) |
||
149 | |||
150 | /** |
||
151 | * @param MigrationStep $step |
||
152 | * @return string |
||
153 | */ |
||
154 | protected function getUserContentType($step) |
||
158 | |||
159 | /** |
||
160 | * @param array $context |
||
161 | * @return string |
||
162 | */ |
||
163 | protected function getUserContentTypeFromContext($context) |
||
167 | |||
168 | /** |
||
169 | * @param array $context we have to return FALSE if that is set as adminUserLogin, whereas if NULL is set, we return the default admin |
||
170 | * @return int|string|false |
||
171 | */ |
||
172 | protected function getAdminUserIdentifierFromContext($context) |
||
180 | |||
181 | /** |
||
182 | * Sets references to certain content attributes. |
||
183 | * |
||
184 | * @param \Object|AbstractCollectionCollection $item |
||
185 | * @throws \InvalidArgumentException When trying to set a reference to an unsupported attribute |
||
186 | * @return boolean |
||
187 | * |
||
188 | * @todo add support for other attributes... ? |
||
189 | */ |
||
190 | protected function setReferences($item, $step) |
||
239 | |||
240 | /** |
||
241 | * @param mixed $entity |
||
242 | * @param array $referencesDefinition |
||
243 | * @return array the same as $referencesDefinition, with the references already treated having been removed |
||
244 | */ |
||
245 | protected function setReferencesCommon($entity, $referencesDefinition) |
||
268 | |||
269 | /** |
||
270 | * Verifies compatibility between the definition of the refences to be set and the data set to extarct them from, |
||
271 | * and returns a single entity |
||
272 | * |
||
273 | * @param AbstractCollection|mixed $entity |
||
274 | * @param array $referencesDefinition |
||
275 | * @return AbstractCollection|mixed |
||
276 | */ |
||
277 | protected function insureSingleEntity($entity, $referencesDefinition) |
||
287 | |||
288 | /** |
||
289 | * Verifies compatibility between the definition of the refences to be set and the data set to extract them from. |
||
290 | * Nb: for multivalued refs, we assume that the users always expect at least one value |
||
291 | * @param AbstractCollection|mixed $entity |
||
292 | * @param array $referencesDefinition |
||
293 | * @return void throws when incompatibiliy is found |
||
294 | * @todo should we allow to be passed in plain arrays as well ? |
||
295 | */ |
||
296 | protected function insureEntityCountCompatibilty($entity, $referencesDefinition) |
||
311 | |||
312 | protected function areReferencesMultivalued($referencesDefinition) |
||
316 | |||
317 | protected function getSelfName() |
||
325 | |||
326 | protected function getCollectionName($collection) |
||
334 | |||
335 | /** |
||
336 | * Courtesy code to avoid reimplementing it in every subclass |
||
337 | * @todo will be moved into the reference resolver classes |
||
338 | */ |
||
339 | View Code Duplication | protected function resolveReferencesRecursively($match) |
|
350 | } |
||
351 |
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function. It has, however, found a similar but not annotated parameter which might be a good fit.
Consider the following example. The parameter
$ireland
is not defined by the methodfinale(...)
.The most likely cause is that the parameter was changed, but the annotation was not.