1 | <?php |
||
12 | class RouteGroup implements RouteInterface { |
||
13 | use HasRoutesTrait { |
||
14 | route as traitRoute; |
||
15 | } |
||
16 | use HasMiddlewareTrait { |
||
17 | addMiddleware as traitAddMiddleware; |
||
18 | } |
||
19 | |||
20 | /** |
||
21 | * Route target |
||
22 | * |
||
23 | * @var ConditionInterface |
||
24 | */ |
||
25 | protected $target = null; |
||
26 | |||
27 | /** |
||
28 | * Constructor |
||
29 | * |
||
30 | * @param string|ConditionInterface $target |
||
31 | * @param Closure $callable |
||
32 | */ |
||
33 | public function __construct( $target, Closure $callable ) { |
||
46 | |||
47 | /** |
||
48 | * Return the first child route which is satisfied |
||
49 | * |
||
50 | * @return RouteInterface|null |
||
51 | */ |
||
52 | protected function getSatisfiedRoute( Request $request ) { |
||
61 | |||
62 | /** |
||
63 | * {@inheritDoc} |
||
64 | */ |
||
65 | public function satisfied( Request $request ) { |
||
69 | |||
70 | /** |
||
71 | * {@inheritDoc} |
||
72 | */ |
||
73 | public function handle( Request $request, $template ) { |
||
77 | |||
78 | /** |
||
79 | * {@inheritDoc} |
||
80 | */ |
||
81 | public function route( $methods, $target, $handler ) { |
||
93 | |||
94 | /** |
||
95 | * {@inheritDoc} |
||
96 | */ |
||
97 | public function addMiddleware( $middleware ) { |
||
106 | } |
||
107 |
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: