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 |
|
284 | |||
285 | /** |
||
286 | * Create attribute by annotation of method |
||
287 | * |
||
288 | * @param AttributeAnnotation $annotation |
||
289 | * @param \ReflectionMethod $method |
||
290 | * @return Attribute |
||
291 | */ |
||
292 | 2 | protected function createAttributeByMethod(AttributeAnnotation $annotation, \ReflectionMethod $method): Attribute |
|
306 | |||
307 | /** |
||
308 | * Resolve name of attribute by method |
||
309 | * |
||
310 | * @param \ReflectionMethod $method |
||
311 | * @return string |
||
312 | */ |
||
313 | 2 | protected function resolveNameByMethod(\ReflectionMethod $method): string |
|
323 | |||
324 | /** |
||
325 | * Process data-type |
||
326 | * |
||
327 | * @param string $definition |
||
328 | * @param Attribute $attribute |
||
329 | */ |
||
330 | 4 | protected function processDataType(string $definition, Attribute $attribute) |
|
347 | |||
348 | /** |
||
349 | * Process relationship |
||
350 | * |
||
351 | * @param RelationshipAnnotation $annotation |
||
352 | * @param \ReflectionProperty $property |
||
353 | * @return Relationship |
||
354 | */ |
||
355 | 9 | protected function createRelationship(RelationshipAnnotation $annotation, \ReflectionProperty $property): Relationship |
|
375 | |||
376 | /** |
||
377 | * Resolve getter of related object |
||
378 | * |
||
379 | * @param \ReflectionProperty $property |
||
380 | * @return string |
||
381 | */ |
||
382 | 11 | protected function resolveGetter(\ReflectionProperty $property): string |
|
402 | |||
403 | /** |
||
404 | * Handle links |
||
405 | * |
||
406 | * @param RelationshipAnnotation $annotation |
||
407 | * @param Relationship $relationship |
||
408 | */ |
||
409 | 9 | protected function handleLinks(RelationshipAnnotation $annotation, Relationship $relationship) |
|
418 | |||
419 | /** |
||
420 | * Create link by link's annotation |
||
421 | * |
||
422 | * @param LinkAnnotation $annotation |
||
423 | * @return Link |
||
424 | */ |
||
425 | 8 | protected function createLink(LinkAnnotation $annotation): Link |
|
442 | |||
443 | /** |
||
444 | * Handle control of data-section |
||
445 | * |
||
446 | * @param RelationshipAnnotation $annotation |
||
447 | * @param Relationship $relationship |
||
448 | */ |
||
449 | 9 | protected function handleDataControl(RelationshipAnnotation $annotation, Relationship $relationship) |
|
454 | |||
455 | /** |
||
456 | * Resolve type of relationship |
||
457 | * |
||
458 | * @param RelationshipAnnotation $annotation |
||
459 | * @return int |
||
460 | */ |
||
461 | 9 | protected function resolveType(RelationshipAnnotation $annotation): int |
|
473 | } |