Conditions | 4 |
Paths | 3 |
Total Lines | 19 |
Lines | 0 |
Ratio | 0 % |
Tests | 10 |
CRAP Score | 4 |
Changes | 0 |
1 | <?php |
||
30 | 5 | public function resolve(DefinitionInterface $definition) |
|
31 | { |
||
32 | 5 | $this->validate($definition, FactoryDefinition::class); |
|
33 | |||
34 | 5 | $factoryClass = $definition->getFactoryClass(); |
|
|
|||
35 | |||
36 | 5 | if (!class_exists($factoryClass)) { |
|
37 | 1 | throw ClassNotFoundException::create($factoryClass); |
|
38 | } |
||
39 | |||
40 | 4 | $factoryInterface = FactoryInterface::class; |
|
41 | 4 | $interfaces = class_implements($factoryClass); |
|
42 | |||
43 | 4 | if (empty($interfaces) || !in_array($factoryInterface, $interfaces, true)) { |
|
44 | 1 | throw InvalidFactoryException::create($factoryClass, $factoryInterface); |
|
45 | } |
||
46 | |||
47 | 3 | return (new $factoryClass)($this->getContainer()); |
|
48 | } |
||
49 | } |
||
50 |
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: