Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
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 |
||
| 33 | class Container extends Component implements ContainerInterface, FactoryInterface, ResolverInterface |
||
| 34 | { |
||
| 35 | /** |
||
| 36 | * IoC bindings. |
||
| 37 | * |
||
| 38 | * @invisible |
||
| 39 | * @var array |
||
| 40 | */ |
||
| 41 | protected $bindings = []; |
||
| 42 | |||
| 43 | /** |
||
| 44 | * Registered injectors. |
||
| 45 | * |
||
| 46 | * @invisible |
||
| 47 | * @var array |
||
| 48 | */ |
||
| 49 | protected $injectors = []; |
||
| 50 | |||
| 51 | /** |
||
| 52 | * {@inheritdoc} |
||
| 53 | */ |
||
| 54 | public function has($alias) |
||
| 58 | |||
| 59 | /** |
||
| 60 | * {@inheritdoc} |
||
| 61 | * |
||
| 62 | * Context parameter will be passed to class injectors, which makes possible to use this method |
||
| 63 | * as: |
||
| 64 | * $this->container->get(DatabaseInterface::class, 'default'); |
||
| 65 | * |
||
| 66 | * @param string|null $context Call context. |
||
| 67 | */ |
||
| 68 | public function get($alias, $context = null) |
||
| 73 | |||
| 74 | /** |
||
| 75 | * {@inheritdoc} |
||
| 76 | * |
||
| 77 | * @param string|null $context Related to parameter caused injection if any. |
||
| 78 | */ |
||
| 79 | public function make($class, $parameters = [], $context = null) |
||
| 134 | |||
| 135 | /** |
||
| 136 | * {@inheritdoc} |
||
| 137 | */ |
||
| 138 | public function resolveArguments(ContextFunction $reflection, array $parameters = []) |
||
| 200 | |||
| 201 | /** |
||
| 202 | * {@inheritdoc} |
||
| 203 | * |
||
| 204 | * @return $this |
||
| 205 | */ |
||
| 206 | View Code Duplication | public function bind($alias, $resolver) |
|
| 218 | |||
| 219 | /** |
||
| 220 | * {@inheritdoc} |
||
| 221 | * |
||
| 222 | * @return $this |
||
| 223 | */ |
||
| 224 | View Code Duplication | public function bindSingleton($alias, $resolver) |
|
| 236 | |||
| 237 | /** |
||
| 238 | * {@inheritdoc} |
||
| 239 | * |
||
| 240 | * @return $this |
||
| 241 | */ |
||
| 242 | public function bindInjector($class, $injector) |
||
| 248 | |||
| 249 | /** |
||
| 250 | * {@inheritdoc} |
||
| 251 | */ |
||
| 252 | public function replace($alias, $resolver) |
||
| 263 | |||
| 264 | /** |
||
| 265 | * {@inheritdoc} |
||
| 266 | */ |
||
| 267 | public function restore($replacePayload) |
||
| 278 | |||
| 279 | /** |
||
| 280 | * {@inheritdoc} |
||
| 281 | */ |
||
| 282 | public function hasInstance($alias) |
||
| 295 | |||
| 296 | /** |
||
| 297 | * {@inheritdoc} |
||
| 298 | */ |
||
| 299 | public function removeBinding($alias) |
||
| 303 | |||
| 304 | /** |
||
| 305 | * Every declared Container binding. Must not be used in production code due container format is |
||
| 306 | * vary. |
||
| 307 | * |
||
| 308 | * @return array |
||
| 309 | */ |
||
| 310 | public function getBindings() |
||
| 314 | |||
| 315 | /** |
||
| 316 | * Every binded injector. |
||
| 317 | * |
||
| 318 | * @return array |
||
| 319 | */ |
||
| 320 | public function getInjectors() |
||
| 324 | |||
| 325 | /** |
||
| 326 | * Automatically create class. |
||
| 327 | * |
||
| 328 | * @param string $class |
||
| 329 | * @param array $parameters |
||
| 330 | * @param string $context |
||
| 331 | * @return object |
||
| 332 | * @throws AutowireException |
||
| 333 | */ |
||
| 334 | protected function autowire($class, array $parameters, $context) |
||
| 360 | |||
| 361 | /** |
||
| 362 | * Check if given class has associated injector. |
||
| 363 | * |
||
| 364 | * @param \ReflectionClass $reflection |
||
| 365 | * @return bool |
||
| 366 | */ |
||
| 367 | protected function hasInjector(\ReflectionClass $reflection) |
||
| 375 | |||
| 376 | /** |
||
| 377 | * Get injector associated with given class. |
||
| 378 | * |
||
| 379 | * @param \ReflectionClass $reflection |
||
| 380 | * @return InjectorInterface |
||
| 381 | */ |
||
| 382 | protected function getInjector(\ReflectionClass $reflection) |
||
| 390 | |||
| 391 | /** |
||
| 392 | * Create instance of desired class. |
||
| 393 | * |
||
| 394 | * @param string $class |
||
| 395 | * @param array $parameters Constructor parameters. |
||
| 396 | * @param string|null $context |
||
| 397 | * @param \ReflectionClass $reflection Instance of reflection associated with class, |
||
| 398 | * reference. |
||
| 399 | * @return object |
||
| 400 | * @throws ContainerException |
||
| 401 | */ |
||
| 402 | private function createInstance( |
||
| 449 | } |
||
| 450 |