1 | <?php |
||
13 | class JSONServiceProvider implements ServiceProviderInterface |
||
14 | { |
||
15 | private $app; |
||
16 | |||
17 | public function register(Container $app) |
||
24 | |||
25 | /** |
||
26 | * Gère le JSON dans le body d'une requête |
||
27 | * |
||
28 | * @param Request $request |
||
29 | */ |
||
30 | public function jsonInputHandler(Request $request) |
||
45 | |||
46 | /** |
||
47 | * Gère les erreurs et retourne un truc standardisé |
||
48 | * |
||
49 | * Le code d'erreur HTTP est gardé, mais pas forcément le message, et sera encapsuler dans une réponse JSON |
||
50 | * Le message de l'exception n'est retourné que si ce n'est pas une erreur 500 |
||
51 | * ou que le `debug` est activé |
||
52 | * |
||
53 | * @param \Exception $exception |
||
54 | * @param integer $code HTTP status code (100 <= $code <= 50x) |
||
55 | * @return \Symfony\Component\HttpFoundation\JsonResponse |
||
56 | */ |
||
57 | public function errorHandler(\Exception $exception, $code) |
||
74 | |||
75 | /** |
||
76 | * @param integer $code |
||
77 | * @param null|\Exception $exception |
||
78 | * @return integer |
||
79 | */ |
||
80 | private function sanitizeExceptionCode($code, \Exception $exception = null) |
||
96 | } |
||
97 |
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 sub-classes 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 parent class: