Conditions | 9 |
Paths | 20 |
Total Lines | 53 |
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 |
||
45 | private function createWrapper(ReflectionMethod $method, array $supportedMethods, ClassType $newClass, $methodNameTemplate, $typeTemplate) |
||
46 | { |
||
47 | $newMethodName = sprintf($methodNameTemplate, ucfirst($method->name)); |
||
48 | |||
49 | if (!in_array($newMethodName, $supportedMethods, true)) { |
||
50 | return; |
||
51 | } |
||
52 | |||
53 | $comment = $method->getDocComment(); |
||
54 | |||
55 | $p = DocComment::parse($comment); |
||
56 | |||
57 | if (!in_array('psalm-assert', array_keys($p['specials']))) { |
||
58 | return; |
||
59 | } |
||
60 | |||
61 | $newMethod = $newClass->addMethod($newMethodName) |
||
62 | ->setStatic() |
||
63 | ; |
||
64 | |||
65 | $parameters = $method->getParameters(); |
||
66 | |||
67 | $parametersCall = []; |
||
68 | foreach ($parameters as $parameter) { |
||
69 | $newParameter = $newMethod->addParameter($parameter->name); |
||
70 | |||
71 | if ($parameter->isDefaultValueAvailable()) { |
||
72 | $newParameter->setDefaultValue($parameter->getDefaultValue()); |
||
73 | } |
||
74 | |||
75 | $parametersCall[] = sprintf('$%s', $parameter->name); |
||
76 | } |
||
77 | |||
78 | $newMethod->addBody(sprintf('parent::%s(%s);', $newMethodName, implode(', ', $parametersCall))); |
||
79 | |||
80 | foreach ($p['specials'] as $key => $values) { |
||
81 | foreach ($values as $value) { |
||
82 | $parts = CommentAnalyzer::splitDocLine($value); |
||
83 | $type = $parts[0]; |
||
84 | |||
85 | if ($key === 'psalm-assert') { |
||
86 | $type = sprintf($typeTemplate, $type); |
||
87 | } |
||
88 | |||
89 | $comment = sprintf('@%s %s', $key, $type); |
||
90 | if (count($parts) >= 2) { |
||
91 | $comment .= sprintf(' %s', implode(' ', array_slice($parts, 1))); |
||
92 | } |
||
93 | |||
94 | $newMethod->addComment($comment); |
||
95 | } |
||
96 | } |
||
97 | } |
||
98 | |||
156 |
Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a
@return
annotation as described here.