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 | 58 | public function __construct(Addendum $addendum = null) |
|
67 | |||
68 | /** |
||
69 | * Build annotations collection |
||
70 | * @param ReflectionAnnotatedClass|ReflectionAnnotatedMethod|ReflectionAnnotatedProperty $targetReflection |
||
71 | * @return AnnotationsCollection |
||
72 | */ |
||
73 | 58 | public function build($targetReflection) |
|
94 | |||
95 | 58 | private function buildOne($targetReflection) |
|
160 | |||
161 | /** |
||
162 | * Get reflection for related target reflection. |
||
163 | * Will return class, property or method reflection from related reflection, |
||
164 | * based on type of target reflection. |
||
165 | * |
||
166 | * Will return false target reflection id for method or property, and |
||
167 | * method or property does not exists on related reflection. |
||
168 | * |
||
169 | * @param Reflector $targetReflection |
||
170 | * @param ReflectionClass $relatedReflection |
||
171 | * @return ReflectionClass|ReflectionProperty|ReflectionMethod|bool |
||
172 | */ |
||
173 | 51 | private function getRelated(Reflector $targetReflection, ReflectionClass $relatedReflection) |
|
186 | |||
187 | /** |
||
188 | * Get partials of class, this includes: |
||
189 | * |
||
190 | * * Parent class |
||
191 | * * Interfaces |
||
192 | * * Traits |
||
193 | * |
||
194 | * @param ReflectionClass $targetClass |
||
195 | * @return ReflectionAnnotatedClass[] |
||
196 | */ |
||
197 | 53 | private function getPartials(ReflectionClass $targetClass) |
|
225 | |||
226 | /** |
||
227 | * Create new instance of annotation |
||
228 | * @param string $class |
||
229 | * @param mixed[] $parameters |
||
230 | * @param ReflectionAnnotatedClass|ReflectionAnnotatedMethod|ReflectionAnnotatedProperty|bool $targetReflection |
||
231 | * @return boolean|object |
||
232 | */ |
||
233 | 55 | public function instantiateAnnotation($class, $parameters, $targetReflection = false) |
|
234 | { |
||
235 | 55 | $class = ucfirst($class) . "Annotation"; |
|
236 | |||
237 | // If namespaces are empty assume global namespace |
||
238 | 55 | $fqn = $this->normalizeFqn('\\', $class); |
|
239 | 55 | $mandatoryNs = TargetAnnotation::Ns; |
|
240 | 55 | $namespaces = $this->addendum->namespaces; |
|
241 | 55 | if (!in_array($mandatoryNs, $namespaces)) |
|
242 | { |
||
243 | $namespaces[] = $mandatoryNs; |
||
244 | } |
||
245 | 55 | foreach ($namespaces as $ns) |
|
246 | { |
||
247 | 55 | $fqn = $this->normalizeFqn($ns, $class); |
|
248 | 55 | if (Blacklister::ignores($fqn)) |
|
249 | { |
||
250 | 46 | continue; |
|
251 | } |
||
252 | try |
||
253 | { |
||
254 | 55 | if (!ClassChecker::exists($fqn)) |
|
255 | { |
||
256 | 15 | $this->addendum->getLogger()->debug('Annotation class `{fqn}` not found, ignoring', ['fqn' => $fqn]); |
|
257 | 15 | Blacklister::ignore($fqn); |
|
258 | } |
||
259 | else |
||
260 | { |
||
261 | // Class exists, exit loop |
||
262 | 55 | break; |
|
263 | } |
||
264 | } |
||
265 | catch (Exception $e) |
||
266 | { |
||
267 | // Ignore class autoloading errors |
||
268 | } |
||
269 | } |
||
270 | 55 | if (Blacklister::ignores($fqn)) |
|
271 | { |
||
272 | 1 | return false; |
|
273 | } |
||
274 | try |
||
275 | { |
||
276 | // NOTE: was class_exists here, however should be safe to use ClassChecker::exists here |
||
277 | 54 | if (!ClassChecker::exists($fqn)) |
|
278 | { |
||
279 | $this->addendum->getLogger()->debug('Annotation class `{fqn}` not found, ignoring', ['fqn' => $fqn]); |
||
280 | Blacklister::ignore($fqn); |
||
281 | 54 | return false; |
|
282 | } |
||
283 | } |
||
284 | catch (Exception $e) |
||
285 | { |
||
286 | // Ignore autoload errors and return false |
||
287 | Blacklister::ignore($fqn); |
||
288 | return false; |
||
289 | } |
||
290 | 54 | $resolvedClass = Addendum::resolveClassName($fqn); |
|
291 | 54 | if ((new ReflectionClass($resolvedClass))->implementsInterface(AnnotationInterface::class) || $resolvedClass == AnnotationInterface::class) |
|
292 | { |
||
293 | 54 | return new $resolvedClass($parameters, $targetReflection); |
|
294 | } |
||
295 | return false; |
||
296 | } |
||
297 | |||
298 | /** |
||
299 | * Normalize class name and namespace to proper fully qualified name |
||
300 | * @param string $ns |
||
301 | * @param string $class |
||
302 | * @return string |
||
303 | */ |
||
304 | 55 | private function normalizeFqn($ns, $class) |
|
310 | |||
311 | /** |
||
312 | * Get doc comment |
||
313 | * @param ReflectionAnnotatedClass|ReflectionAnnotatedMethod|ReflectionAnnotatedProperty $reflection |
||
314 | * @return array |
||
315 | */ |
||
316 | 53 | private function parse($reflection) |
|
338 | |||
339 | /** |
||
340 | * Get doc comment |
||
341 | * @param ReflectionAnnotatedClass|ReflectionAnnotatedMethod|ReflectionAnnotatedProperty $reflection |
||
342 | * @return mixed[] |
||
343 | */ |
||
344 | 50 | protected function getDocComment($reflection) |
|
348 | |||
349 | /** |
||
350 | * Clear local parsing cache |
||
351 | */ |
||
352 | 3 | public static function clearCache() |
|
356 | |||
357 | } |
||
358 |
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: