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 |
||
18 | class JsonDefinition |
||
19 | { |
||
20 | /** |
||
21 | * Schema |
||
22 | * |
||
23 | * @var Schema\Definition |
||
24 | */ |
||
25 | private $def; |
||
26 | |||
27 | /** |
||
28 | * Composed namespace of this definition, must be explicitly set |
||
29 | * |
||
30 | * @var string |
||
31 | */ |
||
32 | private $namespace; |
||
33 | |||
34 | /** |
||
35 | * Constructor |
||
36 | * |
||
37 | * @param Schema\Definition $definition |
||
38 | */ |
||
39 | public function __construct(Schema\Definition $definition) |
||
43 | |||
44 | /** |
||
45 | * @return Schema\Definition |
||
46 | */ |
||
47 | public function getDef() |
||
51 | |||
52 | /** |
||
53 | * Returns this loads ID |
||
54 | * |
||
55 | * @return string ID |
||
56 | */ |
||
57 | public function getId() |
||
65 | |||
66 | /** |
||
67 | * Returns the description |
||
68 | * |
||
69 | * @return string Description |
||
70 | */ |
||
71 | public function getDescription() |
||
75 | |||
76 | /** |
||
77 | * Returns the title |
||
78 | * |
||
79 | * @return string Title |
||
80 | */ |
||
81 | public function getTitle() |
||
85 | |||
86 | /** |
||
87 | * Returns whether this definition requires the generation |
||
88 | * of a controller. normally yes, but sometimes not ;-) |
||
89 | * |
||
90 | * @return bool true if yes, false if no |
||
91 | */ |
||
92 | public function hasController() |
||
97 | |||
98 | /** |
||
99 | * This is a method that allows us to distinguish between a full json spec |
||
100 | * and a hash defined in a full spec which was divided into a seperate Document (thus, a SubDocument). |
||
101 | * To be aware what it is mainly serves for the generator to generate them as embedded documents, |
||
102 | * as subdocuments are always embedded. |
||
103 | * |
||
104 | * @return bool true if yes, false if not |
||
105 | */ |
||
106 | public function isSubDocument() |
||
110 | |||
111 | /** |
||
112 | * Gets the namespace |
||
113 | * |
||
114 | * @return string namespace |
||
115 | */ |
||
116 | public function getNamespace() |
||
120 | |||
121 | /** |
||
122 | * Sets the namespace |
||
123 | * |
||
124 | * @param string $namespace namespace |
||
125 | * |
||
126 | * @return void |
||
127 | */ |
||
128 | public function setNamespace($namespace) |
||
139 | |||
140 | /** |
||
141 | * Returns whether this service is read-only |
||
142 | * |
||
143 | * @return bool true if yes, false if not |
||
144 | */ |
||
145 | public function isReadOnlyService() |
||
153 | |||
154 | /** |
||
155 | * Returns whether this service has fixtures |
||
156 | * |
||
157 | * @return bool true if yes, false if not |
||
158 | */ |
||
159 | public function hasFixtures() |
||
163 | |||
164 | /** |
||
165 | * Returns the fixtures or empty array if none |
||
166 | * |
||
167 | * @return array fixtures |
||
168 | */ |
||
169 | public function getFixtures() |
||
177 | |||
178 | /** |
||
179 | * Returns the order number at which order this fixture should be loaded. |
||
180 | * this is needed if we have relations/references between the fixtures.. |
||
181 | * |
||
182 | * @return int order |
||
183 | */ |
||
184 | public function getFixtureOrder() |
||
193 | |||
194 | /** |
||
195 | * Returns a router base path. false if default should be used. |
||
196 | * |
||
197 | * @return string router base, i.e. /bundle/name/ |
||
|
|||
198 | */ |
||
199 | public function getRouterBase() |
||
216 | |||
217 | /** |
||
218 | * Returns the Controller classname this services' controller shout inherit. |
||
219 | * Defaults to the RestController of the RestBundle of course. |
||
220 | * |
||
221 | * @return string base controller |
||
222 | */ |
||
223 | public function getBaseController() |
||
232 | |||
233 | /** |
||
234 | * Returns the parent service to use when adding the service xml |
||
235 | * |
||
236 | * Defaults to graviton.rest.controller |
||
237 | * |
||
238 | * @return string base controller |
||
239 | */ |
||
240 | public function getParentService() |
||
249 | |||
250 | /** |
||
251 | * Returns a specific field or null |
||
252 | * |
||
253 | * @param string $name Field name |
||
254 | * |
||
255 | * @return DefinitionElementInterface The field |
||
256 | */ |
||
257 | public function getField($name) |
||
262 | |||
263 | /** |
||
264 | * Returns the field definition |
||
265 | * |
||
266 | * @return DefinitionElementInterface[] Fields |
||
267 | */ |
||
268 | public function getFields() |
||
285 | |||
286 | /** |
||
287 | * @param Schema\Field $definition Raw field definition |
||
288 | * @param string $path Relative field path |
||
289 | * @return mixed |
||
290 | * @throws \InvalidArgumentException |
||
291 | */ |
||
292 | private function createFieldHierarchyRecursive(Schema\Field $definition, $path) |
||
307 | |||
308 | /** |
||
309 | * @param string $name Field name |
||
310 | * @param array $data Field data |
||
311 | * |
||
312 | * @return DefinitionElementInterface |
||
313 | */ |
||
314 | private function processFieldHierarchyRecursive($name, $data) |
||
342 | |||
343 | /** |
||
344 | * @param string $name Field name |
||
345 | * @param Schema\Field $definition Field |
||
346 | * |
||
347 | * @return DefinitionElementInterface |
||
348 | */ |
||
349 | private function processSimpleField($name, Schema\Field $definition) |
||
363 | |||
364 | /** |
||
365 | * Get target relations which are explictly defined |
||
366 | * |
||
367 | * @return Schema\Relation[] relations |
||
368 | */ |
||
369 | public function getRelations() |
||
382 | |||
383 | /** |
||
384 | * Get relation by field name |
||
385 | * |
||
386 | * @param string $field Field name |
||
387 | * @return Schema\Relation|null |
||
388 | */ |
||
389 | private function getRelation($field) |
||
394 | |||
395 | /** |
||
396 | * Provides the role set defined in the service section. |
||
397 | * |
||
398 | * @return array |
||
399 | */ |
||
400 | public function getRoles() |
||
408 | |||
409 | /** |
||
410 | * Can record origin be modified |
||
411 | * |
||
412 | * @return bool |
||
413 | */ |
||
414 | public function isRecordOriginModifiable() |
||
423 | |||
424 | /** |
||
425 | * check if the RecordOriginModifiable flag is set |
||
426 | * |
||
427 | * @return bool |
||
428 | */ |
||
429 | public function isRecordOriginFlagSet() |
||
440 | |||
441 | /** |
||
442 | * @return string |
||
443 | */ |
||
444 | public function getServiceCollection() |
||
456 | |||
457 | /** |
||
458 | * @return string[] |
||
459 | */ |
||
460 | public function getIndexes() |
||
468 | |||
469 | /** |
||
470 | * @return string[] |
||
471 | */ |
||
472 | public function getSearchables() |
||
484 | } |
||
485 |
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.