| Conditions | 13 |
| Paths | 242 |
| Total Lines | 61 |
| Code Lines | 44 |
| 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 | $context = $method->isStatic() ? 'null' : '$this->__container_proxy_context'; |
||
| 28 | |||
| 29 | $args = []; |
||
| 30 | foreach ($method->getParameters() as $param) { |
||
| 31 | $hasRefs = $hasRefs || $param->isPassedByReference(); |
||
| 32 | $args[] = ($param->isVariadic() ? '...' : '') . '$' . $param->getName(); |
||
| 33 | } |
||
| 34 | |||
| 35 | if (!$hasRefs && !$method->isVariadic()) { |
||
| 36 | $classBody[] = self::renderMethod($method, <<<PHP |
||
| 37 | {$return}\\Spiral\\Core\\Internal\\Proxy\\Resolver::resolve( |
||
| 38 | '{$interface}', |
||
| 39 | $context, |
||
| 40 | ){$call}(...\\func_get_args()); |
||
| 41 | PHP); |
||
| 42 | continue; |
||
| 43 | } |
||
| 44 | |||
| 45 | $argsStr = \implode(', ', $args); |
||
| 46 | |||
| 47 | if ($method->isVariadic()) { |
||
| 48 | $classBody[] = self::renderMethod($method, <<<PHP |
||
| 49 | {$return}\\Spiral\\Core\\Internal\\Proxy\\Resolver::resolve( |
||
| 50 | '{$interface}', |
||
| 51 | $context, |
||
| 52 | ){$call}($argsStr); |
||
| 53 | PHP); |
||
| 54 | continue; |
||
| 55 | } |
||
| 56 | |||
| 57 | $classBody[] = self::renderMethod($method, <<<PHP |
||
| 58 | {$return}\\Spiral\\Core\\Internal\\Proxy\\Resolver::resolve( |
||
| 59 | '{$interface}', |
||
| 60 | $context, |
||
| 61 | ){$call}($argsStr, ...\\array_slice(\\func_get_args(), {$method->getNumberOfParameters()})); |
||
| 62 | PHP); |
||
| 63 | } |
||
| 64 | $bodyStr = \implode("\n\n", $classBody); |
||
| 65 | |||
| 66 | return <<<PHP |
||
| 67 | namespace $classNamespace; |
||
| 68 | |||
| 69 | final class $classShortName implements \\$interface { |
||
| 70 | use \Spiral\Core\Internal\Proxy\ProxyTrait; |
||
| 71 | |||
| 72 | $bodyStr |
||
| 73 | } |
||
| 157 |