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 | 15 | 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 | 15 | public function __construct(Reader $reader) |
|
77 | |||
78 | /** |
||
79 | * {@inheritdoc} |
||
80 | */ |
||
81 | 15 | public function getDefinition(string $class): Definition |
|
91 | |||
92 | /** |
||
93 | * Create definition for given class |
||
94 | * |
||
95 | * @param \ReflectionClass $reflection |
||
96 | * @return Definition |
||
97 | */ |
||
98 | 15 | protected function createDefinition(\ReflectionClass $reflection): Definition |
|
113 | |||
114 | /** |
||
115 | * Process properties of class |
||
116 | * |
||
117 | * @param \ReflectionClass $reflection |
||
118 | * @param Definition $definition |
||
119 | */ |
||
120 | 15 | protected function processProperties(\ReflectionClass $reflection, Definition $definition) |
|
143 | |||
144 | /** |
||
145 | * Process annotations of class |
||
146 | * |
||
147 | * @param \ReflectionClass $reflection |
||
148 | * @param Definition $definition |
||
149 | */ |
||
150 | 15 | protected function processClassAnnotations(\ReflectionClass $reflection, Definition $definition) |
|
168 | |||
169 | /** |
||
170 | * Handler resource identifier |
||
171 | * |
||
172 | * @param ResourceIdentifierAnnotation $annotation |
||
173 | * @param Definition $definition |
||
174 | */ |
||
175 | 7 | protected function handlerResourceIdentifier(ResourceIdentifierAnnotation $annotation, Definition $definition) |
|
181 | |||
182 | /** |
||
183 | * Create attribute |
||
184 | * |
||
185 | * @param AttributeAnnotation $annotation |
||
186 | * @param \ReflectionProperty $property |
||
187 | * @return Attribute |
||
188 | */ |
||
189 | 2 | protected function createAttribute(AttributeAnnotation $annotation, \ReflectionProperty $property) |
|
208 | |||
209 | /** |
||
210 | * Process data-type |
||
211 | * |
||
212 | * @param string $definition |
||
213 | * @param Attribute $attribute |
||
214 | */ |
||
215 | 2 | protected function processDataType(string $definition, Attribute $attribute) |
|
232 | |||
233 | /** |
||
234 | * Process relationship |
||
235 | * |
||
236 | * @param RelationshipAnnotation $annotation |
||
237 | * @param \ReflectionProperty $property |
||
238 | * @return Relationship |
||
239 | */ |
||
240 | 9 | protected function createRelationship(RelationshipAnnotation $annotation, \ReflectionProperty $property): Relationship |
|
260 | |||
261 | /** |
||
262 | * Resolve getter of related object |
||
263 | * |
||
264 | * @param \ReflectionProperty $property |
||
265 | * @return string |
||
266 | */ |
||
267 | 11 | protected function resolveGetter(\ReflectionProperty $property): string |
|
287 | |||
288 | /** |
||
289 | * Handle links |
||
290 | * |
||
291 | * @param RelationshipAnnotation $annotation |
||
292 | * @param Relationship $relationship |
||
293 | */ |
||
294 | 9 | protected function handleLinks(RelationshipAnnotation $annotation, Relationship $relationship) |
|
303 | |||
304 | /** |
||
305 | * Create link by link's annotation |
||
306 | * |
||
307 | * @param LinkAnnotation $annotation |
||
308 | * @return Link |
||
309 | */ |
||
310 | 8 | protected function createLink(LinkAnnotation $annotation): Link |
|
327 | |||
328 | /** |
||
329 | * Handle control of data-section |
||
330 | * |
||
331 | * @param RelationshipAnnotation $annotation |
||
332 | * @param Relationship $relationship |
||
333 | */ |
||
334 | 9 | protected function handleDataControl(RelationshipAnnotation $annotation, Relationship $relationship) |
|
339 | |||
340 | /** |
||
341 | * Resolve type of relationship |
||
342 | * |
||
343 | * @param RelationshipAnnotation $annotation |
||
344 | * @return int |
||
345 | */ |
||
346 | 9 | protected function resolveType(RelationshipAnnotation $annotation): int |
|
358 | } |