1 | <?php |
||
16 | class Csrf |
||
17 | { |
||
18 | /** |
||
19 | * Generates a token and save it to session |
||
20 | * |
||
21 | * @param string $identifier |
||
22 | * |
||
23 | * @return string |
||
24 | * |
||
25 | * @throws ServiceNotFoundException |
||
26 | */ |
||
27 | public static function getToken(string $identifier = '') :string |
||
38 | |||
39 | /** |
||
40 | * Check if token is valid |
||
41 | * |
||
42 | * @param string $token |
||
43 | * @param string $identifier |
||
44 | * |
||
45 | * @return bool |
||
46 | * |
||
47 | * @throws ServiceNotFoundException |
||
48 | */ |
||
49 | public static function isValid(string $token, string $identifier = '') :bool |
||
61 | |||
62 | /** |
||
63 | * Saves token into session |
||
64 | * |
||
65 | * @param string $token |
||
66 | * @param string $identifier |
||
67 | * |
||
68 | * @return void |
||
69 | * |
||
70 | * @throws ServiceNotFoundException |
||
71 | */ |
||
72 | private static function saveToSession(string $token, string $identifier = '') |
||
76 | |||
77 | /** |
||
78 | * @return SessionManagerService|ServiceInterface |
||
79 | * |
||
80 | * @throws ServiceNotFoundException |
||
81 | */ |
||
82 | private static function _getSessionManager() |
||
86 | |||
87 | } |
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: