1 | <?php |
||
18 | class AuthenticatorService implements ServiceInterface |
||
19 | { |
||
20 | |||
21 | /** @var Controller */ |
||
22 | protected $controller; |
||
23 | |||
24 | /** @var DbService */ |
||
25 | protected $orm; |
||
26 | |||
27 | /** @var Config */ |
||
28 | protected $config; |
||
29 | |||
30 | /** @var string */ |
||
31 | protected $redirectAfterAuth; |
||
32 | |||
33 | /** |
||
34 | * Authenticator constructor. |
||
35 | * |
||
36 | * @param Controller $controller |
||
37 | * @param Config $config |
||
38 | */ |
||
39 | public function __construct(Controller $controller, Config $config) |
||
44 | |||
45 | /** |
||
46 | * Login user with given entity |
||
47 | * |
||
48 | * @param Entity $user |
||
49 | * @param bool $shouldBeActive |
||
50 | * @param string $redirectUrl |
||
51 | * |
||
52 | * @return bool |
||
53 | */ |
||
54 | public function loginUser(Entity $user, $shouldBeActive = true, $redirectUrl = '') |
||
96 | |||
97 | /** |
||
98 | * @return bool |
||
99 | */ |
||
100 | public function redirectToAuthentication() |
||
108 | |||
109 | /** |
||
110 | * @param array $roles |
||
111 | * @return bool |
||
112 | */ |
||
113 | public function isPermitted(array $roles): bool |
||
132 | |||
133 | /** |
||
134 | * @param Entity $user |
||
135 | * @codeCoverageIgnore |
||
136 | */ |
||
137 | public function saveUserInSession(Entity $user) |
||
141 | |||
142 | /** |
||
143 | * @param string $entity |
||
144 | * @return Entity * |
||
145 | * @codeCoverageIgnore |
||
146 | */ |
||
147 | public function getUserFromSession(string $entity = '') |
||
164 | |||
165 | } |
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: