Conditions | 2 |
Paths | 2 |
Total Lines | 30 |
Code Lines | 15 |
Lines | 0 |
Ratio | 0 % |
Changes | 2 | ||
Bugs | 0 | Features | 1 |
1 | <?php |
||
18 | public function createService(ServiceLocatorInterface $cm) |
||
19 | { |
||
20 | $sl = $cm->getServiceLocator(); |
||
|
|||
21 | |||
22 | $plugins = $sl->get('ControllerPluginManager'); |
||
23 | |||
24 | /** @var Application $app */ |
||
25 | $app = $sl->get('Application'); |
||
26 | $event = $app->getMvcEvent(); |
||
27 | |||
28 | /** @var RouteMatch $routeMatch */ |
||
29 | $routeMatch = $event->getRouteMatch(); |
||
30 | |||
31 | if ($routeMatch->getParam('layout')) { |
||
32 | $viewModel = $event->getViewModel(); |
||
33 | $viewModel->setTemplate($routeMatch->getParam('layout')); |
||
34 | } |
||
35 | |||
36 | /** @var Redirect $redirect */ |
||
37 | $redirect = $plugins->get('redirect'); |
||
38 | |||
39 | $controller = new IndexController( |
||
40 | $sl->get(InteractiveAuth::class), |
||
41 | $redirect |
||
42 | ); |
||
43 | |||
44 | $redirect->setController($controller); |
||
45 | |||
46 | return $controller; |
||
47 | } |
||
48 | } |
||
49 |
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: