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. 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 Container, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 47 | class Container implements ContainerInterface, ArrayAccess |
||
| 48 | { |
||
| 49 | const MAX_DEPENDENCY_LEVEL = 30; |
||
| 50 | |||
| 51 | /** |
||
| 52 | * @var self Store for last container instance |
||
| 53 | */ |
||
| 54 | protected static $instance; |
||
| 55 | |||
| 56 | /** |
||
| 57 | * @var array Store for services. |
||
| 58 | */ |
||
| 59 | protected $services = []; |
||
| 60 | |||
| 61 | /** |
||
| 62 | * @var array Store for shared services. |
||
| 63 | */ |
||
| 64 | protected $shared = []; |
||
| 65 | |||
| 66 | /** |
||
| 67 | * @var int |
||
| 68 | */ |
||
| 69 | protected $level = 0; |
||
| 70 | |||
| 71 | /** |
||
| 72 | * Returns last container instance. |
||
| 73 | * |
||
| 74 | * @return Container Last container instance. |
||
| 75 | */ |
||
| 76 | public static function instance() |
||
| 80 | |||
| 81 | /** |
||
| 82 | * Initialisation |
||
| 83 | */ |
||
| 84 | public function __construct() |
||
| 88 | |||
| 89 | /** |
||
| 90 | * Merge two containers into one. |
||
| 91 | * |
||
| 92 | * @param ServiceProviderInterface $provider Service provider. |
||
| 93 | * |
||
| 94 | * @return self |
||
| 95 | */ |
||
| 96 | public function register(ServiceProviderInterface $provider) |
||
| 101 | |||
| 102 | /** |
||
| 103 | * Registers a service in the container. |
||
| 104 | * |
||
| 105 | * @param string $id Service id. |
||
| 106 | * @param mixed $definition Service definition. |
||
| 107 | * @param bool $shared Shared or not. |
||
| 108 | * |
||
| 109 | * @return $this |
||
| 110 | * @throws ContainerException Error while retrieving the entry. |
||
| 111 | */ |
||
| 112 | public function set($id, $definition, $shared = false) |
||
| 120 | |||
| 121 | /** |
||
| 122 | * Normalize service name. |
||
| 123 | * |
||
| 124 | * @param string $id Service id. |
||
| 125 | * |
||
| 126 | * @return string Normalized name. |
||
| 127 | */ |
||
| 128 | protected function normalize($id) |
||
| 132 | |||
| 133 | /** |
||
| 134 | * Registers a shared service in the container. |
||
| 135 | * |
||
| 136 | * @param string $id Service id. |
||
| 137 | * @param mixed $definition Service definition. |
||
| 138 | * |
||
| 139 | * @return $this |
||
| 140 | */ |
||
| 141 | public function shared($id, $definition) |
||
| 145 | |||
| 146 | /** |
||
| 147 | * Check whether the service is shared or not. |
||
| 148 | * |
||
| 149 | * @param string $id Service id. |
||
| 150 | * |
||
| 151 | * @return bool True if shared, false - not. |
||
| 152 | */ |
||
| 153 | public function isShared($id) |
||
| 157 | |||
| 158 | /** |
||
| 159 | * Sets if the service is shared or not. |
||
| 160 | * |
||
| 161 | * @param string $id Service id. |
||
| 162 | * @param bool $shared Shared or not. |
||
| 163 | * |
||
| 164 | * @return self |
||
| 165 | * @throws NotFoundException No entry was found for this identifier. |
||
| 166 | */ |
||
| 167 | public function setShared($id, $shared = true) |
||
| 176 | |||
| 177 | /** |
||
| 178 | * Finds an entry of the container by its identifier and returns it. |
||
| 179 | * |
||
| 180 | * @param string $id Identifier of the entry to look for. |
||
| 181 | * @param array $parameters Parameter for service construct. |
||
| 182 | * |
||
| 183 | * @throws NotFoundException No entry was found for this identifier. |
||
| 184 | * @throws ContainerException Error while retrieving the entry. |
||
| 185 | * |
||
| 186 | * @return mixed Entry. |
||
| 187 | */ |
||
| 188 | public function get($id, array $parameters = []) |
||
| 213 | |||
| 214 | /** |
||
| 215 | * Resolves the service. |
||
| 216 | * |
||
| 217 | * @param mixed $definition The definition of service. |
||
| 218 | * @param array $parameters Parameters for service construct. |
||
| 219 | * |
||
| 220 | * @return mixed Entry. |
||
| 221 | * @throws ContainerException Error while retrieving the entry. |
||
| 222 | * @throws NotFoundException No entry was found for this identifier. |
||
| 223 | */ |
||
| 224 | protected function resolveService($definition, array $parameters = []) |
||
| 252 | |||
| 253 | /** |
||
| 254 | * Resolve callback dependencies and executes him. |
||
| 255 | * |
||
| 256 | * @param callable $callback |
||
| 257 | * @param array $parameters |
||
| 258 | * |
||
| 259 | * @return mixed |
||
| 260 | * @throws ContainerException Error while retrieving the entry. |
||
| 261 | * @throws NotFoundException No entry was found for this identifier. |
||
| 262 | */ |
||
| 263 | public function call(callable $callback, array $parameters = []) |
||
| 279 | |||
| 280 | /** |
||
| 281 | * Resolve parameters of service. |
||
| 282 | * |
||
| 283 | * @param array $dependencies |
||
| 284 | * @param array $parameters |
||
| 285 | * |
||
| 286 | * @return array Resolved parameters. |
||
| 287 | * @throws ContainerException Error while retrieving the entry. |
||
| 288 | * @throws NotFoundException No entry was found for this identifier. |
||
| 289 | */ |
||
| 290 | protected function resolveOptions(array $dependencies, array $parameters) |
||
| 319 | |||
| 320 | /** |
||
| 321 | * Removes a service in the services container. |
||
| 322 | * |
||
| 323 | * @param string $id Service id. |
||
| 324 | * |
||
| 325 | * @return void |
||
| 326 | */ |
||
| 327 | public function remove($id) |
||
| 331 | |||
| 332 | /** |
||
| 333 | * Returns true if the container can return an entry for the given identifier. |
||
| 334 | * Returns false otherwise. |
||
| 335 | * |
||
| 336 | * @param string $id Identifier of the entry to look for. |
||
| 337 | * |
||
| 338 | * @return bool |
||
| 339 | */ |
||
| 340 | public function has($id) |
||
| 344 | |||
| 345 | /** |
||
| 346 | * Allows to register a shared service using the array syntax. |
||
| 347 | * |
||
| 348 | * @param string $id Service id. |
||
| 349 | * @param mixed $definition Service definition. |
||
| 350 | * |
||
| 351 | * @return self |
||
| 352 | */ |
||
| 353 | public function offsetSet($id, $definition) |
||
| 357 | |||
| 358 | /** |
||
| 359 | * Finds an entry of the container by its identifier and returns it. |
||
| 360 | * |
||
| 361 | * @param string $id Identifier of the entry to look for. |
||
| 362 | * |
||
| 363 | * @throws NotFoundException No entry was found for this identifier. |
||
| 364 | * @throws ContainerException Error while retrieving the entry. |
||
| 365 | * |
||
| 366 | * @return mixed Entry. |
||
| 367 | */ |
||
| 368 | public function offsetGet($id) |
||
| 372 | |||
| 373 | /** |
||
| 374 | * Removes a service from the services container using the array syntax. |
||
| 375 | * |
||
| 376 | * @param string $id Service id. |
||
| 377 | * |
||
| 378 | * @return void |
||
| 379 | */ |
||
| 380 | public function offsetUnset($id) |
||
| 384 | |||
| 385 | /** |
||
| 386 | * Returns true if the container can return an entry for the given identifier. |
||
| 387 | * Returns false otherwise. |
||
| 388 | * |
||
| 389 | * @param string $id Identifier of the entry to look for. |
||
| 390 | * |
||
| 391 | * @return bool |
||
| 392 | */ |
||
| 393 | public function offsetExists($id) |
||
| 397 | |||
| 398 | /** |
||
| 399 | * Allows to register a shared service using the array syntax. |
||
| 400 | * |
||
| 401 | * @param string $id Service id. |
||
| 402 | * @param mixed $definition Service definition. |
||
| 403 | * |
||
| 404 | * @return self |
||
| 405 | */ |
||
| 406 | public function __set($id, $definition) |
||
| 410 | |||
| 411 | /** |
||
| 412 | * Finds an entry of the container by its identifier and returns it. |
||
| 413 | * |
||
| 414 | * @param string $id Identifier of the entry to look for. |
||
| 415 | * |
||
| 416 | * @throws NotFoundException No entry was found for this identifier. |
||
| 417 | * @throws ContainerException Error while retrieving the entry. |
||
| 418 | * |
||
| 419 | * @return mixed Entry. |
||
| 420 | */ |
||
| 421 | public function __get($id) |
||
| 425 | |||
| 426 | /** |
||
| 427 | * Removes a service from the services container using the array syntax. |
||
| 428 | * |
||
| 429 | * @param string $id Service id. |
||
| 430 | * |
||
| 431 | * @return void |
||
| 432 | */ |
||
| 433 | public function __unset($id) |
||
| 437 | |||
| 438 | /** |
||
| 439 | * Returns true if the container can return an entry for the given identifier. |
||
| 440 | * Returns false otherwise. |
||
| 441 | * |
||
| 442 | * @param string $id Identifier of the entry to look for. |
||
| 443 | * |
||
| 444 | * @return bool |
||
| 445 | */ |
||
| 446 | public function __isset($id) |
||
| 450 | } |