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 implements ContainerBuilderInterface |
||
22 | { |
||
23 | /** Controller classes scope name */ |
||
24 | const SCOPE_CONTROLLER = 'controllers'; |
||
25 | |||
26 | /** Service classes scope name */ |
||
27 | const SCOPE_SERVICES = 'service'; |
||
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) |
|
66 | |||
67 | /** |
||
68 | * Build container class. |
||
69 | * |
||
70 | * @param array $classesMetadata |
||
71 | * @param string|null $containerClass Container class name |
||
72 | * @param string $namespace Name space |
||
73 | * |
||
74 | * @return string Generated Container class code |
||
75 | */ |
||
76 | 2 | public function build(array $classesMetadata, $containerClass = 'Container', $namespace = '') |
|
77 | { |
||
78 | 2 | $this->classesMetadata = $classesMetadata; |
|
79 | 2 | $this->processClassMetadata($classesMetadata); |
|
80 | |||
81 | // Build dependency injection container function name |
||
82 | 2 | $this->resolverFunction = uniqid(self::DI_FUNCTION_PREFIX); |
|
83 | |||
84 | 2 | $containerDependencies = []; |
|
85 | 2 | foreach ($classesMetadata as $classMetadata) { |
|
86 | 2 | $className = $classMetadata->className; |
|
87 | // Store inner dependencies |
||
88 | 2 | if (array_key_exists('__construct', $classMetadata->methodsMetadata)) { |
|
89 | 2 | $containerDependencies[$className] = array_values($classMetadata->methodsMetadata['__construct']->dependencies ?? []); |
|
90 | } |
||
91 | } |
||
92 | |||
93 | 2 | $this->generator |
|
94 | 2 | ->text('<?php declare(strict_types = 1);') |
|
95 | 2 | ->newLine() |
|
96 | 2 | ->defNamespace($namespace) |
|
97 | 2 | ->multiComment(['Application container']) |
|
98 | 2 | ->defClass($containerClass, '\\' . Container::class) |
|
99 | 2 | ->multiComment(['@var array Collection of service instances']) |
|
100 | 2 | ->defClassFunction('__construct', 'public', [], ['Container constructor']) |
|
101 | 2 | ->newLine('$this->dependencies = ')->arrayValue($containerDependencies)->text(';') |
|
102 | 2 | ->newLine('$this->aliases = ')->arrayValue($this->classAliases)->text(';') |
|
103 | 2 | ->newLine('$this->scopes = ')->arrayValue($this->scopes)->text(';') |
|
104 | 2 | ->newLine('$this->services = ')->arrayValue($this->scopes[self::SCOPE_SERVICES])->text(';') |
|
105 | 2 | ->endClassFunction() |
|
106 | 2 | ->defClassFunction('logic', 'protected', ['$dependency'], ['{@inheritdoc}']) |
|
107 | 2 | ->newLine('return $this->' . $this->resolverFunction . '($dependency);') |
|
108 | 2 | ->endClassFunction(); |
|
109 | |||
110 | 2 | foreach ($classesMetadata as $classMetadata) { |
|
111 | 2 | $className = $classMetadata->className; |
|
112 | 2 | $dependencyName = $classMetadata->name ?? $className; |
|
113 | |||
114 | // Generate camel case getter method |
||
115 | 2 | $camelMethodName = 'get' . str_replace(' ', '', ucwords(ucfirst(str_replace(['\\', '_'], ' ', $dependencyName)))); |
|
116 | |||
117 | 2 | $this->generator |
|
118 | 2 | ->defClassFunction($camelMethodName, 'public', [], ['@return ' . '\\'.ltrim($className, '\\') . ' Get ' . $dependencyName . ' instance']) |
|
119 | 2 | ->newLine('return $this->' . $this->resolverFunction . '(\'' . $dependencyName . '\');') |
|
120 | 2 | ->endClassFunction(); |
|
121 | } |
||
122 | |||
123 | // Build di container function and add to container class and return class code |
||
124 | 2 | $this->buildDependencyResolver($this->resolverFunction); |
|
125 | |||
126 | 2 | return $this->generator |
|
127 | 2 | ->endClass() |
|
128 | 2 | ->flush(); |
|
129 | } |
||
130 | |||
131 | /** |
||
132 | * Read class metadata and fill internal collections. |
||
133 | * |
||
134 | * @param ClassMetadata[] $classesMetadata |
||
135 | */ |
||
136 | 2 | public function processClassMetadata(array $classesMetadata) |
|
137 | { |
||
138 | // Read all classes in given file |
||
139 | 2 | foreach ($classesMetadata as $classMetadata) { |
|
140 | // Store by metadata name as alias |
||
141 | 2 | $this->classAliases[$classMetadata->name] = $classMetadata->className; |
|
142 | |||
143 | // Store class in defined scopes |
||
144 | 2 | foreach ($classMetadata->scopes as $scope) { |
|
145 | 2 | $this->scopes[$scope][$classMetadata->name] = $classMetadata->className; |
|
146 | } |
||
147 | } |
||
148 | 2 | } |
|
149 | |||
150 | /** |
||
151 | * Build dependency resolving function. |
||
152 | * |
||
153 | * @param string $functionName Function name |
||
154 | * |
||
155 | * @throws \InvalidArgumentException |
||
156 | */ |
||
157 | 2 | protected function buildDependencyResolver($functionName) |
|
158 | { |
||
159 | 2 | $inputVariable = '$aliasOrClassName'; |
|
160 | 2 | $this->generator |
|
161 | 2 | ->defClassFunction($functionName, 'protected', [$inputVariable], ['Dependency resolving function']) |
|
162 | //->defVar('static ' . self::DI_FUNCTION_SERVICES . ' = []') |
||
163 | 2 | ->newLine(); |
|
164 | |||
165 | // Generate all container and delegate conditions |
||
166 | 2 | $this->generateConditions($inputVariable, false); |
|
167 | |||
168 | // Add method not found |
||
169 | 2 | $this->generator->endIfCondition()->endFunction(); |
|
170 | 2 | } |
|
171 | |||
172 | /** |
||
173 | * Generate logic conditions and their implementation for container and its delegates. |
||
174 | * |
||
175 | * @param string $inputVariable Input condition parameter variable name |
||
176 | * @param bool|false $started Flag if condition branching has been started |
||
177 | */ |
||
178 | 2 | public function generateConditions($inputVariable = '$alias', $started = false) |
|
179 | { |
||
180 | // Iterate all container dependencies |
||
181 | 2 | foreach ($this->classesMetadata as $classMetadata) { |
|
182 | 2 | $className = $classMetadata->className; |
|
183 | // Generate condition statement to define if this class is needed |
||
184 | 2 | $conditionFunc = !$started ? 'defIfCondition' : 'defElseIfCondition'; |
|
185 | |||
186 | // Output condition branch |
||
187 | 2 | $this->generator->$conditionFunc( |
|
188 | 2 | $this->buildResolverCondition($inputVariable, $className, $classMetadata->name) |
|
189 | ); |
||
190 | |||
191 | // Define if this class has service scope |
||
192 | 2 | $isService = in_array($className, $this->scopes[self::SCOPE_SERVICES], true); |
|
193 | |||
194 | /** @var MethodMetadata[] Gather only valid method for container */ |
||
195 | 2 | $classValidMethods = $this->getValidClassMethodsMetadata($classMetadata->methodsMetadata); |
|
196 | |||
197 | /** @var PropertyMetadata[] Gather only valid property for container */ |
||
198 | 2 | $classValidProperties = $this->getValidClassPropertiesMetadata($classMetadata->propertiesMetadata); |
|
199 | |||
200 | // Define class or service variable |
||
201 | 2 | $staticContainerName = $isService |
|
202 | 2 | ? '$this->' . self::DI_FUNCTION_SERVICES . '[\'' . $classMetadata->name . '\']' |
|
203 | 2 | : '$temp'; |
|
204 | |||
205 | 2 | if ($isService) { |
|
206 | // Check if dependency was instantiated |
||
207 | 2 | $this->generator->defIfCondition('!array_key_exists(\'' . $classMetadata->name . '\', $this->' . self::DI_FUNCTION_SERVICES . ')'); |
|
208 | } |
||
209 | |||
210 | 2 | if (count($classValidMethods) || count($classValidProperties)) { |
|
211 | 2 | $this->generator->newLine($staticContainerName . ' = '); |
|
212 | 2 | $this->buildResolvingClassDeclaration($className); |
|
213 | 2 | $this->buildConstructorDependencies($classMetadata->methodsMetadata); |
|
214 | |||
215 | // Internal scope reflection variable |
||
216 | 2 | $reflectionVariable = '$reflectionClass'; |
|
217 | |||
218 | 2 | $this->buildReflectionClass($className, $classValidProperties, $classValidMethods, $reflectionVariable); |
|
219 | |||
220 | // Process class properties |
||
221 | 2 | foreach ($classValidProperties as $property) { |
|
222 | // If such property has the dependency |
||
223 | 2 | if ($property->dependency) { |
|
224 | // Set value via refection |
||
225 | 2 | $this->buildResolverPropertyDeclaration( |
|
226 | 2 | $property->name, |
|
227 | 2 | $property->dependency, |
|
228 | $staticContainerName, |
||
229 | $reflectionVariable, |
||
230 | 2 | $property->isPublic |
|
231 | ); |
||
232 | } |
||
233 | } |
||
234 | |||
235 | /** @var MethodMetadata $methodMetadata */ |
||
236 | 2 | foreach ($classValidMethods as $methodName => $methodMetadata) { |
|
237 | 2 | $this->buildResolverMethodDeclaration( |
|
238 | 2 | $methodMetadata->dependencies, |
|
239 | $methodName, |
||
240 | $staticContainerName, |
||
241 | $reflectionVariable, |
||
242 | 2 | $methodMetadata->isPublic |
|
243 | ); |
||
244 | } |
||
245 | |||
246 | 2 | if ($isService) { |
|
247 | 2 | $this->generator->endIfCondition(); |
|
248 | } |
||
249 | |||
250 | 2 | $this->generator->newLine()->newLine('return ' . $staticContainerName . ';'); |
|
251 | } else { |
||
252 | 2 | if ($isService) { |
|
253 | $this->generator->newLine($staticContainerName.' = '); |
||
254 | $this->buildResolvingClassDeclaration($className); |
||
255 | $this->buildConstructorDependencies($classMetadata->methodsMetadata); |
||
256 | $this->generator->endIfCondition()->newLine('return ' . $staticContainerName . ';'); |
||
257 | } else { |
||
258 | 2 | $this->generator->newLine('return '); |
|
259 | 2 | $this->buildResolvingClassDeclaration($className); |
|
260 | 2 | $this->buildConstructorDependencies($classMetadata->methodsMetadata); |
|
261 | } |
||
262 | |||
263 | } |
||
264 | |||
265 | // Set flag that condition is started |
||
266 | 2 | $started = true; |
|
267 | } |
||
268 | 2 | } |
|
269 | |||
270 | /** |
||
271 | * Build resolving function condition. |
||
272 | * |
||
273 | * @param string $inputVariable Condition variable |
||
274 | * @param string $className |
||
275 | * @param string|null $alias |
||
276 | * |
||
277 | * @return string Condition code |
||
278 | */ |
||
279 | 2 | protected function buildResolverCondition(string $inputVariable, string $className, string $alias = null) : string |
|
290 | |||
291 | /** |
||
292 | * Get valid class methods metadata. |
||
293 | * |
||
294 | * @param MethodMetadata[] $classMethodsMetadata All class methods metadata |
||
295 | * |
||
296 | * @return array Valid class methods metadata |
||
297 | */ |
||
298 | 2 | protected function getValidClassMethodsMetadata(array $classMethodsMetadata) |
|
311 | |||
312 | /** |
||
313 | * Get valid class properties metadata. |
||
314 | * |
||
315 | * @param PropertyMetadata[] $classPropertiesMetadata All class properties metadata |
||
316 | * |
||
317 | * @return array Valid class properties metadata |
||
318 | */ |
||
319 | 2 | protected function getValidClassPropertiesMetadata(array $classPropertiesMetadata) |
|
332 | |||
333 | /** |
||
334 | * Build resolving function class block. |
||
335 | * |
||
336 | * @param string $className Class name for new instance creation |
||
337 | */ |
||
338 | 2 | protected function buildResolvingClassDeclaration(string $className) |
|
342 | |||
343 | /** |
||
344 | * Build constructor arguments injection. |
||
345 | * |
||
346 | * @param MethodMetadata[] $methodsMetaData |
||
347 | */ |
||
348 | 2 | protected function buildConstructorDependencies(array $methodsMetaData) |
|
377 | |||
378 | /** |
||
379 | * Build resolving function dependency argument. |
||
380 | * |
||
381 | * @param mixed $argument Dependency argument |
||
382 | * |
||
383 | * @throws \InvalidArgumentException On invalid argument type |
||
384 | */ |
||
385 | 2 | protected function buildResolverArgument($argument, $textFunction = 'newLine') |
|
406 | |||
407 | /** |
||
408 | * Generate reflection class for private/protected methods or properties |
||
409 | * in current scope. |
||
410 | * |
||
411 | * @param string $className Reflection class source class name |
||
412 | * @param PropertyMetadata[] $propertiesMetadata Properties metadata |
||
413 | * @param MethodMetadata[] $methodsMetadata Methods metadata |
||
414 | * @param string $reflectionVariable Reflection class variable name |
||
415 | */ |
||
416 | 2 | protected function buildReflectionClass(string $className, array $propertiesMetadata, array $methodsMetadata, string $reflectionVariable) |
|
450 | |||
451 | /** |
||
452 | * Build resolving property injection declaration. |
||
453 | * |
||
454 | * @param string $propertyName Target property name |
||
455 | * @param string $dependency Dependency class name |
||
456 | * @param string $containerVariable Container declaration variable name |
||
457 | * @param string $reflectionVariable Reflection class variable name |
||
458 | * @param bool $isPublic Flag if property is public |
||
459 | */ |
||
460 | 2 | protected function buildResolverPropertyDeclaration( |
|
492 | |||
493 | /** |
||
494 | * Build resolving method injection declaration. |
||
495 | * |
||
496 | * @param array $dependencies Collection of method dependencies |
||
497 | * @param string $methodName Method name |
||
498 | * @param string $containerVariable Container declaration variable name |
||
499 | * @param string $reflectionVariable Reflection class variable name |
||
500 | * @param bool $isPublic Flag if method is public |
||
501 | */ |
||
502 | 2 | protected function buildResolverMethodDeclaration( |
|
540 | } |
||
541 |
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.
Consider the following example. The parameter
$italy
is not defined by the methodfinale(...)
.The most likely cause is that the parameter was removed, but the annotation was not.