| Conditions | 12 |
| Paths | 122 |
| Total Lines | 56 |
| Code Lines | 38 |
| 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 |
||
| 12 | public static function renderClass(\ReflectionClass $type, $className): string |
||
| 13 | { |
||
| 14 | $classShortName = \substr($className, \strrpos($className, '\\') + 1); |
||
| 15 | $classNamespace = \substr($className, 0, \strrpos($className, '\\')); |
||
| 16 | |||
| 17 | $interface = $type->getName(); |
||
| 18 | $classBody = []; |
||
| 19 | foreach ($type->getMethods() as $method) { |
||
| 20 | if ($method->isConstructor()) { |
||
| 21 | continue; |
||
| 22 | } |
||
| 23 | |||
| 24 | $hasRefs = false; |
||
| 25 | $return = $method->hasReturnType() && (string)$method->getReturnType() === 'void' ? '' : 'return '; |
||
| 26 | $call = ($method->isStatic() ? '::' : '->') . $method->getName(); |
||
| 27 | |||
| 28 | $args = []; |
||
| 29 | foreach ($method->getParameters() as $param) { |
||
| 30 | $hasRefs = $hasRefs || $param->isPassedByReference(); |
||
| 31 | $args[] = ($param->isVariadic() ? '...' : '') . '$'. $param->getName(); |
||
| 32 | } |
||
| 33 | |||
| 34 | if (!$hasRefs && !$method->isVariadic()) { |
||
| 35 | $classBody[] = self::renderMethod($method, <<<PHP |
||
| 36 | {$return}\\Spiral\\Core\\Internal\\Proxy\\Resolver::resolve('{$interface}') |
||
| 37 | {$call}(...\\func_get_args()); |
||
| 38 | PHP); |
||
| 39 | continue; |
||
| 40 | } |
||
| 41 | |||
| 42 | $argsStr = \implode(', ', $args); |
||
| 43 | |||
| 44 | if ($method->isVariadic()) { |
||
| 45 | $classBody[] = self::renderMethod($method, <<<PHP |
||
| 46 | {$return}\\Spiral\\Core\\Internal\\Proxy\\Resolver::resolve('{$interface}') |
||
| 47 | {$call}($argsStr); |
||
| 48 | PHP); |
||
| 49 | continue; |
||
| 50 | } |
||
| 51 | |||
| 52 | $classBody[] = self::renderMethod($method, <<<PHP |
||
| 53 | {$return}\\Spiral\\Core\\Internal\\Proxy\\Resolver::resolve('{$interface}') |
||
| 54 | {$call}($argsStr, ...\\array_slice(\\func_get_args(), {$method->getNumberOfParameters()})); |
||
| 55 | PHP); |
||
| 56 | } |
||
| 57 | $bodyStr = \implode("\n\n", $classBody); |
||
| 58 | |||
| 59 | echo $bodyStr; |
||
| 60 | |||
| 61 | return <<<PHP |
||
| 62 | namespace $classNamespace; |
||
| 63 | |||
| 64 | final class $classShortName implements \\$interface { |
||
| 65 | use \Spiral\Core\Internal\Proxy\ProxyTrait; |
||
| 66 | |||
| 67 | $bodyStr |
||
| 68 | } |
||
| 152 |