| Conditions | 2 |
| Paths | 2 |
| Total Lines | 17 |
| Lines | 0 |
| Ratio | 0 % |
| Tests | 0 |
| CRAP Score | 6 |
| Changes | 0 | ||
| 1 | <?php |
||
| 24 | public function render(array $params = []): string |
||
| 25 | { |
||
| 26 | $fullPath = Module::getInstance()->getStorage()->getLocalPath($this->path); |
||
|
|
|||
| 27 | $mimeType = mime_content_type($fullPath); |
||
| 28 | |||
| 29 | if ($mimeType === 'text/plain') { |
||
| 30 | $text = Module::getInstance()->getStorage()->read($this->path); |
||
| 31 | |||
| 32 | return "<pre>$text</pre>"; |
||
| 33 | } |
||
| 34 | |||
| 35 | $response = Yii::$app->response; |
||
| 36 | $response->format = $response::FORMAT_RAW; |
||
| 37 | $response->headers->set('Content-Type', $mimeType); |
||
| 38 | readfile($fullPath); |
||
| 39 | Yii::$app->end(); |
||
| 40 | } |
||
| 41 | |||
| 47 |
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 implementation 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 interface: