1 | <?php |
||
8 | class SecureAdmin |
||
9 | { |
||
10 | /** |
||
11 | * The Guard implementation. |
||
12 | * |
||
13 | * @var Guard |
||
14 | */ |
||
15 | protected $auth; |
||
16 | |||
17 | public function __construct(Guard $auth) |
||
21 | |||
22 | /** |
||
23 | * Handle an incoming request. |
||
24 | * |
||
25 | * @param \Illuminate\Http\Request $request |
||
26 | * @param \Closure $next |
||
27 | * |
||
28 | * @return mixed |
||
29 | */ |
||
30 | public function handle($request, Closure $next) |
||
39 | } |
||
40 |
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: