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 |
||
| 41 | class Container implements ContainerInterface, ArrayAccess |
||
| 42 | { |
||
| 43 | /** |
||
| 44 | * @var self |
||
| 45 | */ |
||
| 46 | protected static $instance; |
||
| 47 | |||
| 48 | /** |
||
| 49 | * Store services. |
||
| 50 | * |
||
| 51 | * @var array |
||
| 52 | */ |
||
| 53 | protected $services = []; |
||
| 54 | |||
| 55 | /** |
||
| 56 | * @var array |
||
| 57 | */ |
||
| 58 | protected $shared = []; |
||
| 59 | |||
| 60 | /** |
||
| 61 | * Returns last container instance. |
||
| 62 | * |
||
| 63 | * @return Container |
||
| 64 | */ |
||
| 65 | public static function instance() |
||
| 69 | |||
| 70 | /** |
||
| 71 | * Initialisation |
||
| 72 | */ |
||
| 73 | public function __construct() |
||
| 77 | |||
| 78 | /** |
||
| 79 | * Registers a service in the services container. |
||
| 80 | * |
||
| 81 | * @param string $id Service id. |
||
| 82 | * @param mixed $definition Service definition. |
||
| 83 | * @param bool $shared Shared or not. |
||
| 84 | * |
||
| 85 | * @return self |
||
| 86 | */ |
||
| 87 | public function set($id, $definition, $shared = false) |
||
| 96 | |||
| 97 | /** |
||
| 98 | * Registers a shared service in the services container. |
||
| 99 | * |
||
| 100 | * @param string $id Service id. |
||
| 101 | * @param mixed $definition Service definition. |
||
| 102 | * |
||
| 103 | * @return self |
||
| 104 | */ |
||
| 105 | public function shared($id, $definition) |
||
| 109 | |||
| 110 | /** |
||
| 111 | * Finds an entry of the container by its identifier and returns it. |
||
| 112 | * |
||
| 113 | * @param string $id Identifier of the entry to look for. |
||
| 114 | * @param array $params Parameter for service construct. |
||
| 115 | * |
||
| 116 | * @throws NotFoundException No entry was found for this identifier. |
||
| 117 | * @throws ContainerException Error while retrieving the entry. |
||
| 118 | * |
||
| 119 | * @return mixed Entry. |
||
| 120 | */ |
||
| 121 | public function get($id, array $params = []) |
||
| 148 | |||
| 149 | /** |
||
| 150 | * |
||
| 151 | * |
||
| 152 | * @param callable $callback |
||
| 153 | * @param array $params |
||
| 154 | * |
||
| 155 | * @return mixed |
||
| 156 | * @throws ContainerException Error while retrieving the entry. |
||
| 157 | * @throws NotFoundException No entry was found for this identifier. |
||
| 158 | */ |
||
| 159 | public function call(callable $callback, array $params = []) |
||
| 178 | |||
| 179 | /** |
||
| 180 | * Resolves the service. |
||
| 181 | * |
||
| 182 | * @param mixed $definition The definition of service. |
||
| 183 | * @param array $params Parameters for service construct. |
||
| 184 | * |
||
| 185 | * @return mixed Entry. |
||
| 186 | * @throws ContainerException Error while retrieving the entry. |
||
| 187 | * @throws NotFoundException No entry was found for this identifier. |
||
| 188 | */ |
||
| 189 | protected function resolveService($definition, array $params) |
||
| 220 | |||
| 221 | /** |
||
| 222 | * Resolve parameters of service construct. |
||
| 223 | * |
||
| 224 | * @param array $dependencies |
||
| 225 | * @param array $parameters |
||
| 226 | * |
||
| 227 | * @return array Resolved parameters. |
||
| 228 | * @throws ContainerException Error while retrieving the entry. |
||
| 229 | * @throws NotFoundException No entry was found for this identifier. |
||
| 230 | */ |
||
| 231 | protected function resolveOptions(array $dependencies, array $parameters) |
||
| 271 | |||
| 272 | /** |
||
| 273 | * Removes a service in the services container. |
||
| 274 | * |
||
| 275 | * @param string $id Service id. |
||
| 276 | * |
||
| 277 | * @return void |
||
| 278 | */ |
||
| 279 | public function remove($id) |
||
| 283 | |||
| 284 | /** |
||
| 285 | * Returns true if the container can return an entry for the given identifier. |
||
| 286 | * Returns false otherwise. |
||
| 287 | * |
||
| 288 | * @param string $id Identifier of the entry to look for. |
||
| 289 | * |
||
| 290 | * @return bool |
||
| 291 | */ |
||
| 292 | public function has($id) |
||
| 296 | |||
| 297 | /** |
||
| 298 | * Merge two containers into one. |
||
| 299 | * |
||
| 300 | * @param ServiceProviderInterface $provider Another container. |
||
| 301 | * |
||
| 302 | * @return self |
||
| 303 | */ |
||
| 304 | public function register(ServiceProviderInterface $provider) |
||
| 309 | |||
| 310 | /** |
||
| 311 | * Check whether the service is shared or not. |
||
| 312 | * |
||
| 313 | * @param string $id Service id. |
||
| 314 | * |
||
| 315 | * @return bool True if shared, false - not. |
||
| 316 | */ |
||
| 317 | public function isShared($id) |
||
| 321 | |||
| 322 | /** |
||
| 323 | * Sets if the service is shared or not. |
||
| 324 | * |
||
| 325 | * @param string $id Service id. |
||
| 326 | * @param bool $shared Shared or not. |
||
| 327 | * |
||
| 328 | * @return self |
||
| 329 | */ |
||
| 330 | public function setShared($id, $shared = true) |
||
| 337 | |||
| 338 | /** |
||
| 339 | * Allows to register a shared service using the array syntax. |
||
| 340 | * |
||
| 341 | * @param string $id Service id. |
||
| 342 | * @param mixed $definition Service definition. |
||
| 343 | * |
||
| 344 | * @return self |
||
| 345 | */ |
||
| 346 | public function offsetSet($id, $definition) |
||
| 350 | |||
| 351 | /** |
||
| 352 | * Finds an entry of the container by its identifier and returns it. |
||
| 353 | * |
||
| 354 | * @param string $id Identifier of the entry to look for. |
||
| 355 | * |
||
| 356 | * @throws NotFoundException No entry was found for this identifier. |
||
| 357 | * @throws ContainerException Error while retrieving the entry. |
||
| 358 | * |
||
| 359 | * @return mixed Entry. |
||
| 360 | */ |
||
| 361 | public function offsetGet($id) |
||
| 365 | |||
| 366 | /** |
||
| 367 | * Removes a service from the services container using the array syntax. |
||
| 368 | * |
||
| 369 | * @param string $id Service id. |
||
| 370 | * |
||
| 371 | * @return void |
||
| 372 | */ |
||
| 373 | public function offsetUnset($id) |
||
| 377 | |||
| 378 | /** |
||
| 379 | * Returns true if the container can return an entry for the given identifier. |
||
| 380 | * Returns false otherwise. |
||
| 381 | * |
||
| 382 | * @param string $id Identifier of the entry to look for. |
||
| 383 | * |
||
| 384 | * @return bool |
||
| 385 | */ |
||
| 386 | public function offsetExists($id) |
||
| 390 | |||
| 391 | /** |
||
| 392 | * Allows to register a shared service using the array syntax. |
||
| 393 | * |
||
| 394 | * @param string $id Service id. |
||
| 395 | * @param mixed $definition Service definition. |
||
| 396 | * |
||
| 397 | * @return self |
||
| 398 | */ |
||
| 399 | public function __set($id, $definition) |
||
| 403 | |||
| 404 | /** |
||
| 405 | * Finds an entry of the container by its identifier and returns it. |
||
| 406 | * |
||
| 407 | * @param string $id Identifier of the entry to look for. |
||
| 408 | * |
||
| 409 | * @throws NotFoundException No entry was found for this identifier. |
||
| 410 | * @throws ContainerException Error while retrieving the entry. |
||
| 411 | * |
||
| 412 | * @return mixed Entry. |
||
| 413 | */ |
||
| 414 | public function __get($id) |
||
| 418 | |||
| 419 | /** |
||
| 420 | * Removes a service from the services container using the array syntax. |
||
| 421 | * |
||
| 422 | * @param string $id Service id. |
||
| 423 | * |
||
| 424 | * @return void |
||
| 425 | */ |
||
| 426 | public function __unset($id) |
||
| 430 | |||
| 431 | /** |
||
| 432 | * Returns true if the container can return an entry for the given identifier. |
||
| 433 | * Returns false otherwise. |
||
| 434 | * |
||
| 435 | * @param string $id Identifier of the entry to look for. |
||
| 436 | * |
||
| 437 | * @return bool |
||
| 438 | */ |
||
| 439 | public function __isset($id) |
||
| 443 | } |
This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.
Unreachable code is most often the result of
return,dieorexitstatements that have been added for debug purposes.In the above example, the last
return falsewill never be executed, because a return statement has already been met in every possible execution path.