| Conditions | 2 |
| Paths | 2 |
| Total Lines | 14 |
| Lines | 0 |
| Ratio | 0 % |
| Tests | 7 |
| CRAP Score | 2 |
| Changes | 0 | ||
| 1 | <?php |
||
| 19 | 9 | public function resolve(DefinitionInterface $definition) |
|
| 20 | { |
||
| 21 | 9 | $this->validate($definition, ClassDefinition::class); |
|
| 22 | |||
| 23 | 9 | $class = $definition->getClass(); |
|
|
|
|||
| 24 | |||
| 25 | 9 | if (!class_exists($class)) { |
|
| 26 | 1 | throw ClassNotFoundException::create($class); |
|
| 27 | } |
||
| 28 | |||
| 29 | 8 | $params = $this->getConstructorParams($class); |
|
| 30 | |||
| 31 | 7 | return new $class(...$params); |
|
| 32 | } |
||
| 33 | } |
||
| 34 |
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: