| Conditions | 7 |
| Paths | 13 |
| Total Lines | 53 |
| Code Lines | 45 |
| 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 |
||
| 44 | public function methods() |
||
| 45 | { |
||
| 46 | foreach ($this->params['methods'] as $methodData) { |
||
| 47 | $methodName = formMethodName($methodData['name']); |
||
| 48 | $method = $this->phpClass->addMethod($methodName); |
||
| 49 | $method->addComment(trim($methodData['description'])); |
||
| 50 | $params = []; |
||
| 51 | $params['action'] = $methodData['name']; |
||
| 52 | if (count($methodData['params']) > 4) { |
||
| 53 | $config = new ConfigAbstract(); |
||
| 54 | $config->renderToFile($methodData); |
||
| 55 | $className = '\\' . $config->formClassNamespace() . '\\' . $config->formClassName(); |
||
| 56 | $method->addParameter('config'); |
||
| 57 | $method->addComment("@param $className|array \$config"); |
||
| 58 | |||
| 59 | $body = <<<PHP |
||
| 60 | foreach ((\$config instanceof \carono\\turbotext\ConfigAbstract ? \$config->toArray() : \$config) as \$key => \$value) { |
||
| 61 | \$params[\$key] = \$value; |
||
| 62 | } |
||
| 63 | PHP; |
||
| 64 | $paramsStr = self::arrayAsPhpVar($params); |
||
| 65 | $method->addBody("\$params = $paramsStr;"); |
||
| 66 | $method->addBody($body); |
||
| 67 | } else { |
||
| 68 | foreach ($methodData['params'] as $param) { |
||
| 69 | $type = formParamType($param['type']); |
||
| 70 | $method->addComment("@param {$type} \${$param['name']} {$param['description']}"); |
||
| 71 | $params[$param['name']] = '$' . $param['name']; |
||
| 72 | if ($param['required']) { |
||
| 73 | $method->addParameter($param['name']); |
||
| 74 | } else { |
||
| 75 | $method->addParameter($param['name'], null); |
||
| 76 | } |
||
| 77 | } |
||
| 78 | $paramsStr = self::arrayAsPhpVar($params); |
||
| 79 | $method->addBody("\$params = $paramsStr;"); |
||
| 80 | } |
||
| 81 | $response = new ResponseAbstract(); |
||
| 82 | if (isset($methodData['returns'][0]['result'])) { |
||
| 83 | $response->renderToFile($methodData['returns'][0]); |
||
| 84 | $className = $response->formClassNamespace() . '\\' . $response->formClassName(); |
||
| 85 | } elseif ($methodData['returns']) { |
||
| 86 | $methodData['returns']['name'] = $methodData['name']; |
||
| 87 | $response->properties['asd'] = 213; |
||
| 88 | $response->renderToFile($methodData['returns']); |
||
| 89 | $className = $response->formClassNamespace() . '\\' . $response->formClassName(); |
||
| 90 | } else { |
||
| 91 | $className = 'carono\turbotext\Response'; |
||
| 92 | } |
||
| 93 | $method->addComment("@return \\$className|string|\stdClass|\SimpleXMLElement"); |
||
| 94 | $method->addBody("return \$this->getClient()->getContent('api', \$params, '$className');"); |
||
| 95 | } |
||
| 96 | return false; |
||
| 97 | } |
||
| 113 | } |