| Conditions | 11 |
| Paths | 168 |
| Total Lines | 40 |
| Code Lines | 25 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 4 | ||
| Bugs | 1 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 100 | protected function verifyRequestData(): bool |
||
| 101 | { |
||
| 102 | if (!\is_array($this->requestData)) { |
||
|
|
|||
| 103 | $this->throwInvalidException('root object'); |
||
| 104 | } |
||
| 105 | if (!$this->requestData) { // check if empty array, could also mean the json vas invalid |
||
| 106 | $this->throwRequiredException('root object'); |
||
| 107 | } |
||
| 108 | foreach (['jsonapi', 'data'] as $item) { |
||
| 109 | if (isset($this->requestData[$item])) { |
||
| 110 | continue; |
||
| 111 | } |
||
| 112 | |||
| 113 | $this->throwRequiredException($item); |
||
| 114 | } |
||
| 115 | if (!isset($this->requestData['jsonapi']['version'])) { |
||
| 116 | $this->throwRequiredException('jsonapi.version'); |
||
| 117 | } |
||
| 118 | if (Document::VERSION !== $this->requestData['jsonapi']['version']) { |
||
| 119 | throw new ApiException( |
||
| 120 | \sprintf('Unsupported JSON API version: %s', $this->requestData['jsonapi']['version']), |
||
| 121 | ); |
||
| 122 | } |
||
| 123 | if (!\is_array($this->requestData['data'])) { |
||
| 124 | $this->throwInvalidException('data'); |
||
| 125 | } |
||
| 126 | $key = \key($this->requestData['data']); |
||
| 127 | if (0 === $key) { //multiple data objects |
||
| 128 | if (!$this->allowMultipleDataObjects) { |
||
| 129 | throw new ApiException('Multiple data objects not allowed for this endpoint'); |
||
| 130 | } |
||
| 131 | foreach ($this->requestData['data'] as $item) { |
||
| 132 | $this->verifyData($item); |
||
| 133 | } |
||
| 134 | } else { // single data object |
||
| 135 | $this->verifyData($this->requestData['data']); |
||
| 136 | } |
||
| 137 | $this->verifyMeta(); |
||
| 138 | |||
| 139 | return true; |
||
| 140 | } |
||
| 174 |