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 |
||
| 19 | class JsonDefinition |
||
| 20 | { |
||
| 21 | /** |
||
| 22 | * Schema |
||
| 23 | * |
||
| 24 | * @var Schema\Definition |
||
| 25 | */ |
||
| 26 | private $def; |
||
| 27 | |||
| 28 | /** |
||
| 29 | * Composed namespace of this definition, must be explicitly set |
||
| 30 | * |
||
| 31 | * @var string |
||
| 32 | */ |
||
| 33 | private $namespace; |
||
| 34 | |||
| 35 | /** |
||
| 36 | * Constructor |
||
| 37 | * |
||
| 38 | * @param Schema\Definition $definition |
||
| 39 | */ |
||
| 40 | 64 | public function __construct(Schema\Definition $definition) |
|
| 44 | |||
| 45 | /** |
||
| 46 | * @return Schema\Definition |
||
| 47 | */ |
||
| 48 | public function getDef() |
||
| 52 | |||
| 53 | /** |
||
| 54 | * Returns this loads ID |
||
| 55 | * |
||
| 56 | * @return string ID |
||
| 57 | */ |
||
| 58 | 24 | public function getId() |
|
| 66 | |||
| 67 | /** |
||
| 68 | * Returns the description |
||
| 69 | * |
||
| 70 | * @return string Description |
||
| 71 | */ |
||
| 72 | 2 | public function getDescription() |
|
| 76 | |||
| 77 | /** |
||
| 78 | * Returns the title |
||
| 79 | * |
||
| 80 | * @return string Title |
||
| 81 | */ |
||
| 82 | public function getTitle() |
||
| 86 | |||
| 87 | /** |
||
| 88 | * Returns whether this definition requires the generation |
||
| 89 | * of a controller. normally yes, but sometimes not ;-) |
||
| 90 | * |
||
| 91 | * @return bool true if yes, false if no |
||
| 92 | */ |
||
| 93 | 4 | public function hasController() |
|
| 98 | |||
| 99 | /** |
||
| 100 | * This is a method that allows us to distinguish between a full json spec |
||
| 101 | * and a hash defined in a full spec which was divided into a seperate Document (thus, a SubDocument). |
||
| 102 | * To be aware what it is mainly serves for the generator to generate them as embedded documents, |
||
| 103 | * as subdocuments are always embedded. |
||
| 104 | * |
||
| 105 | * @return bool true if yes, false if not |
||
| 106 | */ |
||
| 107 | 6 | public function isSubDocument() |
|
| 111 | |||
| 112 | /** |
||
| 113 | * Gets the namespace |
||
| 114 | * |
||
| 115 | * @return string namespace |
||
| 116 | */ |
||
| 117 | 14 | public function getNamespace() |
|
| 121 | |||
| 122 | /** |
||
| 123 | * Sets the namespace |
||
| 124 | * |
||
| 125 | * @param string $namespace namespace |
||
| 126 | * |
||
| 127 | * @return void |
||
| 128 | */ |
||
| 129 | 10 | public function setNamespace($namespace) |
|
| 140 | |||
| 141 | /** |
||
| 142 | * Returns whether this service is read-only |
||
| 143 | * |
||
| 144 | * @return bool true if yes, false if not |
||
| 145 | */ |
||
| 146 | 4 | public function isReadOnlyService() |
|
| 154 | |||
| 155 | /** |
||
| 156 | * Returns whether this service is versioning |
||
| 157 | * |
||
| 158 | * @return bool true if yes, false if not |
||
| 159 | */ |
||
| 160 | public function isVersionedService() |
||
| 168 | |||
| 169 | /** |
||
| 170 | * Returns whether this service has fixtures |
||
| 171 | * |
||
| 172 | * @return bool true if yes, false if not |
||
| 173 | */ |
||
| 174 | 4 | public function hasFixtures() |
|
| 178 | |||
| 179 | /** |
||
| 180 | * Returns the fixtures or empty array if none |
||
| 181 | * |
||
| 182 | * @return array fixtures |
||
| 183 | */ |
||
| 184 | 4 | public function getFixtures() |
|
| 192 | |||
| 193 | /** |
||
| 194 | * Returns the order number at which order this fixture should be loaded. |
||
| 195 | * this is needed if we have relations/references between the fixtures.. |
||
| 196 | * |
||
| 197 | * @return int order |
||
| 198 | */ |
||
| 199 | 4 | public function getFixtureOrder() |
|
| 208 | |||
| 209 | /** |
||
| 210 | * Returns a router base path. false if default should be used. |
||
| 211 | * |
||
| 212 | * @return string router base, i.e. /bundle/name/ |
||
|
|
|||
| 213 | */ |
||
| 214 | 6 | public function getRouterBase() |
|
| 231 | |||
| 232 | /** |
||
| 233 | * Returns the Controller classname this services' controller shout inherit. |
||
| 234 | * Defaults to the RestController of the RestBundle of course. |
||
| 235 | * |
||
| 236 | * @return string base controller |
||
| 237 | */ |
||
| 238 | 4 | public function getBaseController() |
|
| 247 | |||
| 248 | /** |
||
| 249 | * Returns the parent service to use when adding the service xml |
||
| 250 | * |
||
| 251 | * Defaults to graviton.rest.controller |
||
| 252 | * |
||
| 253 | * @return string base controller |
||
| 254 | */ |
||
| 255 | public function getParentService() |
||
| 264 | |||
| 265 | /** |
||
| 266 | * Returns a specific field or null |
||
| 267 | * |
||
| 268 | * @param string $name Field name |
||
| 269 | * |
||
| 270 | * @return DefinitionElementInterface The field |
||
| 271 | */ |
||
| 272 | 30 | public function getField($name) |
|
| 277 | |||
| 278 | /** |
||
| 279 | * Returns the field definition |
||
| 280 | * |
||
| 281 | * @return DefinitionElementInterface[] Fields |
||
| 282 | */ |
||
| 283 | 32 | public function getFields() |
|
| 310 | |||
| 311 | /** |
||
| 312 | * @param Schema\Field $definition Raw field definition |
||
| 313 | * @param string $path Relative field path |
||
| 314 | * @return mixed |
||
| 315 | * @throws \InvalidArgumentException |
||
| 316 | */ |
||
| 317 | 30 | private function createFieldHierarchyRecursive(Schema\Field $definition, $path) |
|
| 332 | |||
| 333 | /** |
||
| 334 | * @param string $name Field name |
||
| 335 | * @param array $data Field data |
||
| 336 | * |
||
| 337 | * @return DefinitionElementInterface |
||
| 338 | */ |
||
| 339 | 30 | private function processFieldHierarchyRecursive($name, $data) |
|
| 367 | |||
| 368 | /** |
||
| 369 | * @param string $name Field name |
||
| 370 | * @param Schema\Field $definition Field |
||
| 371 | * |
||
| 372 | * @return DefinitionElementInterface |
||
| 373 | */ |
||
| 374 | 30 | private function processSimpleField($name, Schema\Field $definition) |
|
| 388 | |||
| 389 | /** |
||
| 390 | * Get target relations which are explictly defined |
||
| 391 | * |
||
| 392 | * @return Schema\Relation[] relations |
||
| 393 | */ |
||
| 394 | 32 | public function getRelations() |
|
| 407 | |||
| 408 | /** |
||
| 409 | * Get relation by field name |
||
| 410 | * |
||
| 411 | * @param string $field Field name |
||
| 412 | * @return Schema\Relation|null |
||
| 413 | */ |
||
| 414 | 24 | private function getRelation($field) |
|
| 419 | |||
| 420 | /** |
||
| 421 | * Provides the role set defined in the service section. |
||
| 422 | * |
||
| 423 | * @return array |
||
| 424 | */ |
||
| 425 | 2 | public function getRoles() |
|
| 433 | |||
| 434 | /** |
||
| 435 | * Can record origin be modified |
||
| 436 | * |
||
| 437 | * @return bool |
||
| 438 | */ |
||
| 439 | public function isRecordOriginModifiable() |
||
| 448 | |||
| 449 | /** |
||
| 450 | * check if the RecordOriginModifiable flag is set |
||
| 451 | * |
||
| 452 | * @return bool |
||
| 453 | */ |
||
| 454 | public function isRecordOriginFlagSet() |
||
| 465 | |||
| 466 | /** |
||
| 467 | * @return string |
||
| 468 | */ |
||
| 469 | public function getServiceCollection() |
||
| 481 | |||
| 482 | /** |
||
| 483 | * @return string[] |
||
| 484 | */ |
||
| 485 | 2 | public function getIndexes() |
|
| 493 | |||
| 494 | /** |
||
| 495 | * @return string[] |
||
| 496 | */ |
||
| 497 | public function getSearchables() |
||
| 509 | |||
| 510 | /** |
||
| 511 | * @return string[] |
||
| 512 | */ |
||
| 513 | 2 | public function getTextIndexes() |
|
| 525 | |||
| 526 | /** |
||
| 527 | * Combine in one array the Search text indexes |
||
| 528 | * |
||
| 529 | * @return array |
||
| 530 | */ |
||
| 531 | public function getAllTextIndexes() |
||
| 538 | } |
||
| 539 |
This check compares the return type specified in the
@returnannotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.