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 ContainerBuilder 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 ContainerBuilder, and based on these observations, apply Extract Interface, too.
| 1 | <?php declare(strict_types = 1); |
||
| 21 | class ContainerBuilder |
||
| 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 $classMetadata = []; |
||
| 43 | |||
| 44 | /** @var FileManagerInterface */ |
||
| 45 | protected $fileManger; |
||
| 46 | |||
| 47 | /** @var ResolverInterface */ |
||
| 48 | protected $classResolver; |
||
| 49 | |||
| 50 | /** @var Generator */ |
||
| 51 | protected $generator; |
||
| 52 | |||
| 53 | /** @var string Resolver function name */ |
||
| 54 | protected $resolverFunction; |
||
| 55 | |||
| 56 | /** |
||
| 57 | * Container constructor. |
||
| 58 | * |
||
| 59 | * @param FileManagerInterface $fileManger |
||
| 60 | * @param ResolverInterface $classResolver |
||
| 61 | * @param Generator $generator |
||
| 62 | */ |
||
| 63 | 11 | public function __construct( |
|
| 73 | |||
| 74 | /** |
||
| 75 | * Load classes from paths. |
||
| 76 | * |
||
| 77 | * @param array $paths Paths for importing |
||
| 78 | * |
||
| 79 | * @return $this |
||
| 80 | */ |
||
| 81 | 1 | public function loadFromPaths(array $paths) |
|
| 91 | |||
| 92 | /** |
||
| 93 | * Load classes from class names collection. |
||
| 94 | * |
||
| 95 | * @param string[] $classes Collection of class names for resolving |
||
| 96 | * |
||
| 97 | * @return $this |
||
| 98 | */ |
||
| 99 | 11 | public function loadFromClassNames(array $classes) |
|
| 113 | |||
| 114 | /** |
||
| 115 | * Find class names defined in PHP code. |
||
| 116 | * |
||
| 117 | * @param string $php PHP code for scanning |
||
| 118 | * |
||
| 119 | * @return string[] Collection of found class names in php code |
||
| 120 | */ |
||
| 121 | 2 | protected function getDefinedClasses($php) : array |
|
| 141 | |||
| 142 | /** |
||
| 143 | * Load classes from PHP code. |
||
| 144 | * |
||
| 145 | * @param string $php PHP code |
||
| 146 | * |
||
| 147 | * @return $this |
||
| 148 | */ |
||
| 149 | 1 | public function loadFromCode($php) |
|
| 159 | |||
| 160 | /** |
||
| 161 | * Build container class. |
||
| 162 | * |
||
| 163 | * @param string|null $containerClass Container class name |
||
| 164 | * @param string $namespace Name space |
||
| 165 | * |
||
| 166 | * @return string Generated Container class code |
||
| 167 | * @throws \InvalidArgumentException |
||
| 168 | */ |
||
| 169 | 7 | public function build($containerClass = 'Container', $namespace = '') |
|
| 220 | |||
| 221 | /** |
||
| 222 | * Build dependency resolving function. |
||
| 223 | * |
||
| 224 | * @param string $functionName Function name |
||
| 225 | * |
||
| 226 | * @throws \InvalidArgumentException |
||
| 227 | */ |
||
| 228 | 7 | protected function buildDependencyResolver($functionName) |
|
| 242 | |||
| 243 | /** |
||
| 244 | * Generate logic conditions and their implementation for container and its delegates. |
||
| 245 | * |
||
| 246 | * @param string $inputVariable Input condition parameter variable name |
||
| 247 | * @param bool|false $started Flag if condition branching has been started |
||
| 248 | */ |
||
| 249 | 7 | public function generateConditions($inputVariable = '$alias', $started = false) |
|
| 331 | |||
| 332 | /** |
||
| 333 | * Build resolving function condition. |
||
| 334 | * |
||
| 335 | * @param string $inputVariable Condition variable |
||
| 336 | * @param string $className |
||
| 337 | * @param string|null $alias |
||
| 338 | * |
||
| 339 | * @return string Condition code |
||
| 340 | */ |
||
| 341 | 7 | protected function buildResolverCondition(string $inputVariable, string $className, string $alias = null) : string |
|
| 352 | |||
| 353 | /** |
||
| 354 | * Get valid class methods metadata. |
||
| 355 | * |
||
| 356 | * @param MethodMetadata[] $classMethodsMetadata All class methods metadata |
||
| 357 | * |
||
| 358 | * @return array Valid class methods metadata |
||
| 359 | */ |
||
| 360 | 7 | protected function getValidClassMethodsMetadata(array $classMethodsMetadata) |
|
| 373 | |||
| 374 | /** |
||
| 375 | * Get valid class properties metadata. |
||
| 376 | * |
||
| 377 | * @param PropertyMetadata[] $classPropertiesMetadata All class properties metadata |
||
| 378 | * |
||
| 379 | * @return array Valid class properties metadata |
||
| 380 | */ |
||
| 381 | 7 | protected function getValidClassPropertiesMetadata(array $classPropertiesMetadata) |
|
| 394 | |||
| 395 | /** |
||
| 396 | * Build resolving function class block. |
||
| 397 | * |
||
| 398 | * @param string $className Class name for new instance creation |
||
| 399 | */ |
||
| 400 | 7 | protected function buildResolvingClassDeclaration(string $className) |
|
| 404 | |||
| 405 | /** |
||
| 406 | * Build constructor arguments injection. |
||
| 407 | * |
||
| 408 | * @param MethodMetadata[] $methodsMetaData |
||
| 409 | */ |
||
| 410 | 7 | protected function buildConstructorDependencies(array $methodsMetaData) |
|
| 439 | |||
| 440 | /** |
||
| 441 | * Build resolving function dependency argument. |
||
| 442 | * |
||
| 443 | * @param mixed $argument Dependency argument |
||
| 444 | */ |
||
| 445 | 7 | protected function buildResolverArgument($argument, $textFunction = 'newLine') |
|
| 446 | { |
||
| 447 | // This is a dependency which invokes resolving function |
||
| 448 | 7 | if (array_key_exists($argument, $this->classMetadata)) { |
|
| 449 | // Call container logic for this dependency |
||
| 450 | 7 | $this->generator->$textFunction('$this->' . $this->resolverFunction . '(\'' . $argument . '\')'); |
|
| 451 | } elseif (is_string($argument)) { // String variable |
||
| 452 | $this->generator->$textFunction()->stringValue($argument); |
||
| 453 | } elseif (is_array($argument)) { // Dependency value is array |
||
| 454 | $this->generator->$textFunction()->arrayValue($argument); |
||
| 455 | } |
||
| 456 | 7 | } |
|
| 457 | |||
| 458 | /** |
||
| 459 | * Generate reflection class for private/protected methods or properties |
||
| 460 | * in current scope. |
||
| 461 | * |
||
| 462 | * @param string $className Reflection class source class name |
||
| 463 | * @param PropertyMetadata[] $propertiesMetadata Properties metadata |
||
| 464 | * @param MethodMetadata[] $methodsMetadata Methods metadata |
||
| 465 | * @param string $reflectionVariable Reflection class variable name |
||
| 466 | */ |
||
| 467 | 7 | protected function buildReflectionClass(string $className, array $propertiesMetadata, array $methodsMetadata, string $reflectionVariable) |
|
| 506 | |||
| 507 | /** |
||
| 508 | * Build resolving property injection declaration. |
||
| 509 | * |
||
| 510 | * @param string $propertyName Target property name |
||
| 511 | * @param string $dependency Dependency class name |
||
| 512 | * @param string $containerVariable Container declaration variable name |
||
| 513 | * @param string $reflectionVariable Reflection class variable name |
||
| 514 | * @param bool $isPublic Flag if property is public |
||
| 515 | */ |
||
| 516 | 7 | protected function buildResolverPropertyDeclaration( |
|
| 548 | |||
| 549 | /** |
||
| 550 | * Build resolving method injection declaration. |
||
| 551 | * |
||
| 552 | * @param array $dependencies Collection of method dependencies |
||
| 553 | * @param string $methodName Method name |
||
| 554 | * @param string $containerVariable Container declaration variable name |
||
| 555 | * @param string $reflectionVariable Reflection class variable name |
||
| 556 | * @param bool $isPublic Flag if method is public |
||
| 557 | */ |
||
| 558 | 7 | protected function buildResolverMethodDeclaration( |
|
| 596 | } |
||
| 597 |