Conditions | 11 |
Paths | 73 |
Total Lines | 46 |
Code Lines | 28 |
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 declare(strict_types=1); |
||
29 | function generateMethodDocs($methods, $flags, $prefix = '') |
||
30 | { |
||
31 | $lines = []; |
||
32 | foreach ( $methods as $method ) |
||
33 | { |
||
34 | $doc = $method->getDocComment(); |
||
35 | $doc = $doc ? explode("\n", $doc) : []; |
||
36 | $return = ''; |
||
37 | foreach ( $doc as $line ) |
||
38 | { |
||
39 | if ( preg_match('/@(return|returns)/', $line, $matches) ) |
||
40 | { |
||
41 | $return = trim(StringUtils::after($matches[0], $line)); |
||
42 | $return = StringUtils::before(' ', $return, true) . ' '; |
||
43 | } |
||
44 | } |
||
45 | $methodName = $prefix . ( $prefix ? ucfirst($method->name) : $method->name ); |
||
46 | $line = " * @method static {$return}{$flags} {$methodName}("; |
||
47 | if ( count($method->getParameters()) === 0 ) |
||
48 | { |
||
49 | $lines[] = $line . ")\n"; |
||
50 | continue; |
||
51 | } |
||
52 | $params = []; |
||
53 | foreach ( $method->getParameters() as $parameter ) |
||
54 | { |
||
55 | $param = '$' . $parameter->name; |
||
56 | if ( $parameter->isOptional() ) |
||
57 | { |
||
58 | if ( null === $parameter->getDefaultValue() ) |
||
59 | { |
||
60 | $param .= ' = null'; |
||
61 | } |
||
62 | else |
||
63 | { |
||
64 | $defval = $parameter->getDefaultValue(); |
||
65 | $defval = is_array($defval) ? implode('', $defval) : $defval; |
||
66 | $param .= sprintf(' = "%s"', $defval); |
||
67 | } |
||
68 | } |
||
69 | $params[] = $param; |
||
70 | } |
||
71 | $lines[] = $line . implode(", ", $params) . ")\n"; |
||
72 | } |
||
73 | return $lines; |
||
74 | } |
||
75 |