| Conditions | 4 |
| Paths | 3 |
| Total Lines | 19 |
| Lines | 0 |
| Ratio | 0 % |
| Tests | 10 |
| CRAP Score | 4 |
| Changes | 0 | ||
| 1 | <?php |
||
| 21 | 4 | public function resolve(DefinitionInterface $definition) |
|
| 22 | { |
||
| 23 | 4 | $this->validate($definition, FactoryDefinition::class); |
|
| 24 | |||
| 25 | 4 | $factoryClass = $definition->getFactoryClass(); |
|
|
|
|||
| 26 | |||
| 27 | 4 | if (!class_exists($factoryClass)) { |
|
| 28 | 1 | throw ClassNotFoundException::create($factoryClass); |
|
| 29 | } |
||
| 30 | |||
| 31 | 3 | $factoryInterface = FactoryInterface::class; |
|
| 32 | 3 | $interfaces = class_implements($factoryClass); |
|
| 33 | |||
| 34 | 3 | if (empty($interfaces) || !in_array($factoryInterface, $interfaces, true)) { |
|
| 35 | 1 | throw InvalidFactoryException::create($factoryClass, $factoryInterface); |
|
| 36 | } |
||
| 37 | |||
| 38 | 2 | return (new $factoryClass)($this->getContainer()); |
|
| 39 | } |
||
| 40 | } |
||
| 41 |
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: