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 | 17 | 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 | 17 | public function __construct(Reader $reader) |
|
| 77 | |||
| 78 | /** |
||
| 79 | * {@inheritdoc} |
||
| 80 | */ |
||
| 81 | 17 | public function getDefinition(string $class): Definition |
|
| 91 | |||
| 92 | /** |
||
| 93 | * Create definition for given class |
||
| 94 | * |
||
| 95 | * @param \ReflectionClass $reflection |
||
| 96 | * @return Definition |
||
| 97 | */ |
||
| 98 | 17 | protected function createDefinition(\ReflectionClass $reflection): Definition |
|
| 114 | |||
| 115 | /** |
||
| 116 | * Process properties of class |
||
| 117 | * |
||
| 118 | * @param \ReflectionClass $reflection |
||
| 119 | * @param Definition $definition |
||
| 120 | */ |
||
| 121 | 17 | protected function processProperties(\ReflectionClass $reflection, Definition $definition) |
|
| 128 | |||
| 129 | /** |
||
| 130 | * Process property of class |
||
| 131 | * |
||
| 132 | * @param \ReflectionProperty $property |
||
| 133 | * @param Definition $definition |
||
| 134 | */ |
||
| 135 | 15 | protected function processProperty(\ReflectionProperty $property, Definition $definition) |
|
| 155 | |||
| 156 | /** |
||
| 157 | * Process methods of class |
||
| 158 | * |
||
| 159 | * @param \ReflectionClass $reflection |
||
| 160 | * @param Definition $definition |
||
| 161 | */ |
||
| 162 | 17 | protected function processMethods(\ReflectionClass $reflection, Definition $definition) |
|
| 173 | |||
| 174 | /** |
||
| 175 | * Process method of class |
||
| 176 | * |
||
| 177 | * @param \ReflectionMethod $method |
||
| 178 | * @param Definition $definition |
||
| 179 | */ |
||
| 180 | 17 | protected function processMethod(\ReflectionMethod $method, Definition $definition) |
|
| 193 | |||
| 194 | /** |
||
| 195 | * Process annotations of class |
||
| 196 | * |
||
| 197 | * @param \ReflectionClass $reflection |
||
| 198 | * @param Definition $definition |
||
| 199 | */ |
||
| 200 | 17 | protected function processClassAnnotations(\ReflectionClass $reflection, Definition $definition) |
|
| 218 | |||
| 219 | /** |
||
| 220 | * Handler resource identifier |
||
| 221 | * |
||
| 222 | * @param ResourceIdentifierAnnotation $annotation |
||
| 223 | * @param Definition $definition |
||
| 224 | */ |
||
| 225 | 7 | protected function handlerResourceIdentifier(ResourceIdentifierAnnotation $annotation, Definition $definition) |
|
| 231 | |||
| 232 | /** |
||
| 233 | * Create attribute by annotation of property |
||
| 234 | * |
||
| 235 | * @param AttributeAnnotation $annotation |
||
| 236 | * @param \ReflectionProperty $property |
||
| 237 | * @return Attribute |
||
| 238 | */ |
||
| 239 | 2 | protected function createAttributeByProperty(AttributeAnnotation $annotation, \ReflectionProperty $property): Attribute |
|
| 258 | |||
| 259 | /** |
||
| 260 | * Create attribute by annotation of method |
||
| 261 | * |
||
| 262 | * @param AttributeAnnotation $annotation |
||
| 263 | * @param \ReflectionMethod $method |
||
| 264 | * @return Attribute |
||
| 265 | */ |
||
| 266 | 2 | protected function createAttributeByMethod(AttributeAnnotation $annotation, \ReflectionMethod $method): Attribute |
|
| 284 | |||
| 285 | /** |
||
| 286 | * Resolve name of attribute by method |
||
| 287 | * |
||
| 288 | * @param \ReflectionMethod $method |
||
| 289 | * @return string |
||
| 290 | */ |
||
| 291 | 2 | protected function resolveNameByMethod(\ReflectionMethod $method): string |
|
| 301 | |||
| 302 | /** |
||
| 303 | * Process data-type |
||
| 304 | * |
||
| 305 | * @param string $definition |
||
| 306 | * @param Attribute $attribute |
||
| 307 | */ |
||
| 308 | 4 | protected function processDataType(string $definition, Attribute $attribute) |
|
| 325 | |||
| 326 | /** |
||
| 327 | * Process relationship |
||
| 328 | * |
||
| 329 | * @param RelationshipAnnotation $annotation |
||
| 330 | * @param \ReflectionProperty $property |
||
| 331 | * @return Relationship |
||
| 332 | */ |
||
| 333 | 9 | protected function createRelationship(RelationshipAnnotation $annotation, \ReflectionProperty $property): Relationship |
|
| 353 | |||
| 354 | /** |
||
| 355 | * Resolve getter of related object |
||
| 356 | * |
||
| 357 | * @param \ReflectionProperty $property |
||
| 358 | * @return string |
||
| 359 | */ |
||
| 360 | 11 | protected function resolveGetter(\ReflectionProperty $property): string |
|
| 380 | |||
| 381 | /** |
||
| 382 | * Handle links |
||
| 383 | * |
||
| 384 | * @param RelationshipAnnotation $annotation |
||
| 385 | * @param Relationship $relationship |
||
| 386 | */ |
||
| 387 | 9 | protected function handleLinks(RelationshipAnnotation $annotation, Relationship $relationship) |
|
| 396 | |||
| 397 | /** |
||
| 398 | * Create link by link's annotation |
||
| 399 | * |
||
| 400 | * @param LinkAnnotation $annotation |
||
| 401 | * @return Link |
||
| 402 | */ |
||
| 403 | 8 | protected function createLink(LinkAnnotation $annotation): Link |
|
| 420 | |||
| 421 | /** |
||
| 422 | * Handle control of data-section |
||
| 423 | * |
||
| 424 | * @param RelationshipAnnotation $annotation |
||
| 425 | * @param Relationship $relationship |
||
| 426 | */ |
||
| 427 | 9 | protected function handleDataControl(RelationshipAnnotation $annotation, Relationship $relationship) |
|
| 432 | |||
| 433 | /** |
||
| 434 | * Resolve type of relationship |
||
| 435 | * |
||
| 436 | * @param RelationshipAnnotation $annotation |
||
| 437 | * @return int |
||
| 438 | */ |
||
| 439 | 9 | protected function resolveType(RelationshipAnnotation $annotation): int |
|
| 451 | } |