| Conditions | 7 |
| Paths | 7 |
| Total Lines | 55 |
| Code Lines | 47 |
| 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 |
||
| 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(stripAndWordWrap($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 | $description = trim(stripAndWordWrap($param['description'])); |
||
| 71 | $method->addComment("@param {$type} \${$param['name']} {$description}"); |
||
| 72 | $params[$param['name']] = '$' . $param['name']; |
||
| 73 | if ($param['required']) { |
||
| 74 | $method->addParameter($param['name']); |
||
| 75 | } else { |
||
| 76 | $method->addParameter($param['name'], null); |
||
| 77 | } |
||
| 78 | } |
||
| 79 | $paramsStr = self::arrayAsPhpVar($params); |
||
| 80 | $method->addBody("\$params = $paramsStr;"); |
||
| 81 | } |
||
| 82 | $response = new ResponseAbstract(); |
||
| 83 | if (isset($methodData['returns'][0]['result'])) { |
||
| 84 | $returns = $methodData['returns'][0]; |
||
| 85 | $response->renderToFile($returns); |
||
| 86 | $className = $response->formClassNamespace() . '\\' . $response->formClassName(); |
||
| 87 | } elseif ($methodData['returns']) { |
||
| 88 | $methodData['returns']['name'] = $methodData['name']; |
||
| 89 | $response->properties['asd'] = 213; |
||
| 90 | $response->renderToFile($methodData['returns']); |
||
| 91 | $className = $response->formClassNamespace() . '\\' . $response->formClassName(); |
||
| 92 | } else { |
||
| 93 | $className = 'carono\turbotext\Response'; |
||
| 94 | } |
||
| 95 | $method->addComment("@return \\$className|string|\stdClass|\SimpleXMLElement"); |
||
| 96 | $method->addBody("return \$this->getClient()->getContent('api', \$params, '$className');"); |
||
| 97 | } |
||
| 98 | return false; |
||
| 99 | } |
||
| 115 | } |