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() |
|
311 | |||
312 | /** |
||
313 | * @param Schema\Field $definition Raw field definition |
||
314 | * @param string $path Relative field path |
||
315 | * @return mixed |
||
316 | * @throws \InvalidArgumentException |
||
317 | */ |
||
318 | 30 | private function createFieldHierarchyRecursive(Schema\Field $definition, $path) |
|
333 | |||
334 | /** |
||
335 | * @param string $name Field name |
||
336 | * @param array $data Field data |
||
337 | * |
||
338 | * @return DefinitionElementInterface |
||
339 | */ |
||
340 | 30 | private function processFieldHierarchyRecursive($name, $data) |
|
368 | |||
369 | /** |
||
370 | * @param string $name Field name |
||
371 | * @param Schema\Field $definition Field |
||
372 | * |
||
373 | * @return DefinitionElementInterface |
||
374 | */ |
||
375 | 30 | private function processSimpleField($name, Schema\Field $definition) |
|
389 | |||
390 | /** |
||
391 | * Get target relations which are explictly defined |
||
392 | * |
||
393 | * @return Schema\Relation[] relations |
||
394 | */ |
||
395 | 32 | public function getRelations() |
|
408 | |||
409 | /** |
||
410 | * Get relation by field name |
||
411 | * |
||
412 | * @param string $field Field name |
||
413 | * @return Schema\Relation|null |
||
414 | */ |
||
415 | 24 | private function getRelation($field) |
|
420 | |||
421 | /** |
||
422 | * Provides the role set defined in the service section. |
||
423 | * |
||
424 | * @return array |
||
425 | */ |
||
426 | 2 | public function getRoles() |
|
434 | |||
435 | /** |
||
436 | * Can record origin be modified |
||
437 | * |
||
438 | * @return bool |
||
439 | */ |
||
440 | public function isRecordOriginModifiable() |
||
449 | |||
450 | /** |
||
451 | * check if the RecordOriginModifiable flag is set |
||
452 | * |
||
453 | * @return bool |
||
454 | */ |
||
455 | public function isRecordOriginFlagSet() |
||
466 | |||
467 | /** |
||
468 | * @return string |
||
469 | */ |
||
470 | public function getServiceCollection() |
||
482 | |||
483 | /** |
||
484 | * @return string[] |
||
485 | */ |
||
486 | 2 | public function getIndexes() |
|
494 | |||
495 | /** |
||
496 | * @return string[] |
||
497 | */ |
||
498 | public function getSearchables() |
||
510 | |||
511 | /** |
||
512 | * @return string[] |
||
513 | */ |
||
514 | 2 | public function getTextIndexes() |
|
526 | |||
527 | /** |
||
528 | * Combine in one array the Search text indexes |
||
529 | * |
||
530 | * @return array |
||
531 | */ |
||
532 | public function getAllTextIndexes() |
||
539 | } |
||
540 |
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.