Conditions | 5 |
Paths | 5 |
Total Lines | 20 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
1 | <?php |
||
23 | public static function assertInternalType(string $expected, $actual, string $message = ''): void |
||
24 | { |
||
25 | if (version_compare(phpversion(), '7.2', '>=')) { |
||
26 | switch ($expected) { |
||
27 | case 'string': |
||
28 | static::assertIsString($actual); |
||
29 | break; |
||
30 | case 'object': |
||
31 | static::assertIsObject($actual); |
||
32 | break; |
||
33 | case 'float': |
||
34 | static::assertIsFloat($actual); |
||
35 | break; |
||
36 | default: |
||
37 | throw new Error(); |
||
38 | } |
||
39 | } else { |
||
40 | \PHPUnit\Framework\TestCase::assertInternalType($expected, $actual, $message); |
||
|
|||
41 | } |
||
42 | } |
||
43 | } |
||
44 |
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: