| Conditions | 11 |
| Paths | 85 |
| Total Lines | 51 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 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 |
||
| 74 | public function __call(string $name, array $arguments):ResponseInterface{ |
||
| 75 | |||
| 76 | if(!$this->endpoints instanceof EndpointMapInterface || !$this->endpoints->__isset($name)){ |
||
| 77 | throw new ApiClientException('endpoint not found'); |
||
| 78 | } |
||
| 79 | |||
| 80 | $m = $this->endpoints->{$name}; |
||
| 81 | |||
| 82 | $endpoint = $this->endpoints->API_BASE.$m['path']; |
||
| 83 | $method = $m['method'] ?? 'GET'; |
||
| 84 | $body = null; |
||
| 85 | $headers = isset($m['headers']) && is_array($m['headers']) ? $m['headers'] : []; |
||
| 86 | $path_elements = $m['path_elements'] ?? []; |
||
| 87 | $params_in_url = count($path_elements); |
||
| 88 | $params = $arguments[$params_in_url] ?? []; |
||
| 89 | $urlparams = array_slice($arguments, 0, $params_in_url); |
||
| 90 | |||
| 91 | if($params_in_url > 0){ |
||
| 92 | |||
| 93 | if(count($urlparams) < $params_in_url){ |
||
| 94 | throw new APIClientException('too few URL params, required: '.implode(', ', $path_elements)); |
||
| 95 | } |
||
| 96 | |||
| 97 | $endpoint = sprintf($endpoint, ...$urlparams); |
||
| 98 | } |
||
| 99 | |||
| 100 | if(in_array($method, ['DELETE', 'POST', 'PATCH', 'PUT'], true)){ |
||
| 101 | $body = $arguments[$params_in_url + 1] ?? $params; |
||
| 102 | |||
| 103 | if($params === $body){ |
||
| 104 | $params = []; |
||
| 105 | } |
||
| 106 | |||
| 107 | if(is_iterable($body)){ |
||
| 108 | $body = Psr7\clean_query_params($body); |
||
| 109 | } |
||
| 110 | |||
| 111 | } |
||
| 112 | |||
| 113 | $params = Psr7\clean_query_params($params); |
||
| 114 | |||
| 115 | if($this->logger instanceof LoggerInterface){ |
||
| 116 | |||
| 117 | $this->logger->debug(get_called_class().'::__call() -> '.(new ReflectionClass($this))->getShortName().'::'.$name.'()', [ |
||
| 118 | '$endpoint' => $endpoint, '$method' => $method, '$params' => $params, '$body' => $body, '$headers' => $headers, |
||
| 119 | ]); |
||
| 120 | |||
| 121 | } |
||
| 122 | |||
| 123 | return $this->request($endpoint, $method, $params, $body, $headers); |
||
| 124 | } |
||
| 125 | |||
| 127 |
This check compares the return type specified in the
@returnannotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.