| Conditions | 13 |
| Paths | 10 |
| Total Lines | 26 |
| Code Lines | 16 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 0 | 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 |
||
| 15 | public function match(mixed $body): bool |
||
| 16 | { |
||
| 17 | $hasFormData = false; |
||
| 18 | foreach ($this->structure as $parameter) { |
||
| 19 | if ($parameter['in'] === "body") { |
||
| 20 | if (isset($parameter['required']) && $parameter['required'] === true && empty($body)) { |
||
| 21 | throw new RequiredArgumentNotFound('The body is required but it is empty'); |
||
| 22 | } |
||
| 23 | return $this->matchSchema($this->name, $parameter['schema'], $body) ?? false; |
||
| 24 | } |
||
| 25 | if ($parameter['in'] === "formData") { |
||
| 26 | $hasFormData = true; |
||
| 27 | if (isset($parameter['required']) && $parameter['required'] === true && !isset($body[$parameter['name']])) { |
||
| 28 | throw new RequiredArgumentNotFound("The formData parameter '{$parameter['name']}' is required but it isn't found. "); |
||
| 29 | } |
||
| 30 | if (!$this->matchTypes($parameter['name'], $parameter, ($body[$parameter['name']] ?? null))) { |
||
|
|
|||
| 31 | throw new NotMatchedException("The formData parameter '{$parameter['name']}' not match with the specification"); |
||
| 32 | } |
||
| 33 | } |
||
| 34 | } |
||
| 35 | |||
| 36 | if (!empty($body) && !$hasFormData) { |
||
| 37 | throw new InvalidDefinitionException('Body is passed but there is no request body definition'); |
||
| 38 | } |
||
| 39 | |||
| 40 | return false; |
||
| 41 | } |
||
| 43 |
If an expression can have both
false, andnullas possible values. It is generally a good practice to always use strict comparison to clearly distinguish between those two values.