Conditions | 26 |
Paths | 1472 |
Total Lines | 119 |
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 |
||
57 | public static function parse($docblock, $line_number = null, $preserve_format = false) |
||
58 | { |
||
59 | // Strip off comments. |
||
60 | $docblock = trim($docblock); |
||
61 | $docblock = preg_replace('@^/\*\*@', '', $docblock); |
||
62 | $docblock = preg_replace('@\*/$@', '', $docblock); |
||
63 | $docblock = preg_replace('@^[ \t]*\*@m', '', $docblock); |
||
64 | |||
65 | // Normalize multi-line @specials. |
||
66 | $lines = explode("\n", $docblock); |
||
67 | |||
68 | $line_map = []; |
||
69 | |||
70 | $last = false; |
||
71 | foreach ($lines as $k => $line) { |
||
72 | if (preg_match('/^\s?@\w/i', $line)) { |
||
73 | $last = $k; |
||
74 | } elseif (preg_match('/^\s*$/', $line)) { |
||
75 | $last = false; |
||
76 | } elseif ($last !== false) { |
||
77 | $old_last_line = $lines[$last]; |
||
78 | $lines[$last] = rtrim($old_last_line) |
||
79 | . ($preserve_format || trim($old_last_line) === '@return' ? "\n" . $line : ' ' . trim($line)); |
||
80 | |||
81 | if ($line_number) { |
||
82 | $old_line_number = $line_map[$old_last_line]; |
||
83 | unset($line_map[$old_last_line]); |
||
84 | $line_map[$lines[$last]] = $old_line_number; |
||
85 | } |
||
86 | |||
87 | unset($lines[$k]); |
||
88 | } |
||
89 | |||
90 | if ($line_number) { |
||
91 | $line_map[$line] = $line_number++; |
||
92 | } |
||
93 | } |
||
94 | |||
95 | $special = []; |
||
96 | |||
97 | if ($preserve_format) { |
||
98 | foreach ($lines as $m => $line) { |
||
99 | if (preg_match('/^\s?@([\w\-:]+)[\t ]*(.*)$/sm', $line, $matches)) { |
||
100 | list($full_match, $type, $data) = $matches; |
||
101 | |||
102 | $docblock = str_replace($full_match, '', $docblock); |
||
103 | |||
104 | if (empty($special[$type])) { |
||
105 | $special[$type] = []; |
||
106 | } |
||
107 | |||
108 | $line_number = $line_map && isset($line_map[$full_match]) ? $line_map[$full_match] : (int)$m; |
||
109 | |||
110 | $special[$type][$line_number] = rtrim($data); |
||
111 | } |
||
112 | } |
||
113 | } else { |
||
114 | $docblock = implode("\n", $lines); |
||
115 | |||
116 | // Parse @specials. |
||
117 | if (preg_match_all('/^\s?@([\w\-:]+)[\t ]*([^\n]*)/m', $docblock, $matches, PREG_SET_ORDER)) { |
||
118 | $docblock = preg_replace('/^\s?@([\w\-:]+)\s*([^\n]*)/m', '', $docblock); |
||
119 | /** @var string[] $match */ |
||
120 | foreach ($matches as $m => $match) { |
||
121 | list($_, $type, $data) = $match; |
||
122 | |||
123 | if (empty($special[$type])) { |
||
124 | $special[$type] = []; |
||
125 | } |
||
126 | |||
127 | $line_number = $line_map && isset($line_map[$_]) ? $line_map[$_] : (int)$m; |
||
128 | |||
129 | $special[$type][$line_number] = $data; |
||
130 | } |
||
131 | } |
||
132 | } |
||
133 | |||
134 | $docblock = str_replace("\t", ' ', $docblock); |
||
135 | |||
136 | // Smush the whole docblock to the left edge. |
||
137 | $min_indent = 80; |
||
138 | $indent = 0; |
||
139 | foreach (array_filter(explode("\n", $docblock)) as $line) { |
||
140 | for ($ii = 0; $ii < strlen($line); ++$ii) { |
||
141 | if ($line[$ii] != ' ') { |
||
142 | break; |
||
143 | } |
||
144 | ++$indent; |
||
145 | } |
||
146 | |||
147 | $min_indent = min($indent, $min_indent); |
||
148 | } |
||
149 | |||
150 | $docblock = preg_replace('/^' . str_repeat(' ', $min_indent) . '/m', '', $docblock); |
||
151 | $docblock = rtrim($docblock); |
||
152 | |||
153 | // Trim any empty lines off the front, but leave the indent level if there |
||
154 | // is one. |
||
155 | $docblock = preg_replace('/^\s*\n/', '', $docblock); |
||
156 | |||
157 | foreach ($special as $special_key => $_) { |
||
158 | if (substr($special_key, 0, 6) === 'psalm-') { |
||
159 | $special_key = substr($special_key, 6); |
||
160 | |||
161 | if (!in_array( |
||
162 | $special_key, |
||
163 | self::PSALM_ANNOTATIONS, |
||
164 | true |
||
165 | )) { |
||
166 | throw new DocblockParseException('Unrecognised annotation @psalm-' . $special_key); |
||
167 | } |
||
168 | } |
||
169 | } |
||
170 | |||
171 | return [ |
||
172 | 'description' => $docblock, |
||
173 | 'specials' => $special, |
||
174 | ]; |
||
175 | } |
||
176 | |||
204 |
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.
Consider the following example. The parameter
$italy
is not defined by the methodfinale(...)
.The most likely cause is that the parameter was removed, but the annotation was not.