| Conditions | 6 |
| Paths | 5 |
| Total Lines | 23 |
| Lines | 0 |
| Ratio | 0 % |
| Tests | 0 |
| CRAP Score | 42 |
| Changes | 0 | ||
| 1 | <?php |
||
| 53 | public function getRequest(Array $headers = [], $body = '') |
||
| 54 | { |
||
| 55 | if ($this->id === null) { |
||
| 56 | throw new RuntimeException('You must provide the Droplet ID you want to perform an action on'); |
||
| 57 | } |
||
| 58 | |||
| 59 | if (method_exists($this, 'getBody') && $body === '') { |
||
| 60 | $body = $this->getBody(); |
||
|
|
|||
| 61 | } |
||
| 62 | |||
| 63 | if ($body === '' || json_decode($body, true) === []) { |
||
| 64 | throw new RuntimeException('Body cannot be empty'); |
||
| 65 | } |
||
| 66 | |||
| 67 | $endpoint = str_replace('[:id:]', $this->id, self::ENDPOINT); |
||
| 68 | |||
| 69 | return new Request( |
||
| 70 | static::METHOD, |
||
| 71 | $endpoint, |
||
| 72 | $headers, |
||
| 73 | $body |
||
| 74 | ); |
||
| 75 | } |
||
| 76 | } |
||
| 77 |
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: