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) |
||
| 144 | |||
| 145 | /** |
||
| 146 | * {@inheritdoc} |
||
| 147 | */ |
||
| 148 | public function resolveArguments(ContextFunction $reflection, array $parameters = []) |
||
| 210 | |||
| 211 | /** |
||
| 212 | * {@inheritdoc} |
||
| 213 | * |
||
| 214 | * @return $this |
||
| 215 | */ |
||
| 216 | View Code Duplication | public function bind($alias, $resolver) |
|
| 228 | |||
| 229 | /** |
||
| 230 | * {@inheritdoc} |
||
| 231 | * |
||
| 232 | * @return $this |
||
| 233 | */ |
||
| 234 | View Code Duplication | public function bindSingleton($alias, $resolver) |
|
| 246 | |||
| 247 | /** |
||
| 248 | * {@inheritdoc} |
||
| 249 | * |
||
| 250 | * @return $this |
||
| 251 | */ |
||
| 252 | public function bindInjector($class, $injector) |
||
| 258 | |||
| 259 | /** |
||
| 260 | * {@inheritdoc} |
||
| 261 | */ |
||
| 262 | public function replace($alias, $resolver) |
||
| 273 | |||
| 274 | /** |
||
| 275 | * {@inheritdoc} |
||
| 276 | */ |
||
| 277 | public function restore($replacePayload) |
||
| 288 | |||
| 289 | /** |
||
| 290 | * {@inheritdoc} |
||
| 291 | */ |
||
| 292 | public function hasInstance($alias) |
||
| 305 | |||
| 306 | /** |
||
| 307 | * {@inheritdoc} |
||
| 308 | */ |
||
| 309 | public function removeBinding($alias) |
||
| 313 | |||
| 314 | /** |
||
| 315 | * Every declared Container binding. Must not be used in production code due container format is |
||
| 316 | * vary. |
||
| 317 | * |
||
| 318 | * @return array |
||
| 319 | */ |
||
| 320 | public function getBindings() |
||
| 324 | |||
| 325 | /** |
||
| 326 | * Every binded injector. |
||
| 327 | * |
||
| 328 | * @return array |
||
| 329 | */ |
||
| 330 | public function getInjectors() |
||
| 334 | |||
| 335 | /** |
||
| 336 | * Automatically create class. |
||
| 337 | * |
||
| 338 | * @param string $class |
||
| 339 | * @param array $parameters |
||
| 340 | * @param string $context |
||
| 341 | * @return object |
||
| 342 | * @throws AutowireException |
||
| 343 | */ |
||
| 344 | protected function autowire($class, array $parameters, $context) |
||
| 355 | |||
| 356 | /** |
||
| 357 | * Check if given class has associated injector. |
||
| 358 | * |
||
| 359 | * @todo replace with Context on demand |
||
| 360 | * @param \ReflectionClass $reflection |
||
| 361 | * @return bool |
||
| 362 | */ |
||
| 363 | protected function hasInjector(\ReflectionClass $reflection) |
||
| 371 | |||
| 372 | /** |
||
| 373 | * Get injector associated with given class. |
||
| 374 | * |
||
| 375 | * @todo replace with Context on demand |
||
| 376 | * @param \ReflectionClass $reflection |
||
| 377 | * @return InjectorInterface |
||
| 378 | */ |
||
| 379 | protected function getInjector(\ReflectionClass $reflection) |
||
| 387 | |||
| 388 | /** |
||
| 389 | * Create instance of desired class. |
||
| 390 | * |
||
| 391 | * @param string $class |
||
| 392 | * @param array $parameters Constructor parameters. |
||
| 393 | * @param string|null $context |
||
| 394 | * @param \ReflectionClass $reflection Instance of reflection associated with class, |
||
| 395 | * reference. |
||
| 396 | * @return object |
||
| 397 | * @throws ContainerException |
||
| 398 | */ |
||
| 399 | private function createInstance( |
||
| 446 | |||
| 447 | |||
| 448 | /** |
||
| 449 | * Make sure instance conditions are met |
||
| 450 | * |
||
| 451 | * @param object $instance |
||
| 452 | * @param \ReflectionClass $reflector |
||
| 453 | * @param array $parameters |
||
| 454 | * @return object |
||
| 455 | */ |
||
| 456 | private function registerInstance($instance, \ReflectionClass $reflector, array $parameters) |
||
| 472 | } |
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: