Conditions | 3 |
Paths | 4 |
Total Lines | 17 |
Code Lines | 10 |
Lines | 0 |
Ratio | 0 % |
Tests | 12 |
CRAP Score | 3 |
Changes | 0 |
1 | <?php |
||
30 | 3 | public function handle(Exception $e) |
|
31 | { |
||
32 | 3 | $status = 400; |
|
33 | 3 | $error = []; |
|
34 | |||
35 | 3 | $code = $e->getCode(); |
|
36 | 3 | if ($code) { |
|
37 | 3 | $error['code'] = $code; |
|
38 | 3 | } |
|
39 | |||
40 | 3 | $invalidParameter = $e->getInvalidParameter(); |
|
|
|||
41 | 3 | if ($invalidParameter) { |
|
42 | 3 | $error['source'] = ['parameter' => $invalidParameter]; |
|
43 | 3 | } |
|
44 | |||
45 | 3 | return new ResponseBag($status, [$error]); |
|
46 | } |
||
47 | } |
||
48 |
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: