| Total Complexity | 59 |
| Total Lines | 413 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like Container 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.
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 Container, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 19 | class Container implements ContainerInterface |
||
| 20 | { |
||
| 21 | /** |
||
| 22 | * Config. |
||
| 23 | * |
||
| 24 | * @var iterable |
||
| 25 | */ |
||
| 26 | protected $config = []; |
||
| 27 | |||
| 28 | /** |
||
| 29 | * Service registry. |
||
| 30 | * |
||
| 31 | * @var array |
||
| 32 | */ |
||
| 33 | protected $service = []; |
||
| 34 | |||
| 35 | /** |
||
| 36 | * Registered but not initialized service registry. |
||
| 37 | * |
||
| 38 | * @var array |
||
| 39 | */ |
||
| 40 | protected $registry = []; |
||
| 41 | |||
| 42 | /** |
||
| 43 | * Parent container. |
||
| 44 | * |
||
| 45 | * @var ContainerInterface |
||
| 46 | */ |
||
| 47 | protected $parent; |
||
| 48 | |||
| 49 | /** |
||
| 50 | * Children container. |
||
| 51 | * |
||
| 52 | * @var ContainerInterface[] |
||
| 53 | */ |
||
| 54 | protected $children = []; |
||
| 55 | |||
| 56 | /** |
||
| 57 | * Create container. |
||
| 58 | * |
||
| 59 | * @param iterable $config |
||
| 60 | */ |
||
| 61 | public function __construct(Iterable $config = [], ?ContainerInterface $parent = null) |
||
| 66 | } |
||
| 67 | |||
| 68 | /** |
||
| 69 | * Get service. |
||
| 70 | * |
||
| 71 | * @param string $name |
||
| 72 | * |
||
| 73 | * @return mixed |
||
| 74 | */ |
||
| 75 | public function get($name) |
||
| 76 | { |
||
| 77 | if ($service = null !== $this->resolve($name)) { |
||
| 78 | return $service; |
||
| 79 | } |
||
| 80 | |||
| 81 | try { |
||
| 82 | return $this->lookupService($name); |
||
| 83 | } catch (Exception\ServiceNotFound $e) { |
||
| 84 | return $this->autoWireClass($name); |
||
| 85 | } |
||
| 86 | } |
||
| 87 | |||
| 88 | /** |
||
| 89 | * Traverse tree up and look for service. |
||
| 90 | * |
||
| 91 | * @param string $name |
||
| 92 | * |
||
| 93 | * @return mixed |
||
| 94 | */ |
||
| 95 | public function lookupService(string $name) |
||
| 96 | { |
||
| 97 | if ($service = null !== $this->resolve($name)) { |
||
| 98 | return $service; |
||
| 99 | } |
||
| 100 | |||
| 101 | if (null !== $this->parent) { |
||
| 102 | return $this->parent->lookupService($name); |
||
| 103 | } |
||
| 104 | |||
| 105 | throw new Exception\ServiceNotFound("service $name was not found in service tree"); |
||
| 106 | } |
||
| 107 | |||
| 108 | /** |
||
| 109 | * Add service. |
||
| 110 | * |
||
| 111 | * @param string $name |
||
| 112 | * @param mixed $service |
||
| 113 | * |
||
| 114 | * @return Container |
||
| 115 | */ |
||
| 116 | public function add(string $name, $service): self |
||
| 117 | { |
||
| 118 | if ($this->has($name)) { |
||
| 119 | throw new Exception\ServiceAlreadyExists('service '.$name.' is already registered'); |
||
| 120 | } |
||
| 121 | |||
| 122 | $this->registry[$name] = $service; |
||
| 123 | |||
| 124 | return $this; |
||
| 125 | } |
||
| 126 | |||
| 127 | /** |
||
| 128 | * Check if service is registered. |
||
| 129 | * |
||
| 130 | * @param mixed $name |
||
| 131 | * |
||
| 132 | * @return bool |
||
| 133 | */ |
||
| 134 | public function has($name): bool |
||
| 135 | { |
||
| 136 | return isset($this->service[$name]); |
||
| 137 | } |
||
| 138 | |||
| 139 | /** |
||
| 140 | * Resolve service. |
||
| 141 | * |
||
| 142 | * @param string $name |
||
| 143 | * |
||
| 144 | * @return mixed |
||
| 145 | */ |
||
| 146 | protected function resolve(string $name) |
||
| 147 | { |
||
| 148 | if ($this->has($name)) { |
||
| 149 | return $this->service[$name]; |
||
| 150 | } |
||
| 151 | |||
| 152 | if (isset($this->registry[$name])) { |
||
| 153 | return $this->addStaticService($name); |
||
| 154 | } |
||
| 155 | |||
| 156 | if (isset($this->config[$name])) { |
||
| 157 | return $this->autoWireClass($name); |
||
| 158 | } |
||
| 159 | |||
| 160 | return null; |
||
| 161 | } |
||
| 162 | |||
| 163 | /** |
||
| 164 | * Check for static injections. |
||
| 165 | * |
||
| 166 | * @param string $name |
||
| 167 | * |
||
| 168 | * @return mixed |
||
| 169 | */ |
||
| 170 | protected function addStaticService(string $name) |
||
| 171 | { |
||
| 172 | if ($this->registry[$name] instanceof Closure) { |
||
| 173 | $this->service[$name] = $this->registry[$name]->call($this); |
||
| 174 | } else { |
||
| 175 | $this->service[$name] = $this->registry[$name]; |
||
| 176 | } |
||
| 177 | |||
| 178 | unset($this->registry[$name]); |
||
| 179 | |||
| 180 | return $this->service[$name]; |
||
| 181 | } |
||
| 182 | |||
| 183 | /** |
||
| 184 | * Create service config. |
||
| 185 | * |
||
| 186 | * @param string $name |
||
| 187 | * |
||
| 188 | * @return array |
||
| 189 | */ |
||
| 190 | protected function createServiceConfig(string $name): array |
||
| 205 | } |
||
| 206 | |||
| 207 | /** |
||
| 208 | * Auto wire. |
||
| 209 | * |
||
| 210 | * @param string $name |
||
| 211 | * @param array $config |
||
| 212 | * @param array $parents |
||
| 213 | * |
||
| 214 | * @return mixed |
||
| 215 | */ |
||
| 216 | protected function autoWireClass(string $name) |
||
| 217 | { |
||
| 218 | $class = $name; |
||
| 219 | $config = []; |
||
| 220 | |||
| 221 | if (isset($this->config[$name])) { |
||
| 222 | $config = $this->config[$name]; |
||
| 223 | } |
||
| 224 | |||
| 225 | if (isset($config['use'])) { |
||
| 226 | if (!is_string($config['use'])) { |
||
| 227 | throw new Exception\InvalidConfiguration('use must be a string for service '.$name); |
||
|
|
|||
| 228 | } |
||
| 229 | |||
| 230 | $class = $config['use']; |
||
| 231 | } else { |
||
| 232 | $config = $this->createServiceConfig($name); |
||
| 233 | } |
||
| 234 | |||
| 235 | if (preg_match('#^\{(.*)\}$#', $class, $match)) { |
||
| 236 | $service = $this->get($match[1]); |
||
| 237 | |||
| 238 | if (isset($this->config[$name]['selects'])) { |
||
| 239 | $reflection = new ReflectionClass(get_class($service)); |
||
| 240 | |||
| 241 | foreach ($this->config[$name]['selects'] as $select) { |
||
| 242 | $args = $this->autoWireMethod($name, $reflection->getMethod($select['method']), $select); |
||
| 243 | $service = call_user_func_array([&$service, $select['method']], $args); |
||
| 244 | } |
||
| 245 | } |
||
| 246 | |||
| 247 | return $this->storeService($name, $config, $service); |
||
| 248 | } |
||
| 249 | |||
| 250 | try { |
||
| 251 | $reflection = new ReflectionClass($class); |
||
| 252 | } catch (\Exception $e) { |
||
| 253 | throw new Exception\ServiceNotFound($class.' can not be resolved to an existing class for service '.$name); |
||
| 254 | } |
||
| 255 | |||
| 256 | $constructor = $reflection->getConstructor(); |
||
| 257 | |||
| 258 | if (null === $constructor) { |
||
| 259 | return new $class(); |
||
| 260 | } |
||
| 261 | |||
| 262 | $args = $this->autoWireMethod($name, $constructor, $config); |
||
| 263 | |||
| 264 | return $this->createInstance($name, $reflection, $args, $config); |
||
| 265 | } |
||
| 266 | |||
| 267 | /** |
||
| 268 | * Store service. |
||
| 269 | * |
||
| 270 | * @param param string $name |
||
| 271 | * @param array $config |
||
| 272 | * @param mixed $service |
||
| 273 | * |
||
| 274 | * @return mixed |
||
| 275 | */ |
||
| 276 | protected function storeService(string $name, array $config, $service) |
||
| 277 | { |
||
| 278 | if (isset($config['singleton']) && true === $config['singleton']) { |
||
| 279 | return $service; |
||
| 280 | } |
||
| 281 | |||
| 282 | $this->service[$name] = $service; |
||
| 283 | |||
| 284 | return $service; |
||
| 285 | } |
||
| 286 | |||
| 287 | /** |
||
| 288 | * Create instance. |
||
| 289 | * |
||
| 290 | * @param string $name |
||
| 291 | * @param ReflectionClass $class |
||
| 292 | * @param array $arguments |
||
| 293 | * @param array $config |
||
| 294 | * |
||
| 295 | * @return mixed |
||
| 296 | */ |
||
| 297 | protected function createInstance(string $name, ReflectionClass $class, array $arguments, array $config) |
||
| 298 | { |
||
| 299 | $instance = $class->newInstanceArgs($arguments); |
||
| 300 | $this->storeService($name, $config, $instance); |
||
| 301 | |||
| 302 | if (!isset($this->config[$name]['calls'])) { |
||
| 303 | return $instance; |
||
| 304 | } |
||
| 305 | |||
| 306 | foreach ($this->config[$name]['calls'] as $call) { |
||
| 307 | if (!isset($call['method'])) { |
||
| 308 | throw new Exception\InvalidConfiguration('method is required for setter injection in service '.$name); |
||
| 309 | } |
||
| 310 | |||
| 311 | $arguments = []; |
||
| 312 | |||
| 313 | try { |
||
| 314 | $method = $class->getMethod($call['method']); |
||
| 315 | } catch (\ReflectionException $e) { |
||
| 316 | throw new Exception\InvalidConfiguration('method '.$call['method'].' is not callable in class '.$class->getName().' for service '.$name); |
||
| 317 | } |
||
| 318 | |||
| 319 | $arguments = $this->autoWireMethod($name, $method, $call); |
||
| 320 | call_user_func_array([&$instance, $call['method']], $arguments); |
||
| 321 | } |
||
| 322 | |||
| 323 | return $instance; |
||
| 324 | } |
||
| 325 | |||
| 326 | /** |
||
| 327 | * Autowire method. |
||
| 328 | * |
||
| 329 | * @param string $name |
||
| 330 | * @param ReflectionMethod $method |
||
| 331 | * @param array $config |
||
| 332 | * |
||
| 333 | * @return array |
||
| 334 | */ |
||
| 335 | protected function autoWireMethod(string $name, ReflectionMethod $method, array $config): array |
||
| 364 | } |
||
| 365 | |||
| 366 | /** |
||
| 367 | * Parse param value. |
||
| 368 | * |
||
| 369 | * @param mixed $param |
||
| 370 | * @param string $name |
||
| 371 | * |
||
| 372 | * @return mixed |
||
| 373 | */ |
||
| 374 | protected function parseParam($param, string $name) |
||
| 411 | } |
||
| 412 | |||
| 413 | /** |
||
| 414 | * Locate service. |
||
| 415 | * |
||
| 416 | * @param string $current_service |
||
| 417 | * @param string $service |
||
| 418 | */ |
||
| 419 | protected function findService(string $current_service, string $service) |
||
| 432 | } |
||
| 433 | } |
||
| 434 |
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths