| Conditions | 3 |
| Paths | 4 |
| Total Lines | 22 |
| Code Lines | 9 |
| Lines | 3 |
| Ratio | 13.64 % |
| Changes | 0 | ||
| 1 | <?php declare(strict_types=1); |
||
| 39 | public function code(int $indentation = 0): string |
||
| 40 | { |
||
| 41 | // Close condition statement |
||
| 42 | $this->code[] = $this->indentation($indentation) . '}'; |
||
| 43 | |||
| 44 | $code = implode("\n" . $this->indentation($indentation), $this->code); |
||
| 45 | |||
| 46 | // Add conditions code |
||
| 47 | if (array_key_exists(ConditionGenerator::class, $this->generatedCode)) { |
||
| 48 | $code = $this->generatedCode[ConditionGenerator::class] . "\n" . $code; |
||
| 49 | } |
||
| 50 | |||
| 51 | // Add comments |
||
| 52 | View Code Duplication | if (array_key_exists(FunctionCommentsGenerator::class, $this->generatedCode)) { |
|
| 53 | $code = $this->generatedCode[FunctionCommentsGenerator::class] . "\n" . $code; |
||
| 54 | } |
||
| 55 | |||
| 56 | // Pass code to parent to preserve order |
||
| 57 | $this->parent->defLine($code); |
||
| 58 | |||
| 59 | return $code; |
||
| 60 | } |
||
| 61 | } |
||
| 62 |
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: