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 JsonDefinition 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 JsonDefinition, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
20 | class JsonDefinition |
||
21 | { |
||
22 | /** |
||
23 | * Schema |
||
24 | * |
||
25 | * @var Schema\Definition |
||
26 | */ |
||
27 | private $def; |
||
28 | |||
29 | /** |
||
30 | * Composed namespace of this definition, must be explicitly set |
||
31 | * |
||
32 | * @var string |
||
33 | */ |
||
34 | private $namespace; |
||
35 | |||
36 | /** |
||
37 | * Constructor |
||
38 | * |
||
39 | * @param Schema\Definition $definition |
||
40 | */ |
||
41 | 64 | public function __construct(Schema\Definition $definition) |
|
45 | |||
46 | /** |
||
47 | * @return Schema\Definition |
||
48 | */ |
||
49 | public function getDef() |
||
53 | |||
54 | /** |
||
55 | * Returns this loads ID |
||
56 | * |
||
57 | * @return string ID |
||
58 | */ |
||
59 | 24 | public function getId() |
|
67 | |||
68 | /** |
||
69 | * Returns the description |
||
70 | * |
||
71 | * @return string Description |
||
72 | */ |
||
73 | 2 | public function getDescription() |
|
77 | |||
78 | /** |
||
79 | * Returns the title |
||
80 | * |
||
81 | * @return string Title |
||
82 | */ |
||
83 | public function getTitle() |
||
87 | |||
88 | /** |
||
89 | * Returns whether this definition requires the generation |
||
90 | * of a controller. normally yes, but sometimes not ;-) |
||
91 | * |
||
92 | * @return bool true if yes, false if no |
||
93 | */ |
||
94 | 4 | public function hasController() |
|
99 | |||
100 | /** |
||
101 | * This is a method that allows us to distinguish between a full json spec |
||
102 | * and a hash defined in a full spec which was divided into a seperate Document (thus, a SubDocument). |
||
103 | * To be aware what it is mainly serves for the generator to generate them as embedded documents, |
||
104 | * as subdocuments are always embedded. |
||
105 | * |
||
106 | * @return bool true if yes, false if not |
||
107 | */ |
||
108 | 6 | public function isSubDocument() |
|
112 | |||
113 | /** |
||
114 | * Gets the namespace |
||
115 | * |
||
116 | * @return string namespace |
||
117 | */ |
||
118 | 14 | public function getNamespace() |
|
122 | |||
123 | /** |
||
124 | * Sets the namespace |
||
125 | * |
||
126 | * @param string $namespace namespace |
||
127 | * |
||
128 | * @return void |
||
129 | */ |
||
130 | 10 | public function setNamespace($namespace) |
|
141 | |||
142 | /** |
||
143 | * Returns whether this service is read-only |
||
144 | * |
||
145 | * @return bool true if yes, false if not |
||
146 | */ |
||
147 | 4 | public function isReadOnlyService() |
|
155 | |||
156 | /** |
||
157 | * Returns whether this service is versioning |
||
158 | * |
||
159 | * @return bool true if yes, false if not |
||
160 | */ |
||
161 | public function isVersionedService() |
||
169 | |||
170 | /** |
||
171 | * Returns whether this service has fixtures |
||
172 | * |
||
173 | * @return bool true if yes, false if not |
||
174 | */ |
||
175 | 4 | public function hasFixtures() |
|
179 | |||
180 | /** |
||
181 | * Returns the fixtures or empty array if none |
||
182 | * |
||
183 | * @return array fixtures |
||
184 | */ |
||
185 | 4 | public function getFixtures() |
|
193 | |||
194 | /** |
||
195 | * Returns the order number at which order this fixture should be loaded. |
||
196 | * this is needed if we have relations/references between the fixtures.. |
||
197 | * |
||
198 | * @return int order |
||
199 | */ |
||
200 | 4 | public function getFixtureOrder() |
|
209 | |||
210 | /** |
||
211 | * Returns a router base path. false if default should be used. |
||
212 | * |
||
213 | * @return string router base, i.e. /bundle/name/ |
||
|
|||
214 | */ |
||
215 | 6 | public function getRouterBase() |
|
232 | |||
233 | /** |
||
234 | * Returns the Controller classname this services' controller shout inherit. |
||
235 | * Defaults to the RestController of the RestBundle of course. |
||
236 | * |
||
237 | * @return string base controller |
||
238 | */ |
||
239 | 4 | public function getBaseController() |
|
248 | |||
249 | /** |
||
250 | * Returns the parent service to use when adding the service xml |
||
251 | * |
||
252 | * Defaults to graviton.rest.controller |
||
253 | * |
||
254 | * @return string base controller |
||
255 | */ |
||
256 | public function getParentService() |
||
265 | |||
266 | /** |
||
267 | * Returns a specific field or null |
||
268 | * |
||
269 | * @param string $name Field name |
||
270 | * |
||
271 | * @return DefinitionElementInterface The field |
||
272 | */ |
||
273 | 30 | public function getField($name) |
|
278 | |||
279 | /** |
||
280 | * Returns the field definition |
||
281 | * |
||
282 | * @return DefinitionElementInterface[] Fields |
||
283 | */ |
||
284 | 32 | public function getFields() |
|
324 | |||
325 | /** |
||
326 | * @param Schema\Field $definition Raw field definition |
||
327 | * @param string $path Relative field path |
||
328 | * @return mixed |
||
329 | * @throws \InvalidArgumentException |
||
330 | */ |
||
331 | 30 | private function createFieldHierarchyRecursive(Schema\Field $definition, $path) |
|
346 | |||
347 | /** |
||
348 | * @param string $name Field name |
||
349 | * @param array $data Field data |
||
350 | * |
||
351 | * @return DefinitionElementInterface |
||
352 | */ |
||
353 | 30 | private function processFieldHierarchyRecursive($name, $data) |
|
381 | |||
382 | /** |
||
383 | * @param string $name Field name |
||
384 | * @param Schema\Field $definition Field |
||
385 | * |
||
386 | * @return DefinitionElementInterface |
||
387 | */ |
||
388 | 30 | private function processSimpleField($name, Schema\Field $definition) |
|
402 | |||
403 | /** |
||
404 | * Get target relations which are explictly defined |
||
405 | * |
||
406 | * @return Schema\Relation[] relations |
||
407 | */ |
||
408 | 32 | public function getRelations() |
|
421 | |||
422 | /** |
||
423 | * Get relation by field name |
||
424 | * |
||
425 | * @param string $field Field name |
||
426 | * @return Schema\Relation|null |
||
427 | */ |
||
428 | 24 | private function getRelation($field) |
|
433 | |||
434 | /** |
||
435 | * Provides the role set defined in the service section. |
||
436 | * |
||
437 | * @return array |
||
438 | */ |
||
439 | 2 | public function getRoles() |
|
447 | |||
448 | /** |
||
449 | * Provides the variations attribute from the service section |
||
450 | * |
||
451 | * @return array |
||
452 | */ |
||
453 | public function getVariations() |
||
461 | |||
462 | /** |
||
463 | * gets information about solr |
||
464 | * |
||
465 | * @return array|Schema\Solr |
||
466 | */ |
||
467 | public function getSolrFields() |
||
485 | |||
486 | /** |
||
487 | * Can record origin be modified |
||
488 | * |
||
489 | * @return bool |
||
490 | */ |
||
491 | public function isRecordOriginModifiable() |
||
500 | |||
501 | /** |
||
502 | * check if the RecordOriginModifiable flag is set |
||
503 | * |
||
504 | * @return bool |
||
505 | */ |
||
506 | public function isRecordOriginFlagSet() |
||
517 | |||
518 | /** |
||
519 | * @return string |
||
520 | */ |
||
521 | public function getServiceCollection() |
||
533 | |||
534 | /** |
||
535 | * @return string[] |
||
536 | */ |
||
537 | 2 | public function getIndexes() |
|
545 | |||
546 | /** |
||
547 | * @return string[] |
||
548 | */ |
||
549 | public function getSearchables() |
||
561 | |||
562 | /** |
||
563 | * @return string[] |
||
564 | */ |
||
565 | 2 | public function getTextIndexes() |
|
577 | |||
578 | /** |
||
579 | * Combine in one array the Search text indexes |
||
580 | * |
||
581 | * @return array |
||
582 | */ |
||
583 | public function getAllTextIndexes() |
||
590 | } |
||
591 |
This check compares the return type specified in the
@return
annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.