| Conditions | 24 |
| Paths | 1530 |
| Total Lines | 85 |
| Code Lines | 44 |
| Lines | 25 |
| Ratio | 29.41 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php declare(strict_types=1); |
||
| 161 | public function replaceInterfaceDependencies(array $metadataCollection) { |
||
| 162 | |||
| 163 | $list = []; |
||
| 164 | $listPath = []; |
||
| 165 | /** @var ClassMetadata $classMetadata */ |
||
| 166 | foreach ($metadataCollection as $classPath => $classMetadata) { |
||
| 167 | $list[$classMetadata->className] = $classMetadata; |
||
| 168 | $listPath[$classMetadata->name ?? $classMetadata->className] = $classPath; |
||
| 169 | } |
||
| 170 | $metadataCollection = $list; |
||
| 171 | |||
| 172 | // Gather all interface implementations |
||
| 173 | $implementsByAlias = $implementsByAlias ?? []; |
||
| 174 | foreach (get_declared_classes() as $class) { |
||
| 175 | $classImplements = class_implements($class); |
||
| 176 | foreach (get_declared_interfaces() as $interface) { |
||
| 177 | if (in_array($interface, $classImplements, true)) { |
||
| 178 | if (array_key_exists($class, $metadataCollection)) { |
||
| 179 | $implementsByAlias[$interface][] = $metadataCollection[$class]->name; |
||
| 180 | } |
||
| 181 | } |
||
| 182 | } |
||
| 183 | } |
||
| 184 | |||
| 185 | // Gather all class implementations |
||
| 186 | $serviceAliasesByClass = $serviceAliasesByClass ?? []; |
||
| 187 | foreach (get_declared_classes() as $class) { |
||
| 188 | if (array_key_exists($class, $metadataCollection)) { |
||
| 189 | $serviceAliasesByClass[$class][] = $metadataCollection[$class]->name; |
||
| 190 | } |
||
| 191 | } |
||
| 192 | |||
| 193 | /** |
||
| 194 | * TODO: now we need to implement not forcing to load fixed dependencies into modules |
||
| 195 | * to give ability to change constructors and inject old variable into properties |
||
| 196 | * and them after refactoring remove them. With this we can only specify needed dependencies |
||
| 197 | * in new modules, and still have old ones working. |
||
| 198 | */ |
||
| 199 | |||
| 200 | foreach ($metadataCollection as $alias => $metadata) { |
||
| 201 | View Code Duplication | foreach ($metadata->propertiesMetadata as $property => $propertyMetadata) { |
|
| 202 | if (is_string($propertyMetadata->dependency)) { |
||
| 203 | $dependency = $propertyMetadata->dependency; |
||
| 204 | if (array_key_exists($dependency, $implementsByAlias)) { |
||
| 205 | $propertyMetadata->dependency = $implementsByAlias[$dependency][0]; |
||
| 206 | } elseif (array_key_exists($dependency, $serviceAliasesByClass)) { |
||
| 207 | $propertyMetadata->dependency = $serviceAliasesByClass[$dependency][0]; |
||
| 208 | } else { |
||
| 209 | |||
| 210 | } |
||
| 211 | } |
||
| 212 | } |
||
| 213 | |||
| 214 | // Iterate constructor arguments to preserve arguments order and inject dependencies |
||
| 215 | $reflectionClass = new \ReflectionClass($metadata->className); |
||
| 216 | // Check if instance has a constructor or it instance of external module |
||
| 217 | if ($reflectionClass->hasMethod('__construct') && is_subclass_of($metadata->className, ExternalModule::class)) { |
||
| 218 | foreach ((new \ReflectionMethod($metadata->className, '__construct'))->getParameters() as $parameter) { |
||
| 219 | if ($parameter->getName() === 'path') { |
||
| 220 | $metadata->methodsMetadata['__construct']->dependencies['path'] = dirname($listPath[$metadata->name ?? $metadata->className]); |
||
| 221 | } elseif ($parameter->getName() === 'resources') { |
||
| 222 | $metadata->methodsMetadata['__construct']->dependencies['resources'] = ResourceMap::class; |
||
| 223 | } elseif ($parameter->getName() === 'system') { |
||
| 224 | $metadata->methodsMetadata['__construct']->dependencies['system'] = 'core'; |
||
| 225 | } |
||
| 226 | } |
||
| 227 | } |
||
| 228 | |||
| 229 | foreach ($metadata->methodsMetadata as $method => $methodMetadata) { |
||
| 230 | View Code Duplication | foreach ($methodMetadata->dependencies as $argument => $dependency) { |
|
| 231 | if (is_string($dependency)) { |
||
| 232 | if (array_key_exists($dependency, $implementsByAlias)) { |
||
| 233 | $methodMetadata->dependencies[$argument] = $implementsByAlias[$dependency][0]; |
||
| 234 | //$methodMetadata->parametersMetadata[$argument]->dependency = $implementsByAlias[$dependency][0]; |
||
| 235 | } elseif (array_key_exists($dependency, $serviceAliasesByClass)) { |
||
| 236 | $methodMetadata->dependencies[$argument] = $serviceAliasesByClass[$dependency][0]; |
||
| 237 | //$methodMetadata->parametersMetadata[$argument]->dependency = $serviceAliasesByClass[$dependency][0]; |
||
| 238 | } else { |
||
| 239 | |||
| 240 | } |
||
| 241 | } |
||
| 242 | } |
||
| 243 | } |
||
| 244 | } |
||
| 245 | } |
||
| 246 | |||
| 300 |