Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
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 declare(strict_types = 1); |
||
21 | class Builder |
||
22 | { |
||
23 | /** Controller classes scope name */ |
||
24 | const SCOPE_CONTROLLER = 'controllers'; |
||
25 | |||
26 | /** Service classes scope name */ |
||
27 | const SCOPE_SERVICES = 'services'; |
||
28 | |||
29 | /** Generated resolving function name prefix */ |
||
30 | const DI_FUNCTION_PREFIX = 'container'; |
||
31 | |||
32 | /** Generated resolving function service static collection name */ |
||
33 | const DI_FUNCTION_SERVICES = '$' . self::SCOPE_SERVICES . 'Instances'; |
||
34 | |||
35 | /** @var string[] Collection of available container scopes */ |
||
36 | protected $scopes = [ |
||
37 | self::SCOPE_CONTROLLER => [], |
||
38 | self::SCOPE_SERVICES => [] |
||
39 | ]; |
||
40 | |||
41 | /** @var ClassMetadata[] Collection of classes metadata */ |
||
42 | protected $classesMetadata = []; |
||
43 | |||
44 | /** @var array Collection of dependencies aliases */ |
||
45 | protected $classAliases = []; |
||
46 | |||
47 | /** |
||
48 | * @var Generator |
||
49 | * @Injectable |
||
50 | */ |
||
51 | protected $generator; |
||
52 | |||
53 | /** @var string Resolver function name */ |
||
54 | protected $resolverFunction; |
||
55 | |||
56 | /** |
||
57 | * Container builder constructor. |
||
58 | * |
||
59 | * @param Generator $generator PHP code generator |
||
60 | * @param ClassMetadata[] $classMetadata Collection of classes metadata for container |
||
61 | */ |
||
62 | 2 | public function __construct(Generator $generator, array $classMetadata) |
|
69 | |||
70 | /** |
||
71 | * Read class metadata and fill internal collections. |
||
72 | * |
||
73 | * @param ClassMetadata[] $classesMetadata |
||
74 | */ |
||
75 | 2 | public function processClassMetadata(array $classesMetadata) |
|
88 | |||
89 | /** |
||
90 | * Build container class. |
||
91 | * |
||
92 | * @param string|null $containerClass Container class name |
||
93 | * @param string $namespace Name space |
||
94 | * |
||
95 | * @return string Generated Container class code |
||
96 | * @throws \InvalidArgumentException |
||
97 | */ |
||
98 | 2 | public function build($containerClass = 'Container', $namespace = '') |
|
153 | |||
154 | /** |
||
155 | * Build dependency resolving function. |
||
156 | * |
||
157 | * @param string $functionName Function name |
||
158 | * |
||
159 | * @throws \InvalidArgumentException |
||
160 | */ |
||
161 | 2 | protected function buildDependencyResolver($functionName) |
|
175 | |||
176 | /** |
||
177 | * Generate logic conditions and their implementation for container and its delegates. |
||
178 | * |
||
179 | * @param string $inputVariable Input condition parameter variable name |
||
180 | * @param bool|false $started Flag if condition branching has been started |
||
181 | */ |
||
182 | 2 | public function generateConditions($inputVariable = '$alias', $started = false) |
|
270 | |||
271 | /** |
||
272 | * Build resolving function condition. |
||
273 | * |
||
274 | * @param string $inputVariable Condition variable |
||
275 | * @param string $className |
||
276 | * @param string|null $alias |
||
277 | * |
||
278 | * @return string Condition code |
||
279 | */ |
||
280 | 2 | protected function buildResolverCondition(string $inputVariable, string $className, string $alias = null) : string |
|
291 | |||
292 | /** |
||
293 | * Get valid class methods metadata. |
||
294 | * |
||
295 | * @param MethodMetadata[] $classMethodsMetadata All class methods metadata |
||
296 | * |
||
297 | * @return array Valid class methods metadata |
||
298 | */ |
||
299 | 2 | protected function getValidClassMethodsMetadata(array $classMethodsMetadata) |
|
312 | |||
313 | /** |
||
314 | * Get valid class properties metadata. |
||
315 | * |
||
316 | * @param PropertyMetadata[] $classPropertiesMetadata All class properties metadata |
||
317 | * |
||
318 | * @return array Valid class properties metadata |
||
319 | */ |
||
320 | 2 | protected function getValidClassPropertiesMetadata(array $classPropertiesMetadata) |
|
333 | |||
334 | /** |
||
335 | * Build resolving function class block. |
||
336 | * |
||
337 | * @param string $className Class name for new instance creation |
||
338 | */ |
||
339 | 2 | protected function buildResolvingClassDeclaration(string $className) |
|
343 | |||
344 | /** |
||
345 | * Build constructor arguments injection. |
||
346 | * |
||
347 | * @param MethodMetadata[] $methodsMetaData |
||
348 | */ |
||
349 | 2 | protected function buildConstructorDependencies(array $methodsMetaData) |
|
378 | |||
379 | /** |
||
380 | * Build resolving function dependency argument. |
||
381 | * |
||
382 | * @param mixed $argument Dependency argument |
||
383 | */ |
||
384 | 2 | protected function buildResolverArgument($argument, $textFunction = 'newLine') |
|
385 | { |
||
386 | // This is a dependency which invokes resolving function |
||
387 | 2 | if (is_string($argument)) { |
|
388 | 2 | if (array_key_exists($argument, $this->classesMetadata)) { |
|
389 | // Call container logic for this dependency |
||
390 | 2 | $this->generator->$textFunction('$this->' . $this->resolverFunction . '(\'' . $argument . '\')'); |
|
391 | 2 | } elseif (array_key_exists($argument, $this->classAliases)) { |
|
392 | // Call container logic for this dependency |
||
393 | $this->generator->$textFunction('$this->' . $this->resolverFunction . '(\'' . $argument . '\')'); |
||
394 | } else { // String variable |
||
395 | 2 | $this->generator->$textFunction()->stringValue($argument); |
|
396 | } |
||
397 | 2 | } elseif (is_array($argument)) { // Dependency value is array |
|
398 | 2 | $this->generator->$textFunction()->arrayValue($argument); |
|
399 | } |
||
400 | 2 | } |
|
401 | |||
402 | /** |
||
403 | * Generate reflection class for private/protected methods or properties |
||
404 | * in current scope. |
||
405 | * |
||
406 | * @param string $className Reflection class source class name |
||
407 | * @param PropertyMetadata[] $propertiesMetadata Properties metadata |
||
408 | * @param MethodMetadata[] $methodsMetadata Methods metadata |
||
409 | * @param string $reflectionVariable Reflection class variable name |
||
410 | */ |
||
411 | 2 | protected function buildReflectionClass(string $className, array $propertiesMetadata, array $methodsMetadata, string $reflectionVariable) |
|
445 | |||
446 | /** |
||
447 | * Build resolving property injection declaration. |
||
448 | * |
||
449 | * @param string $propertyName Target property name |
||
450 | * @param string $dependency Dependency class name |
||
451 | * @param string $containerVariable Container declaration variable name |
||
452 | * @param string $reflectionVariable Reflection class variable name |
||
453 | * @param bool $isPublic Flag if property is public |
||
454 | */ |
||
455 | 2 | protected function buildResolverPropertyDeclaration( |
|
487 | |||
488 | /** |
||
489 | * Build resolving method injection declaration. |
||
490 | * |
||
491 | * @param array $dependencies Collection of method dependencies |
||
492 | * @param string $methodName Method name |
||
493 | * @param string $containerVariable Container declaration variable name |
||
494 | * @param string $reflectionVariable Reflection class variable name |
||
495 | * @param bool $isPublic Flag if method is public |
||
496 | */ |
||
497 | 2 | protected function buildResolverMethodDeclaration( |
|
535 | } |
||
536 |
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: