1 | <?php |
||
21 | class TokenAuth |
||
22 | { |
||
23 | /** |
||
24 | * @var JWTInterface |
||
25 | */ |
||
26 | private $jwt; |
||
27 | /** |
||
28 | * @var Encrypter |
||
29 | */ |
||
30 | private $encrypter; |
||
31 | /** |
||
32 | * @var Guard |
||
33 | */ |
||
34 | private $guard; |
||
35 | |||
36 | public function __construct(JWTInterface $jwt, Encrypter $encrypter, Guard $guard) |
||
43 | |||
44 | /** |
||
45 | * @param string $token |
||
46 | * @return bool |
||
47 | */ |
||
48 | public function attempt(string $token) : bool |
||
61 | |||
62 | /** |
||
63 | * @param string $token |
||
64 | * @throws TokenInvalidException |
||
65 | * @throws DecryptException |
||
66 | * @return array |
||
67 | */ |
||
68 | public function decode(string $token) : array |
||
76 | |||
77 | /** |
||
78 | * @return bool |
||
79 | */ |
||
80 | public function check(): bool |
||
84 | |||
85 | /** |
||
86 | * @return User |
||
87 | */ |
||
88 | public function user() : User |
||
92 | |||
93 | /** |
||
94 | * @return mixed |
||
95 | */ |
||
96 | public function logout() |
||
100 | |||
101 | /** |
||
102 | * @param array $credentials |
||
103 | * @return mixed |
||
104 | */ |
||
105 | public function once(array $credentials) |
||
109 | |||
110 | /** |
||
111 | * @param User $user |
||
112 | * @return string |
||
113 | */ |
||
114 | public function fromUser(User $user) : string |
||
121 | |||
122 | /** |
||
123 | * @param array $inputs |
||
124 | * @return string |
||
125 | */ |
||
126 | public function encode(array $inputs) : string |
||
133 | } |
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: