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 |
||
| 31 | class Container extends Component implements ContainerInterface, FactoryInterface, ResolverInterface |
||
| 32 | { |
||
| 33 | /** |
||
| 34 | * IoC bindings. |
||
| 35 | * |
||
| 36 | * @invisible |
||
| 37 | * @var array |
||
| 38 | */ |
||
| 39 | protected $bindings = []; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * Registered injectors. |
||
| 43 | * |
||
| 44 | * @invisible |
||
| 45 | * @var array |
||
| 46 | */ |
||
| 47 | protected $injectors = []; |
||
| 48 | |||
| 49 | /** |
||
| 50 | * {@inheritdoc} |
||
| 51 | */ |
||
| 52 | public function has($alias) |
||
| 56 | |||
| 57 | /** |
||
| 58 | * {@inheritdoc} |
||
| 59 | * |
||
| 60 | * Context parameter will be passed to class injectors, which makes possible to use this method |
||
| 61 | * as: |
||
| 62 | * $this->container->get(DatabaseInterface::class, 'default'); |
||
| 63 | * |
||
| 64 | * @param string|null $context Call context. |
||
| 65 | */ |
||
| 66 | public function get($alias, $context = null) |
||
| 71 | |||
| 72 | /** |
||
| 73 | * {@inheritdoc} |
||
| 74 | * |
||
| 75 | * @param string|null $context Related to parameter caused injection if any. |
||
| 76 | */ |
||
| 77 | public function make($class, $parameters = [], $context = null) |
||
| 136 | |||
| 137 | /** |
||
| 138 | * {@inheritdoc} |
||
| 139 | */ |
||
| 140 | public function resolveArguments(ContextFunction $reflection, array $parameters = []) |
||
| 202 | |||
| 203 | /** |
||
| 204 | * {@inheritdoc} |
||
| 205 | * |
||
| 206 | * @return $this |
||
| 207 | */ |
||
| 208 | View Code Duplication | public function bind($alias, $resolver) |
|
| 220 | |||
| 221 | /** |
||
| 222 | * {@inheritdoc} |
||
| 223 | * |
||
| 224 | * @return $this |
||
| 225 | */ |
||
| 226 | View Code Duplication | public function bindSingleton($alias, $resolver) |
|
| 238 | |||
| 239 | /** |
||
| 240 | * {@inheritdoc} |
||
| 241 | * |
||
| 242 | * @return $this |
||
| 243 | */ |
||
| 244 | public function bindInjector($class, $injector) |
||
| 250 | |||
| 251 | /** |
||
| 252 | * {@inheritdoc} |
||
| 253 | */ |
||
| 254 | public function replace($alias, $resolver) |
||
| 265 | |||
| 266 | /** |
||
| 267 | * {@inheritdoc} |
||
| 268 | */ |
||
| 269 | public function restore($replacePayload) |
||
| 280 | |||
| 281 | /** |
||
| 282 | * {@inheritdoc} |
||
| 283 | */ |
||
| 284 | public function hasInstance($alias) |
||
| 297 | |||
| 298 | /** |
||
| 299 | * {@inheritdoc} |
||
| 300 | */ |
||
| 301 | public function removeBinding($alias) |
||
| 305 | |||
| 306 | /** |
||
| 307 | * Every declared Container binding. Must not be used in production code due container format is |
||
| 308 | * vary. |
||
| 309 | * |
||
| 310 | * @return array |
||
| 311 | */ |
||
| 312 | public function getBindings() |
||
| 316 | |||
| 317 | /** |
||
| 318 | * Every binded injector. |
||
| 319 | * |
||
| 320 | * @return array |
||
| 321 | */ |
||
| 322 | public function getInjectors() |
||
| 326 | |||
| 327 | /** |
||
| 328 | * Automatically create class. |
||
| 329 | * |
||
| 330 | * @param string $class |
||
| 331 | * @param array $parameters |
||
| 332 | * @param string $context |
||
| 333 | * @return object |
||
| 334 | * @throws AutowireException |
||
| 335 | */ |
||
| 336 | protected function autowire($class, array $parameters, $context) |
||
| 347 | |||
| 348 | /** |
||
| 349 | * Check if given class has associated injector. |
||
| 350 | * |
||
| 351 | * @todo replace with Context on demand |
||
| 352 | * @param \ReflectionClass $reflection |
||
| 353 | * @return bool |
||
| 354 | */ |
||
| 355 | protected function hasInjector(\ReflectionClass $reflection) |
||
| 363 | |||
| 364 | /** |
||
| 365 | * Get injector associated with given class. |
||
| 366 | * |
||
| 367 | * @todo replace with Context on demand |
||
| 368 | * @param \ReflectionClass $reflection |
||
| 369 | * @return InjectorInterface |
||
| 370 | */ |
||
| 371 | protected function getInjector(\ReflectionClass $reflection) |
||
| 379 | |||
| 380 | /** |
||
| 381 | * Create instance of desired class. |
||
| 382 | * |
||
| 383 | * @param string $class |
||
| 384 | * @param array $parameters Constructor parameters. |
||
| 385 | * @param string|null $context |
||
| 386 | * @param \ReflectionClass $reflection Instance of reflection associated with class, |
||
| 387 | * reference. |
||
| 388 | * @return object |
||
| 389 | * @throws ContainerException |
||
| 390 | */ |
||
| 391 | private function createInstance( |
||
| 438 | |||
| 439 | |||
| 440 | /** |
||
| 441 | * Make sure instance conditions are met |
||
| 442 | * |
||
| 443 | * @param object $instance |
||
| 444 | * @param \ReflectionClass $reflector |
||
| 445 | * @param array $parameters |
||
| 446 | * @return object |
||
| 447 | */ |
||
| 448 | private function registerInstance($instance, \ReflectionClass $reflector, array $parameters) |
||
| 464 | } |
Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code: