Conditions | 2 |
Paths | 2 |
Total Lines | 17 |
Code Lines | 9 |
Lines | 0 |
Ratio | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
1 | <?php |
||
19 | public function createService(ServiceLocatorInterface $serviceLocator) |
||
20 | { |
||
21 | $serviceManager = $serviceLocator->getServiceLocator(); |
||
|
|||
22 | |||
23 | $config = $serviceManager->get('Config'); |
||
24 | |||
25 | if (!$serviceManager->has($config['phpab']['analytics']['renderer'])) { |
||
26 | throw new RuntimeException(sprintf( |
||
27 | 'The data renderer "%s" does not exists.', |
||
28 | $config['phpab']['analytics']['renderer'] |
||
29 | )); |
||
30 | } |
||
31 | |||
32 | $renderer = $serviceManager->get($config['phpab']['analytics']['renderer']); |
||
33 | |||
34 | return new Script($renderer); |
||
35 | } |
||
36 | } |
||
37 |
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: