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 | 1 | public function __construct(Generator $generator, array $classMetadata) |
|
| 69 | |||
| 70 | /** |
||
| 71 | * Read class metadata and fill internal collections. |
||
| 72 | * |
||
| 73 | * @param ClassMetadata[] $classesMetadata |
||
| 74 | */ |
||
| 75 | 1 | 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 | 1 | 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 | 1 | 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 | 1 | public function generateConditions($inputVariable = '$alias', $started = false) |
|
| 181 | { |
||
| 182 | // Iterate all container dependencies |
||
| 183 | 1 | foreach ($this->classesMetadata as $classMetadata) { |
|
| 184 | 1 | $className = $classMetadata->className; |
|
| 185 | // Generate condition statement to define if this class is needed |
||
| 186 | 1 | $conditionFunc = !$started ? 'defIfCondition' : 'defElseIfCondition'; |
|
| 187 | |||
| 188 | // Output condition branch |
||
| 189 | 1 | $this->generator->$conditionFunc( |
|
| 190 | 1 | $this->buildResolverCondition($inputVariable, $className, $classMetadata->name) |
|
| 191 | ); |
||
| 192 | |||
| 193 | // Define if this class has service scope |
||
| 194 | 1 | $isService = in_array($className, $this->scopes[self::SCOPE_SERVICES], true); |
|
| 195 | |||
| 196 | /** @var MethodMetadata[] Gather only valid method for container */ |
||
| 197 | 1 | $classValidMethods = $this->getValidClassMethodsMetadata($classMetadata->methodsMetadata); |
|
| 198 | |||
| 199 | /** @var PropertyMetadata[] Gather only valid property for container */ |
||
| 200 | 1 | $classValidProperties = $this->getValidClassPropertiesMetadata($classMetadata->propertiesMetadata); |
|
| 201 | |||
| 202 | // Define class or service variable |
||
| 203 | 1 | $staticContainerName = $isService |
|
| 204 | ? self::DI_FUNCTION_SERVICES . '[\'' . $classMetadata->name . '\']' |
||
| 205 | 1 | : '$temp'; |
|
| 206 | |||
| 207 | 1 | if ($isService) { |
|
| 208 | // Check if dependency was instantiated |
||
| 209 | $this->generator->defIfCondition('!array_key_exists(\'' . $className . '\', ' . self::DI_FUNCTION_SERVICES . ')'); |
||
| 210 | } |
||
| 211 | |||
| 212 | 1 | if (count($classValidMethods) || count($classValidProperties)) { |
|
| 213 | $this->generator->newLine($staticContainerName . ' = '); |
||
| 214 | $this->buildResolvingClassDeclaration($className); |
||
| 215 | $this->buildConstructorDependencies($classMetadata->methodsMetadata); |
||
| 216 | |||
| 217 | // Internal scope reflection variable |
||
| 218 | $reflectionVariable = '$reflectionClass'; |
||
| 219 | |||
| 220 | $this->buildReflectionClass($className, $classValidProperties, $classValidMethods, $reflectionVariable); |
||
| 221 | |||
| 222 | // Process class properties |
||
| 223 | foreach ($classValidProperties as $property) { |
||
| 224 | // If such property has the dependency |
||
| 225 | if ($property->dependency) { |
||
| 226 | // Set value via refection |
||
| 227 | $this->buildResolverPropertyDeclaration( |
||
| 228 | $property->name, |
||
| 229 | $property->dependency, |
||
| 230 | $staticContainerName, |
||
| 231 | $reflectionVariable, |
||
| 232 | $property->isPublic |
||
| 233 | ); |
||
| 234 | } |
||
| 235 | } |
||
| 236 | |||
| 237 | /** @var MethodMetadata $methodMetadata */ |
||
| 238 | foreach ($classValidMethods as $methodName => $methodMetadata) { |
||
| 239 | $this->buildResolverMethodDeclaration( |
||
| 240 | $methodMetadata->dependencies, |
||
| 241 | $methodName, |
||
| 242 | $staticContainerName, |
||
| 243 | $reflectionVariable, |
||
| 244 | $methodMetadata->isPublic |
||
| 245 | ); |
||
| 246 | } |
||
| 247 | |||
| 248 | if ($isService) { |
||
| 249 | $this->generator->endIfCondition(); |
||
| 250 | } |
||
| 251 | |||
| 252 | $this->generator->newLine()->newLine('return ' . $staticContainerName . ';'); |
||
| 253 | } else { |
||
| 254 | 1 | $this->generator->newLine('return '); |
|
| 255 | 1 | $this->buildResolvingClassDeclaration($className); |
|
| 256 | 1 | $this->buildConstructorDependencies($classMetadata->methodsMetadata); |
|
| 257 | |||
| 258 | 1 | if ($isService) { |
|
| 259 | $this->generator->endIfCondition()->newLine('return ' . $staticContainerName . ';'); |
||
| 260 | } |
||
| 261 | |||
| 262 | } |
||
| 263 | |||
| 264 | // Set flag that condition is started |
||
| 265 | 1 | $started = true; |
|
| 266 | } |
||
| 267 | 1 | } |
|
| 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 | 1 | 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 | 1 | 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 | 1 | protected function getValidClassPropertiesMetadata(array $classPropertiesMetadata) |
|
| 319 | { |
||
| 320 | /** @var PropertyMetadata[] Gather only valid property for container */ |
||
| 321 | 1 | $classValidProperties = []; |
|
| 322 | 1 | foreach ($classPropertiesMetadata as $propertyName => $propertyMetadata) { |
|
| 323 | // Skip constructor method and empty dependencies |
||
| 324 | if ($propertyMetadata->dependency) { |
||
| 325 | $classValidProperties[$propertyName] = $propertyMetadata; |
||
| 326 | } |
||
| 327 | } |
||
| 328 | |||
| 329 | 1 | return $classValidProperties; |
|
| 330 | } |
||
| 331 | |||
| 332 | /** |
||
| 333 | * Build resolving function class block. |
||
| 334 | * |
||
| 335 | * @param string $className Class name for new instance creation |
||
| 336 | */ |
||
| 337 | 1 | protected function buildResolvingClassDeclaration(string $className) |
|
| 341 | |||
| 342 | /** |
||
| 343 | * Build constructor arguments injection. |
||
| 344 | * |
||
| 345 | * @param MethodMetadata[] $methodsMetaData |
||
| 346 | */ |
||
| 347 | 1 | protected function buildConstructorDependencies(array $methodsMetaData) |
|
| 376 | |||
| 377 | /** |
||
| 378 | * Build resolving function dependency argument. |
||
| 379 | * |
||
| 380 | * @param mixed $argument Dependency argument |
||
| 381 | */ |
||
| 382 | 1 | 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 | 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 | protected function buildResolverPropertyDeclaration( |
||
| 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 | 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: