Conditions | 6 |
Paths | 16 |
Total Lines | 28 |
Code Lines | 12 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
1 | <?php declare(strict_types=1); |
||
40 | public function code(int $indentation = 0): string |
||
41 | { |
||
42 | $code = ''; |
||
43 | |||
44 | // Add child if groups code |
||
45 | if (array_key_exists(self::class, $this->generatedCode)) { |
||
46 | $code .= $this->generatedCode[self::class]; |
||
47 | } |
||
48 | |||
49 | // Add conditions code |
||
50 | if (array_key_exists(ConditionGenerator::class, $this->generatedCode)) { |
||
51 | $code .= $this->generatedCode[ConditionGenerator::class]; |
||
52 | } |
||
53 | |||
54 | // Close condition block |
||
55 | if ($code !== '') { |
||
56 | $code .= "\n".'}'; |
||
57 | } |
||
58 | |||
59 | // Separate code in lines and add to parent code |
||
60 | if ($code !== '') { |
||
61 | foreach (explode("\n", $code) as $codeLine) { |
||
62 | $this->parent->defLine($codeLine); |
||
63 | } |
||
64 | } |
||
65 | |||
66 | return $code; |
||
67 | } |
||
68 | } |
||
69 |
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: