| Conditions | 3 |
| Paths | 2 |
| Total Lines | 14 |
| Lines | 0 |
| Ratio | 0 % |
| Tests | 7 |
| CRAP Score | 3.0175 |
| Changes | 0 | ||
| 1 | <?php |
||
| 20 | 2 | public function resolve(DefinitionInterface $definition) |
|
| 21 | { |
||
| 22 | 2 | $this->validate($definition, FactoryDefinition::class); |
|
| 23 | |||
| 24 | 2 | $factoryClass = $definition->getFactoryClass(); |
|
|
|
|||
| 25 | 2 | $factoryInterface = FactoryInterface::class; |
|
| 26 | 2 | $interfaces = class_implements($factoryClass); |
|
| 27 | |||
| 28 | 2 | if (empty($interfaces) || !in_array($factoryInterface, $interfaces, true)) { |
|
| 29 | throw new ContainerException("$factoryClass must implement $factoryInterface"); |
||
| 30 | } |
||
| 31 | |||
| 32 | 2 | return (new $factoryClass)($this->getContainer()); |
|
| 33 | } |
||
| 34 | } |
||
| 35 |
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the interface: