Complex classes like Builder 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 Builder, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
41 | class Builder |
||
42 | { |
||
43 | |||
44 | /** |
||
45 | * Cached values of parsing |
||
46 | * @var string[][][] |
||
47 | */ |
||
48 | private static $cache = []; |
||
49 | |||
50 | /** |
||
51 | * Addendum instance |
||
52 | * @var Addendum |
||
53 | */ |
||
54 | private $addendum = null; |
||
55 | |||
56 | /** |
||
57 | * One entity build cache |
||
58 | * @var BuildOneCache |
||
59 | */ |
||
60 | private $buildCache = null; |
||
61 | |||
62 | 50 | public function __construct(Addendum $addendum = null) |
|
67 | |||
68 | /** |
||
69 | * Build annotations collection |
||
70 | * @param ReflectionAnnotatedClass|ReflectionAnnotatedMethod|ReflectionAnnotatedProperty $targetReflection |
||
71 | * @return AnnotationsCollection |
||
72 | */ |
||
73 | 50 | public function build($targetReflection) |
|
94 | |||
95 | 50 | private function buildOne($targetReflection) |
|
159 | |||
160 | /** |
||
161 | * Get reflection for related target reflection. |
||
162 | * Will return class, property or method reflection from related reflection, |
||
163 | * based on type of target reflection. |
||
164 | * |
||
165 | * Will return false target reflection id for method or property, and |
||
166 | * method or property does not exists on related reflection. |
||
167 | * |
||
168 | * @param Reflector $targetReflection |
||
169 | * @param ReflectionClass $relatedReflection |
||
170 | * @return ReflectionClass|ReflectionProperty|ReflectionMethod|bool |
||
171 | */ |
||
172 | 44 | private function getRelated(Reflector $targetReflection, ReflectionClass $relatedReflection) |
|
185 | |||
186 | /** |
||
187 | * Get partials of class, this includes: |
||
188 | * |
||
189 | * * Parent class |
||
190 | * * Interfaces |
||
191 | * * Traits |
||
192 | * |
||
193 | * @param ReflectionClass $targetClass |
||
194 | * @return ReflectionAnnotatedClass[] |
||
195 | */ |
||
196 | 46 | private function getPartials(ReflectionClass $targetClass) |
|
224 | |||
225 | /** |
||
226 | * Create new instance of annotation |
||
227 | * @param string $class |
||
228 | * @param mixed[] $parameters |
||
229 | * @param ReflectionAnnotatedClass|ReflectionAnnotatedMethod|ReflectionAnnotatedProperty|bool $targetReflection |
||
230 | * @return boolean|object |
||
231 | */ |
||
232 | 48 | public function instantiateAnnotation($class, $parameters, $targetReflection = false) |
|
233 | { |
||
234 | 48 | $class = ucfirst($class) . "Annotation"; |
|
235 | |||
236 | // If namespaces are empty assume global namespace |
||
237 | 48 | $fqn = $this->normalizeFqn('\\', $class); |
|
238 | 48 | $mandatoryNs = TargetAnnotation::Ns; |
|
239 | 48 | $namespaces = $this->addendum->namespaces; |
|
240 | 48 | if (!in_array($mandatoryNs, $namespaces)) |
|
241 | { |
||
242 | $namespaces[] = $mandatoryNs; |
||
243 | } |
||
244 | 48 | foreach ($namespaces as $ns) |
|
245 | { |
||
246 | 48 | $fqn = $this->normalizeFqn($ns, $class); |
|
247 | 48 | if (Blacklister::ignores($fqn)) |
|
248 | { |
||
249 | 37 | continue; |
|
250 | } |
||
251 | try |
||
252 | { |
||
253 | 48 | if (!ClassChecker::exists($fqn)) |
|
254 | { |
||
255 | 45 | $this->addendum->getLogger()->debug('Annotation class `{fqn}` not found, ignoring', ['fqn' => $fqn]); |
|
256 | 45 | Blacklister::ignore($fqn); |
|
257 | } |
||
258 | else |
||
259 | { |
||
260 | // Class exists, exit loop |
||
261 | 48 | break; |
|
262 | } |
||
263 | } |
||
264 | catch (Exception $e) |
||
265 | { |
||
266 | // Ignore class autoloading errors |
||
267 | } |
||
268 | } |
||
269 | 48 | if (Blacklister::ignores($fqn)) |
|
270 | { |
||
271 | 1 | return false; |
|
272 | } |
||
273 | try |
||
274 | { |
||
275 | // NOTE: was class_exists here, however should be safe to use ClassChecker::exists here |
||
276 | 48 | if (!ClassChecker::exists($fqn)) |
|
277 | { |
||
278 | $this->addendum->getLogger()->debug('Annotation class `{fqn}` not found, ignoring', ['fqn' => $fqn]); |
||
279 | Blacklister::ignore($fqn); |
||
280 | return false; |
||
281 | } |
||
282 | } |
||
283 | catch (Exception $e) |
||
284 | { |
||
285 | // Ignore autoload errors and return false |
||
286 | Blacklister::ignore($fqn); |
||
287 | return false; |
||
288 | } |
||
289 | 48 | $resolvedClass = Addendum::resolveClassName($fqn); |
|
290 | 48 | if ((new ReflectionClass($resolvedClass))->implementsInterface(AnnotationInterface::class) || $resolvedClass == AnnotationInterface::class) |
|
291 | { |
||
292 | 48 | return new $resolvedClass($parameters, $targetReflection); |
|
293 | } |
||
294 | return false; |
||
295 | } |
||
296 | |||
297 | /** |
||
298 | * Normalize class name and namespace to proper fully qualified name |
||
299 | * @param string $ns |
||
300 | * @param string $class |
||
301 | * @return string |
||
302 | */ |
||
303 | 48 | private function normalizeFqn($ns, $class) |
|
309 | |||
310 | /** |
||
311 | * Get doc comment |
||
312 | * @param ReflectionAnnotatedClass|ReflectionAnnotatedMethod|ReflectionAnnotatedProperty $reflection |
||
313 | * @return mixed[] |
||
314 | */ |
||
315 | 46 | private function parse($reflection) |
|
337 | |||
338 | /** |
||
339 | * Get doc comment |
||
340 | * @param ReflectionAnnotatedClass|ReflectionAnnotatedMethod|ReflectionAnnotatedProperty $reflection |
||
341 | * @return mixed[] |
||
342 | */ |
||
343 | 43 | protected function getDocComment($reflection) |
|
347 | |||
348 | /** |
||
349 | * Clear local parsing cache |
||
350 | */ |
||
351 | 3 | public static function clearCache() |
|
355 | |||
356 | } |
||
357 |