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 |
||
| 25 | class Container implements ContainerInterface |
||
| 26 | { |
||
| 27 | |||
| 28 | /** |
||
| 29 | * Holds all resolved or resolvable instances into the container. |
||
| 30 | * |
||
| 31 | * @var array |
||
| 32 | */ |
||
| 33 | |||
| 34 | protected $collection; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * Class specific defined dependencies. |
||
| 38 | * |
||
| 39 | * @var array |
||
| 40 | */ |
||
| 41 | |||
| 42 | protected $dependencies; |
||
| 43 | |||
| 44 | /** |
||
| 45 | * Cache of classes inspector and resolver. |
||
| 46 | * |
||
| 47 | * @var array |
||
| 48 | */ |
||
| 49 | |||
| 50 | protected $resolving; |
||
| 51 | |||
| 52 | /** |
||
| 53 | * Cache of classes dependencies in callbacks ready for resolution. |
||
| 54 | * |
||
| 55 | * @var array |
||
| 56 | */ |
||
| 57 | |||
| 58 | protected $resolved; |
||
| 59 | |||
| 60 | /** |
||
| 61 | * Call a user function injecting the dependencies. |
||
| 62 | * |
||
| 63 | * @param string|Closure $function The function or the user function name. |
||
| 64 | * @param array $parameters The predefined dependencies. |
||
| 65 | * |
||
| 66 | * @return mixed |
||
| 67 | */ |
||
| 68 | |||
| 69 | 3 | public function call($function, array $parameters = []) |
|
| 78 | |||
| 79 | /** |
||
| 80 | * Makes an element or class injecting automatically all the dependencies. |
||
| 81 | * |
||
| 82 | * @param string $abstract The class name or container element name to make. |
||
| 83 | * @param array $parameters Specific parameters definition. |
||
| 84 | * |
||
| 85 | * @throws ContainerException |
||
| 86 | * @return object|null |
||
| 87 | */ |
||
| 88 | |||
| 89 | 18 | public function make(string $abstract, array $parameters = []) |
|
| 105 | |||
| 106 | /** |
||
| 107 | * Construct a class and all the dependencies using the reflection library of PHP. |
||
| 108 | * |
||
| 109 | * @param string $abstract The class name or container element name to make. |
||
| 110 | * |
||
| 111 | * @throws ReflectionException |
||
| 112 | * @return Closure |
||
| 113 | */ |
||
| 114 | |||
| 115 | 18 | protected function construct(string $abstract) : Closure |
|
| 131 | |||
| 132 | /** |
||
| 133 | * Process all dependencies |
||
| 134 | * |
||
| 135 | * @param string $abstract The class name or container element name to make |
||
| 136 | * @param array $parameters User defined parameters that must be used instead of resolved ones |
||
| 137 | * @param array $dependencies Array of ReflectionParameter |
||
| 138 | * |
||
| 139 | * @throws ContainerException When a dependency cannot be solved. |
||
| 140 | * @return array |
||
| 141 | */ |
||
| 142 | |||
| 143 | 9 | protected function process(string $abstract, array $parameters, array $dependencies) : array |
|
| 153 | |||
| 154 | /** |
||
| 155 | * Resolve all the given class reflected dependencies. |
||
| 156 | * |
||
| 157 | * @param string $abstract The class name or container element name to resolve dependencies. |
||
| 158 | * @param ReflectionParameter $dependency The class dependency to be resolved. |
||
| 159 | * |
||
| 160 | * @throws ContainerException When a dependency cannot be solved. |
||
| 161 | * @return Object |
||
| 162 | */ |
||
| 163 | |||
| 164 | 7 | protected function resolve(string $abstract, ReflectionParameter $dependency) |
|
| 174 | |||
| 175 | /** |
||
| 176 | * Generate the dependencies callbacks to jump some conditions in every dependency creation. |
||
| 177 | * |
||
| 178 | * @param string $abstract The class name or container element name to resolve dependencies. |
||
| 179 | * @param ReflectionParameter $dependency The class dependency to be resolved. |
||
| 180 | * |
||
| 181 | * @throws ContainerException When a dependency cannot be solved. |
||
| 182 | * @return Closure |
||
| 183 | */ |
||
| 184 | |||
| 185 | 7 | protected function generate(string $abstract, ReflectionParameter $dependency) : Closure |
|
| 210 | |||
| 211 | /** |
||
| 212 | * Reset the container, removing all the elements, cache and options. |
||
| 213 | * |
||
| 214 | * @return self |
||
| 215 | */ |
||
| 216 | |||
| 217 | 1 | public function flush() : self |
|
| 226 | |||
| 227 | /** |
||
| 228 | * Finds an entry of the container by its identifier and returns it. |
||
| 229 | * |
||
| 230 | * @param string $abstract Identifier of the entry to look for. |
||
| 231 | * |
||
| 232 | * @throws NotFoundException No entry was found for this identifier. |
||
| 233 | * @throws ContainerException Error while retrieving the entry. |
||
| 234 | * |
||
| 235 | * @return mixed Entry. |
||
| 236 | */ |
||
| 237 | 6 | public function get($abstract) |
|
| 253 | |||
| 254 | /** |
||
| 255 | * Returns true if the container can return an entry for the given identifier. |
||
| 256 | * Returns false otherwise. |
||
| 257 | * |
||
| 258 | * `has($abstract)` returning true does not mean that `get($abstract)` will not throw an exception. |
||
| 259 | * It does however mean that `get($abstract)` will not throw a `NotFoundException`. |
||
| 260 | * |
||
| 261 | * @param string $abstract Identifier of the entry to look for. |
||
| 262 | * |
||
| 263 | * @return boolean |
||
| 264 | */ |
||
| 265 | |||
| 266 | 2 | public function has($abstract) |
|
| 270 | |||
| 271 | /** |
||
| 272 | * Verify if an element has a singleton instance. |
||
| 273 | * |
||
| 274 | * @param string The class name or container element name to resolve dependencies. |
||
| 275 | * @return bool |
||
| 276 | */ |
||
| 277 | |||
| 278 | 5 | public function isSingleton(string $abstract) : bool |
|
| 282 | |||
| 283 | /** |
||
| 284 | * Verify if an element is a instance of something. |
||
| 285 | * |
||
| 286 | * @param string The class name or container element name to resolve dependencies. |
||
| 287 | * @return bool |
||
| 288 | */ |
||
| 289 | public function isInstance(string $abstract) : bool |
||
| 293 | |||
| 294 | /** |
||
| 295 | * Bind a new element to the container. |
||
| 296 | * |
||
| 297 | * @param string $abstract The alias name that will be used to call the element. |
||
| 298 | * @param string|closure|object $concrete The element class name, or an closure that makes the element, or the object itself. |
||
| 299 | * @param bool $shared Define if the element will be a singleton instance. |
||
| 300 | * |
||
| 301 | * @return self |
||
| 302 | */ |
||
| 303 | |||
| 304 | 12 | public function set(string $abstract, $concrete, bool $shared = false) : self |
|
| 322 | |||
| 323 | /** |
||
| 324 | * Bind a new element to the container IF the element name not exists in the container. |
||
| 325 | * |
||
| 326 | * @param string $abstract The alias name that will be used to call the element. |
||
| 327 | * @param string|closure $concrete The element class name, or an closure that makes the element. |
||
| 328 | * @param bool $shared Define if the element will be a singleton instance. |
||
| 329 | * |
||
| 330 | * @return self |
||
| 331 | */ |
||
| 332 | |||
| 333 | 1 | public function setIf(string $abstract, $concrete, bool $shared = false) : self |
|
| 341 | |||
| 342 | /** |
||
| 343 | * Bind an specific instance to a class dependency. |
||
| 344 | * |
||
| 345 | * @param string $class The class full name. |
||
| 346 | * @param string $dependencyName The dependency full name. |
||
| 347 | * @param string|closure $dependency The specific object class name or a classure that makes the element. |
||
| 348 | * |
||
| 349 | * @return self |
||
| 350 | */ |
||
| 351 | |||
| 352 | 3 | public function setTo(string $class, string $dependencyName, $dependency) : self |
|
| 370 | |||
| 371 | /** |
||
| 372 | * Bind an element that will be construct only one time, and every call for the element, |
||
| 373 | * the same instance will be given. |
||
| 374 | * |
||
| 375 | * @param string $abstract The alias name that will be used to call the element. |
||
| 376 | * @param string|closure $concrete The element class name, or an closure that makes the element. |
||
| 377 | * |
||
| 378 | * @return self |
||
| 379 | */ |
||
| 380 | |||
| 381 | 3 | public function singleton(string $abstract, $concrete) : self |
|
| 387 | |||
| 388 | /** |
||
| 389 | * Bind an object to the container. |
||
| 390 | * |
||
| 391 | * @param string $abstract The alias name that will be used to call the object. |
||
| 392 | * @param object $instance The object that will be inserted. |
||
| 393 | * |
||
| 394 | * @throws ContainerException When $instance is not an object. |
||
| 395 | * @return self |
||
| 396 | */ |
||
| 397 | |||
| 398 | 2 | public function instance(string $abstract, $instance) : self |
|
| 408 | |||
| 409 | /** |
||
| 410 | * Modify an element with a given function that receive the old element as argument. |
||
| 411 | * |
||
| 412 | * @param string $abstract The alias name that will be used to call the element. |
||
| 413 | * @param closure $extension The function that receives the old element and return a new or modified one. |
||
| 414 | * |
||
| 415 | * @throws NotFoundException When no element was found with $abstract key. |
||
| 416 | * @return self |
||
| 417 | */ |
||
| 418 | |||
| 419 | 2 | public function extend(string $abstract, closure $extension) : self |
|
| 437 | |||
| 438 | /** |
||
| 439 | * Makes an resolvable element an singleton. |
||
| 440 | * |
||
| 441 | * @param string $abstract The alias name that will be used to call the element. |
||
| 442 | * |
||
| 443 | * @throws NotFoundException When no element was found with $abstract key. |
||
| 444 | * @throws ContainerException When the element on $abstract key is not resolvable. |
||
| 445 | * |
||
| 446 | * @return self |
||
| 447 | */ |
||
| 448 | |||
| 449 | 1 | public function share(string $abstract) : self |
|
| 463 | |||
| 464 | } |
||
| 465 |