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; |
||
| 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 = '') |
|
| 151 | |||
| 152 | /** |
||
| 153 | * Build dependency resolving function. |
||
| 154 | * |
||
| 155 | * @param string $functionName Function name |
||
| 156 | * |
||
| 157 | * @throws \InvalidArgumentException |
||
| 158 | */ |
||
| 159 | 2 | protected function buildDependencyResolver($functionName) |
|
| 173 | |||
| 174 | /** |
||
| 175 | * Generate logic conditions and their implementation for container and its delegates. |
||
| 176 | * |
||
| 177 | * @param string $inputVariable Input condition parameter variable name |
||
| 178 | * @param bool|false $started Flag if condition branching has been started |
||
| 179 | */ |
||
| 180 | 2 | public function generateConditions($inputVariable = '$alias', $started = false) |
|
| 268 | |||
| 269 | /** |
||
| 270 | * Build resolving function condition. |
||
| 271 | * |
||
| 272 | * @param string $inputVariable Condition variable |
||
| 273 | * @param string $className |
||
| 274 | * @param string|null $alias |
||
| 275 | * |
||
| 276 | * @return string Condition code |
||
| 277 | */ |
||
| 278 | 2 | protected function buildResolverCondition(string $inputVariable, string $className, string $alias = null) : string |
|
| 289 | |||
| 290 | /** |
||
| 291 | * Get valid class methods metadata. |
||
| 292 | * |
||
| 293 | * @param MethodMetadata[] $classMethodsMetadata All class methods metadata |
||
| 294 | * |
||
| 295 | * @return array Valid class methods metadata |
||
| 296 | */ |
||
| 297 | 2 | protected function getValidClassMethodsMetadata(array $classMethodsMetadata) |
|
| 310 | |||
| 311 | /** |
||
| 312 | * Get valid class properties metadata. |
||
| 313 | * |
||
| 314 | * @param PropertyMetadata[] $classPropertiesMetadata All class properties metadata |
||
| 315 | * |
||
| 316 | * @return array Valid class properties metadata |
||
| 317 | */ |
||
| 318 | 2 | protected function getValidClassPropertiesMetadata(array $classPropertiesMetadata) |
|
| 331 | |||
| 332 | /** |
||
| 333 | * Build resolving function class block. |
||
| 334 | * |
||
| 335 | * @param string $className Class name for new instance creation |
||
| 336 | */ |
||
| 337 | 2 | protected function buildResolvingClassDeclaration(string $className) |
|
| 341 | |||
| 342 | /** |
||
| 343 | * Build constructor arguments injection. |
||
| 344 | * |
||
| 345 | * @param MethodMetadata[] $methodsMetaData |
||
| 346 | */ |
||
| 347 | 2 | protected function buildConstructorDependencies(array $methodsMetaData) |
|
| 376 | |||
| 377 | /** |
||
| 378 | * Build resolving function dependency argument. |
||
| 379 | * |
||
| 380 | * @param mixed $argument Dependency argument |
||
| 381 | */ |
||
| 382 | 2 | protected function buildResolverArgument($argument, $textFunction = 'newLine') |
|
| 397 | |||
| 398 | /** |
||
| 399 | * Generate reflection class for private/protected methods or properties |
||
| 400 | * in current scope. |
||
| 401 | * |
||
| 402 | * @param string $className Reflection class source class name |
||
| 403 | * @param PropertyMetadata[] $propertiesMetadata Properties metadata |
||
| 404 | * @param MethodMetadata[] $methodsMetadata Methods metadata |
||
| 405 | * @param string $reflectionVariable Reflection class variable name |
||
| 406 | */ |
||
| 407 | 1 | protected function buildReflectionClass(string $className, array $propertiesMetadata, array $methodsMetadata, string $reflectionVariable) |
|
| 446 | |||
| 447 | /** |
||
| 448 | * Build resolving property injection declaration. |
||
| 449 | * |
||
| 450 | * @param string $propertyName Target property name |
||
| 451 | * @param string $dependency Dependency class name |
||
| 452 | * @param string $containerVariable Container declaration variable name |
||
| 453 | * @param string $reflectionVariable Reflection class variable name |
||
| 454 | * @param bool $isPublic Flag if property is public |
||
| 455 | */ |
||
| 456 | 1 | protected function buildResolverPropertyDeclaration( |
|
| 457 | string $propertyName, |
||
| 458 | string $dependency, |
||
| 459 | string $containerVariable, |
||
| 460 | string $reflectionVariable, |
||
| 461 | bool $isPublic |
||
| 462 | ) |
||
| 463 | { |
||
| 464 | 1 | if ($isPublic) { |
|
| 465 | 1 | $this->generator |
|
| 466 | 1 | ->comment('Inject public dependency for $' . $propertyName) |
|
| 467 | 1 | ->newLine($containerVariable . '->' . $propertyName . ' = '); |
|
| 468 | 1 | $this->buildResolverArgument($dependency, 'text'); |
|
| 469 | 1 | $this->generator->text(';'); |
|
| 470 | } else { |
||
| 471 | 1 | $this->generator |
|
| 472 | 1 | ->comment('Inject private dependency for $' . $propertyName) |
|
| 473 | 1 | ->newLine('$property = ' . $reflectionVariable . '->getProperty(\'' . $propertyName . '\');') |
|
| 474 | 1 | ->newLine('$property->setAccessible(true);') |
|
| 475 | 1 | ->newLine('$property->setValue(') |
|
| 476 | 1 | ->increaseIndentation() |
|
| 477 | 1 | ->newLine($containerVariable . ','); |
|
| 478 | |||
| 479 | 1 | $this->buildResolverArgument($dependency); |
|
| 480 | |||
| 481 | 1 | $this->generator |
|
| 482 | 1 | ->decreaseIndentation() |
|
| 483 | 1 | ->newLine(');') |
|
| 484 | 1 | ->newLine('$property->setAccessible(false);') |
|
| 485 | 1 | ->newLine(); |
|
| 486 | } |
||
| 487 | 1 | } |
|
| 488 | |||
| 489 | /** |
||
| 490 | * Build resolving method injection declaration. |
||
| 491 | * |
||
| 492 | * @param array $dependencies Collection of method dependencies |
||
| 493 | * @param string $methodName Method name |
||
| 494 | * @param string $containerVariable Container declaration variable name |
||
| 495 | * @param string $reflectionVariable Reflection class variable name |
||
| 496 | * @param bool $isPublic Flag if method is public |
||
| 497 | */ |
||
| 498 | 1 | protected function buildResolverMethodDeclaration( |
|
| 536 | } |
||
| 537 |
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: