Complex classes like AnnotationDefinitionProvider 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 AnnotationDefinitionProvider, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 18 | class AnnotationDefinitionProvider implements DefinitionProviderInterface |
||
| 19 | { |
||
| 20 | /** |
||
| 21 | * Pattern of "resource" parameter of link annotation |
||
| 22 | */ |
||
| 23 | const RESOURCE_PATTERN = '~^(?<repository>[a-z_][a-z0-9_]*)\.(?<link>[a-z_][a-z0-9_]*)$~i'; |
||
| 24 | |||
| 25 | /** |
||
| 26 | * Pattern of "type" parameter of attribute annotation |
||
| 27 | */ |
||
| 28 | const DATATYPE_PATTERN = '~^(?<type>[a-z_][a-z0-9_]*)\s*(?:\((?<params>[^\)]*)\))?$~i'; |
||
| 29 | |||
| 30 | /** |
||
| 31 | * Annotation classes ha been registered. |
||
| 32 | * |
||
| 33 | * @var bool |
||
| 34 | */ |
||
| 35 | static private $annotationsRegistered = false; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * Cache of created definitions |
||
| 39 | * |
||
| 40 | * @var array |
||
| 41 | */ |
||
| 42 | private $definitionCache = []; |
||
| 43 | |||
| 44 | /** |
||
| 45 | * Register annotation classes. |
||
| 46 | * Supports a medieval-aged way of "autoloading" for the Doctrine Annotation library. |
||
| 47 | */ |
||
| 48 | 18 | static protected function registerAnnotations() |
|
| 58 | |||
| 59 | /** |
||
| 60 | * Doctrine annotation reader |
||
| 61 | * |
||
| 62 | * @var Reader |
||
| 63 | */ |
||
| 64 | protected $reader; |
||
| 65 | |||
| 66 | /** |
||
| 67 | * AnnotationDefinitionProvider constructor. |
||
| 68 | * |
||
| 69 | * @param Reader $reader |
||
| 70 | */ |
||
| 71 | 18 | public function __construct(Reader $reader) |
|
| 77 | |||
| 78 | /** |
||
| 79 | * {@inheritdoc} |
||
| 80 | */ |
||
| 81 | 18 | public function getDefinition(string $class): Definition |
|
| 91 | |||
| 92 | /** |
||
| 93 | * Create definition for given class |
||
| 94 | * |
||
| 95 | * @param \ReflectionClass $reflection |
||
| 96 | * @return Definition |
||
| 97 | */ |
||
| 98 | 18 | protected function createDefinition(\ReflectionClass $reflection): Definition |
|
| 119 | |||
| 120 | /** |
||
| 121 | * Process properties of class |
||
| 122 | * |
||
| 123 | * @param \ReflectionClass $reflection |
||
| 124 | * @param Definition $definition |
||
| 125 | */ |
||
| 126 | 18 | protected function processProperties(\ReflectionClass $reflection, Definition $definition) |
|
| 133 | |||
| 134 | /** |
||
| 135 | * Process property of class |
||
| 136 | * |
||
| 137 | * @param \ReflectionProperty $property |
||
| 138 | * @param Definition $definition |
||
| 139 | */ |
||
| 140 | 16 | protected function processProperty(\ReflectionProperty $property, Definition $definition) |
|
| 160 | |||
| 161 | /** |
||
| 162 | * Process methods of class |
||
| 163 | * |
||
| 164 | * @param \ReflectionClass $reflection |
||
| 165 | * @param Definition $definition |
||
| 166 | */ |
||
| 167 | 18 | protected function processMethods(\ReflectionClass $reflection, Definition $definition) |
|
| 174 | |||
| 175 | /** |
||
| 176 | * Process method of class |
||
| 177 | * |
||
| 178 | * @param \ReflectionMethod $method |
||
| 179 | * @param Definition $definition |
||
| 180 | */ |
||
| 181 | 18 | protected function processMethod(\ReflectionMethod $method, Definition $definition) |
|
| 195 | |||
| 196 | /** |
||
| 197 | * Validate method with attribute definition |
||
| 198 | * |
||
| 199 | * @param AttributeAnnotation $annotation |
||
| 200 | * @param \ReflectionMethod $method |
||
| 201 | * @throws \LogicException |
||
| 202 | */ |
||
| 203 | 2 | protected function validateMethodAttribute(AttributeAnnotation $annotation, \ReflectionMethod $method) |
|
| 219 | |||
| 220 | /** |
||
| 221 | * Process annotations of class |
||
| 222 | * |
||
| 223 | * @param \ReflectionClass $reflection |
||
| 224 | * @param Definition $definition |
||
| 225 | */ |
||
| 226 | 18 | protected function processClassAnnotations(\ReflectionClass $reflection, Definition $definition) |
|
| 244 | |||
| 245 | /** |
||
| 246 | * Handler resource identifier |
||
| 247 | * |
||
| 248 | * @param ResourceIdentifierAnnotation $annotation |
||
| 249 | * @param Definition $definition |
||
| 250 | */ |
||
| 251 | 7 | protected function handlerResourceIdentifier(ResourceIdentifierAnnotation $annotation, Definition $definition) |
|
| 257 | |||
| 258 | /** |
||
| 259 | * Create attribute by annotation of property |
||
| 260 | * |
||
| 261 | * @param AttributeAnnotation $annotation |
||
| 262 | * @param \ReflectionProperty $property |
||
| 263 | * @return Attribute |
||
| 264 | */ |
||
| 265 | 2 | protected function createAttributeByProperty(AttributeAnnotation $annotation, \ReflectionProperty $property): Attribute |
|
| 290 | |||
| 291 | /** |
||
| 292 | * Process optional properties of attribute |
||
| 293 | * |
||
| 294 | * @param AttributeAnnotation $annotation |
||
| 295 | * @param Attribute $attribute |
||
| 296 | */ |
||
| 297 | 2 | protected function processAttributeOptions(AttributeAnnotation $annotation, Attribute $attribute) |
|
| 311 | |||
| 312 | /** |
||
| 313 | * Create attribute by annotation of method |
||
| 314 | * |
||
| 315 | * @param AttributeAnnotation $annotation |
||
| 316 | * @param \ReflectionMethod $method |
||
| 317 | * @return Attribute |
||
| 318 | */ |
||
| 319 | 2 | protected function createAttributeByMethod(AttributeAnnotation $annotation, \ReflectionMethod $method): Attribute |
|
| 333 | |||
| 334 | /** |
||
| 335 | * Resolve name of attribute by method |
||
| 336 | * |
||
| 337 | * @param \ReflectionMethod $method |
||
| 338 | * @return string |
||
| 339 | */ |
||
| 340 | 2 | protected function resolveNameByMethod(\ReflectionMethod $method): string |
|
| 350 | |||
| 351 | /** |
||
| 352 | * Process data-type |
||
| 353 | * |
||
| 354 | * @param string $definition |
||
| 355 | * @param Attribute $attribute |
||
| 356 | */ |
||
| 357 | 4 | protected function processDataType(string $definition, Attribute $attribute) |
|
| 374 | |||
| 375 | /** |
||
| 376 | * Process relationship |
||
| 377 | * |
||
| 378 | * @param RelationshipAnnotation $annotation |
||
| 379 | * @param \ReflectionProperty $property |
||
| 380 | * @return Relationship |
||
| 381 | */ |
||
| 382 | 9 | protected function createRelationship(RelationshipAnnotation $annotation, \ReflectionProperty $property): Relationship |
|
| 402 | |||
| 403 | /** |
||
| 404 | * Resolve getter of related object |
||
| 405 | * |
||
| 406 | * @param \ReflectionProperty $property |
||
| 407 | * @return string |
||
| 408 | */ |
||
| 409 | 11 | protected function resolveGetter(\ReflectionProperty $property) |
|
| 429 | |||
| 430 | /** |
||
| 431 | * Resolve getter of related object |
||
| 432 | * |
||
| 433 | * @param \ReflectionProperty $property |
||
| 434 | * @return string | null |
||
| 435 | */ |
||
| 436 | 2 | protected function resolveSetter(\ReflectionProperty $property) |
|
| 447 | |||
| 448 | /** |
||
| 449 | * Handle links |
||
| 450 | * |
||
| 451 | * @param RelationshipAnnotation $annotation |
||
| 452 | * @param Relationship $relationship |
||
| 453 | */ |
||
| 454 | 9 | protected function handleLinks(RelationshipAnnotation $annotation, Relationship $relationship) |
|
| 463 | |||
| 464 | /** |
||
| 465 | * Create link by link's annotation |
||
| 466 | * |
||
| 467 | * @param LinkAnnotation $annotation |
||
| 468 | * @return Link |
||
| 469 | */ |
||
| 470 | 8 | protected function createLink(LinkAnnotation $annotation): Link |
|
| 487 | |||
| 488 | /** |
||
| 489 | * Handle control of data-section |
||
| 490 | * |
||
| 491 | * @param RelationshipAnnotation $annotation |
||
| 492 | * @param Relationship $relationship |
||
| 493 | */ |
||
| 494 | 9 | protected function handleDataControl(RelationshipAnnotation $annotation, Relationship $relationship) |
|
| 499 | |||
| 500 | /** |
||
| 501 | * Resolve type of relationship |
||
| 502 | * |
||
| 503 | * @param RelationshipAnnotation $annotation |
||
| 504 | * @return int |
||
| 505 | */ |
||
| 506 | 9 | protected function resolveType(RelationshipAnnotation $annotation): int |
|
| 518 | } |