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) |
|
178 | |||
179 | /** |
||
180 | * Process method of class |
||
181 | * |
||
182 | * @param \ReflectionMethod $method |
||
183 | * @param Definition $definition |
||
184 | */ |
||
185 | 18 | protected function processMethod(\ReflectionMethod $method, Definition $definition) |
|
198 | |||
199 | /** |
||
200 | * Process annotations of class |
||
201 | * |
||
202 | * @param \ReflectionClass $reflection |
||
203 | * @param Definition $definition |
||
204 | */ |
||
205 | 18 | protected function processClassAnnotations(\ReflectionClass $reflection, Definition $definition) |
|
223 | |||
224 | /** |
||
225 | * Handler resource identifier |
||
226 | * |
||
227 | * @param ResourceIdentifierAnnotation $annotation |
||
228 | * @param Definition $definition |
||
229 | */ |
||
230 | 7 | protected function handlerResourceIdentifier(ResourceIdentifierAnnotation $annotation, Definition $definition) |
|
236 | |||
237 | /** |
||
238 | * Create attribute by annotation of property |
||
239 | * |
||
240 | * @param AttributeAnnotation $annotation |
||
241 | * @param \ReflectionProperty $property |
||
242 | * @return Attribute |
||
243 | */ |
||
244 | 2 | protected function createAttributeByProperty(AttributeAnnotation $annotation, \ReflectionProperty $property): Attribute |
|
263 | |||
264 | /** |
||
265 | * Create attribute by annotation of method |
||
266 | * |
||
267 | * @param AttributeAnnotation $annotation |
||
268 | * @param \ReflectionMethod $method |
||
269 | * @return Attribute |
||
270 | */ |
||
271 | 2 | protected function createAttributeByMethod(AttributeAnnotation $annotation, \ReflectionMethod $method): Attribute |
|
289 | |||
290 | /** |
||
291 | * Resolve name of attribute by method |
||
292 | * |
||
293 | * @param \ReflectionMethod $method |
||
294 | * @return string |
||
295 | */ |
||
296 | 2 | protected function resolveNameByMethod(\ReflectionMethod $method): string |
|
306 | |||
307 | /** |
||
308 | * Process data-type |
||
309 | * |
||
310 | * @param string $definition |
||
311 | * @param Attribute $attribute |
||
312 | */ |
||
313 | 4 | protected function processDataType(string $definition, Attribute $attribute) |
|
330 | |||
331 | /** |
||
332 | * Process relationship |
||
333 | * |
||
334 | * @param RelationshipAnnotation $annotation |
||
335 | * @param \ReflectionProperty $property |
||
336 | * @return Relationship |
||
337 | */ |
||
338 | 9 | protected function createRelationship(RelationshipAnnotation $annotation, \ReflectionProperty $property): Relationship |
|
358 | |||
359 | /** |
||
360 | * Resolve getter of related object |
||
361 | * |
||
362 | * @param \ReflectionProperty $property |
||
363 | * @return string |
||
364 | */ |
||
365 | 11 | protected function resolveGetter(\ReflectionProperty $property): string |
|
385 | |||
386 | /** |
||
387 | * Handle links |
||
388 | * |
||
389 | * @param RelationshipAnnotation $annotation |
||
390 | * @param Relationship $relationship |
||
391 | */ |
||
392 | 9 | protected function handleLinks(RelationshipAnnotation $annotation, Relationship $relationship) |
|
401 | |||
402 | /** |
||
403 | * Create link by link's annotation |
||
404 | * |
||
405 | * @param LinkAnnotation $annotation |
||
406 | * @return Link |
||
407 | */ |
||
408 | 8 | protected function createLink(LinkAnnotation $annotation): Link |
|
425 | |||
426 | /** |
||
427 | * Handle control of data-section |
||
428 | * |
||
429 | * @param RelationshipAnnotation $annotation |
||
430 | * @param Relationship $relationship |
||
431 | */ |
||
432 | 9 | protected function handleDataControl(RelationshipAnnotation $annotation, Relationship $relationship) |
|
437 | |||
438 | /** |
||
439 | * Resolve type of relationship |
||
440 | * |
||
441 | * @param RelationshipAnnotation $annotation |
||
442 | * @return int |
||
443 | */ |
||
444 | 9 | protected function resolveType(RelationshipAnnotation $annotation): int |
|
456 | } |