| Conditions | 13 |
| Paths | 19 |
| Total Lines | 48 |
| 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 |
||
| 46 | public function create(string $returntype):string{ |
||
| 47 | |||
| 48 | if(!$this->endpointMap instanceof EndpointMap){ |
||
| 49 | throw new ApiClientException('invalid endpoint map'); |
||
| 50 | } |
||
| 51 | |||
| 52 | $str = '/**'.PHP_EOL; |
||
| 53 | |||
| 54 | foreach($this->endpointMap->toArray() as $methodName => $params){ |
||
| 55 | |||
| 56 | if($methodName === 'API_BASE'){ |
||
| 57 | continue; |
||
| 58 | } |
||
| 59 | |||
| 60 | $args = []; |
||
| 61 | |||
| 62 | if(isset($params['path_elements']) && count($params['path_elements']) > 0){ |
||
| 63 | |||
| 64 | foreach($params['path_elements'] as $i){ |
||
| 65 | $args[] = 'string $'.$i; |
||
| 66 | } |
||
| 67 | |||
| 68 | } |
||
| 69 | |||
| 70 | if(isset($params['query']) && !empty($params['query'])){ |
||
| 71 | $args[] = 'array $params = [\''.implode('\', \'', $params['query']).'\']'; |
||
| 72 | } |
||
| 73 | |||
| 74 | if(isset($params['method']) && in_array($params['method'], ['PATCH', 'POST', 'PUT', 'DELETE'], true)){ |
||
| 75 | |||
| 76 | if($params['body'] !== null){ |
||
| 77 | $args[] = is_array($params['body']) ? 'array $body = [\''.implode('\', \'', $params['body']).'\']' : 'array $body = []'; |
||
| 78 | } |
||
| 79 | |||
| 80 | } |
||
| 81 | |||
| 82 | $str.= ' * @method \\'.$returntype.' '.$methodName.'('.implode(', ', $args).')'.PHP_EOL; |
||
| 83 | } |
||
| 84 | |||
| 85 | $str .= ' *'.'/'.PHP_EOL; |
||
| 86 | |||
| 87 | $reflection = new ReflectionClass($this->provider); |
||
| 88 | $classfile = $reflection->getFileName(); |
||
| 89 | |||
| 90 | file_put_contents($classfile, str_replace($reflection->getDocComment().PHP_EOL, $str, file_get_contents($classfile))); |
||
| 91 | |||
| 92 | return $str; |
||
| 93 | } |
||
| 94 | |||
| 158 |