| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  |  * ZfDebugModule. WebUI and Console commands for debugging ZF2 apps. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  * @license http://www.opensource.org/licenses/mit-license.html MIT License | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  * @copyright 2016 Vítor Brandão <[email protected]> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | namespace Noiselabs\ZfDebugModule\Factory\Controller\Console; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | use Noiselabs\ZfDebugModule\Controller\Console\RoutesController; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | use Noiselabs\ZfDebugModule\Factory\Util\Routing\RouteCollectionFactory; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | use Noiselabs\ZfDebugModule\Factory\Util\Routing\RouteMatcherFactory; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | use Noiselabs\ZfDebugModule\Util\Routing\RouteCollection; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | use Noiselabs\ZfDebugModule\Util\Routing\RouteMatcher; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | use Zend\Mvc\Controller\ControllerManager; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | use Zend\ServiceManager\FactoryInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  | use Zend\ServiceManager\ServiceLocatorInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |  * Console\RoutesControllerFactory creates instances of Console\RoutesController. | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 22 |  |  |  */ | 
            
                                                                        
                            
            
                                    
            
            
                | 23 |  |  | class RoutesControllerFactory implements FactoryInterface | 
            
                                                                        
                            
            
                                    
            
            
                | 24 |  |  | { | 
            
                                                                        
                            
            
                                    
            
            
                | 25 |  |  |     const SERVICE_NAME = RoutesController::class; | 
            
                                                                        
                            
            
                                    
            
            
                | 26 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 27 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 28 |  |  |      * @param ServiceLocatorInterface|ControllerManager $serviceLocator | 
            
                                                                        
                            
            
                                    
            
            
                | 29 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 30 |  |  |      * @return RoutesController | 
            
                                                                        
                            
            
                                    
            
            
                | 31 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 | 5 |  |     public function createService(ServiceLocatorInterface $serviceLocator) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |         /** @var ServiceLocatorInterface $mainServiceManager */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 | 5 |  |         $mainServiceManager = $serviceLocator->getServiceLocator(); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |         /** @var RouteCollection $routeCollection */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 | 5 |  |         $routeCollection = $mainServiceManager->get(RouteCollectionFactory::SERVICE_NAME); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |         /** @var RouteMatcher $routeMatcher */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 | 5 |  |         $routeMatcher = $mainServiceManager->get(RouteMatcherFactory::SERVICE_NAME); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 | 5 |  |         $console = $mainServiceManager->get('Console'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 | 5 |  |         return new RoutesController($routeCollection, $routeMatcher, $console); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 44 |  |  |     } | 
            
                                                        
            
                                    
            
            
                | 45 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 46 |  |  |  | 
            
                        
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: