| Conditions | 3 |
| Paths | 1 |
| Total Lines | 17 |
| Lines | 0 |
| Ratio | 0 % |
| Tests | 0 |
| CRAP Score | 12 |
| Changes | 0 | ||
| 1 | <?php |
||
| 14 | public function register() |
||
| 15 | { |
||
| 16 | $this->getContainer()->share(Client::class, function () { |
||
|
|
|||
| 17 | $headers = [ |
||
| 18 | 'transfer-encoding' => 'chunked', |
||
| 19 | ]; |
||
| 20 | if (array_key_exists('HTTP_X_REAL_IP', $_SERVER)) { |
||
| 21 | $headers['x-real-ip'] = $_SERVER['HTTP_X_REAL_IP']; |
||
| 22 | } |
||
| 23 | if (array_key_exists('HTTP_X_SESSION', $_SERVER)) { |
||
| 24 | $headers['x-session'] = $_SERVER['HTTP_X_SESSION']; |
||
| 25 | } |
||
| 26 | return new Client([ |
||
| 27 | 'headers' => $headers |
||
| 28 | ]); |
||
| 29 | }); |
||
| 30 | } |
||
| 31 | } |
||
| 32 |
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: