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 | * Set of abstracts as keys and implementations as values. |
||
| 29 | * |
||
| 30 | * @var array |
||
| 31 | */ |
||
| 32 | protected $services = array(); |
||
| 33 | |||
| 34 | /** |
||
| 35 | * Set of aliases as keys and interfaces as values. |
||
| 36 | * |
||
| 37 | * @var array |
||
| 38 | */ |
||
| 39 | protected $aliases = array(); |
||
| 40 | |||
| 41 | /** |
||
| 42 | * A delegate container to resolve services from. |
||
| 43 | * |
||
| 44 | * @var ContainerInterface |
||
| 45 | */ |
||
| 46 | protected $delegate; |
||
| 47 | |||
| 48 | /** |
||
| 49 | * Instantiate a service container. |
||
| 50 | * |
||
| 51 | * Registers the service container with itself, as well as registering any |
||
| 52 | * given services and aliases. |
||
| 53 | * |
||
| 54 | * @param array $services [optional] Initial set of services and/or aliases |
||
| 55 | */ |
||
| 56 | public function __construct(array $services = array()) |
||
| 65 | |||
| 66 | /** |
||
| 67 | * Dynamically resolve a service. |
||
| 68 | * |
||
| 69 | * @param string $abstract |
||
| 70 | * @return mixed |
||
| 71 | */ |
||
| 72 | public function __get($abstract) |
||
| 76 | |||
| 77 | /** |
||
| 78 | * Dynamically register a service. |
||
| 79 | * |
||
| 80 | * @param string $abstract |
||
| 81 | * @param mixed $service |
||
| 82 | */ |
||
| 83 | public function __set($abstract, $service) |
||
| 87 | |||
| 88 | /** |
||
| 89 | * Determine whether the container has a service registered for the given |
||
| 90 | * abstract or alias. |
||
| 91 | * |
||
| 92 | * @param string $abstract |
||
| 93 | * @return bool |
||
| 94 | */ |
||
| 95 | public function has($abstract) |
||
| 99 | |||
| 100 | /** |
||
| 101 | * Get the service associated with the given abstract or alias. |
||
| 102 | * |
||
| 103 | * This method recursively resolves aliases but does not resolve service |
||
| 104 | * dependencies. |
||
| 105 | * |
||
| 106 | * Returns null if nothing is found. |
||
| 107 | * |
||
| 108 | * @param string $abstract |
||
| 109 | * @return mixed |
||
| 110 | */ |
||
| 111 | public function get($abstract) |
||
| 129 | |||
| 130 | /** |
||
| 131 | * Register a service and its associated implementation. |
||
| 132 | * |
||
| 133 | * @param string $abstract |
||
| 134 | * @param mixed $concrete |
||
| 135 | */ |
||
| 136 | public function set($abstract, $concrete) |
||
| 140 | |||
| 141 | /** |
||
| 142 | * Retrieve all registered services. |
||
| 143 | * |
||
| 144 | * @return array |
||
| 145 | */ |
||
| 146 | public function all() |
||
| 150 | |||
| 151 | /** |
||
| 152 | * Register an alias for the given abstract. |
||
| 153 | * |
||
| 154 | * @param string $alias |
||
| 155 | * @param string $abstract |
||
| 156 | */ |
||
| 157 | public function alias($alias, $abstract) |
||
| 161 | |||
| 162 | /** |
||
| 163 | * Register services and aliases. |
||
| 164 | * |
||
| 165 | * This method registers aliases if their abstract is already |
||
| 166 | * registered with the container. |
||
| 167 | * |
||
| 168 | * @param array $services abstract => concrete and/or alias => abstract |
||
| 169 | */ |
||
| 170 | public function register(array $services = array()) |
||
| 180 | |||
| 181 | /** |
||
| 182 | * Resolve a service and its dependencies. |
||
| 183 | * |
||
| 184 | * This method recursively resolves services and aliases. |
||
| 185 | * |
||
| 186 | * @param string $abstract Abstract or alias |
||
| 187 | * @param array $arguments [optional] |
||
| 188 | * @return mixed |
||
| 189 | */ |
||
| 190 | public function resolve($abstract, array $arguments = array()) |
||
| 210 | |||
| 211 | /** |
||
| 212 | * Wraps a callable in a closure that returns the same instance on every |
||
| 213 | * call using a static variable. |
||
| 214 | * |
||
| 215 | * @param callable $callable |
||
| 216 | * @return Closure |
||
| 217 | * @throws ContainerException |
||
| 218 | */ |
||
| 219 | public function share($callable) |
||
| 237 | |||
| 238 | /** |
||
| 239 | * Call a callable and attempt to resolve its parameters using services |
||
| 240 | * registered with the container. |
||
| 241 | * |
||
| 242 | * @param callable $callable |
||
| 243 | * @param array $arguments [optional] |
||
| 244 | * @return mixed |
||
| 245 | */ |
||
| 246 | public function call($callable, array $arguments = array()) |
||
| 265 | |||
| 266 | /** |
||
| 267 | * Instantiate the given class and attempt to resolve its constructor's |
||
| 268 | * parameters using services registered with the container. |
||
| 269 | * |
||
| 270 | * @param string $class |
||
| 271 | * @param array $arguments [optional] |
||
| 272 | * @return object |
||
| 273 | */ |
||
| 274 | public function create($class, array $arguments = array()) |
||
| 294 | |||
| 295 | /** |
||
| 296 | * Delegate a container to resolve services from when this container is |
||
| 297 | * unable to. |
||
| 298 | * |
||
| 299 | * @param ContainerInterface $container |
||
| 300 | */ |
||
| 301 | public function delegate(ContainerInterface $container) |
||
| 305 | |||
| 306 | /** |
||
| 307 | * Merge resolved parameters with the given arguments. |
||
| 308 | * |
||
| 309 | * TODO: Make this smarter. |
||
| 310 | * |
||
| 311 | * @param array $resolved |
||
| 312 | * @param array $arguments |
||
| 313 | * @return array |
||
| 314 | */ |
||
| 315 | protected function mergeResolvedParameters(array $resolved, array $arguments = array()) |
||
| 324 | |||
| 325 | /** |
||
| 326 | * Resolve a set of reflection parameters. |
||
| 327 | * |
||
| 328 | * @param ReflectionParameter[] $parameters |
||
| 329 | * @param array $arguments [optional] |
||
| 330 | * @return array |
||
| 331 | */ |
||
| 332 | protected function resolveParameters($parameters, array $arguments = array()) |
||
| 345 | |||
| 346 | /** |
||
| 347 | * Attempt to resolve a reflection parameter's argument. |
||
| 348 | * |
||
| 349 | * @param ReflectionParameter|null $parameter |
||
| 350 | * @return mixed |
||
| 351 | */ |
||
| 352 | protected function resolveParameter(ReflectionParameter $parameter) |
||
| 372 | |||
| 373 | /** |
||
| 374 | * Resolve the given reflection parameters type hint. |
||
| 375 | * |
||
| 376 | * @param ReflectionParameter $parameter |
||
| 377 | * @return string|null |
||
| 378 | */ |
||
| 379 | protected function resolveParameterType(ReflectionParameter $parameter) |
||
| 385 | } |
||
| 386 |