Conditions | 14 |
Paths | 26 |
Total Lines | 55 |
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 |
||
140 | public function process(File $file, $position) |
||
141 | { |
||
142 | $tokens = $file->getTokens(); |
||
143 | |||
144 | $endPosition = $file->findEndOfStatement($position); |
||
145 | |||
146 | $classElements = []; |
||
147 | $functionStart = -1; |
||
148 | $functionEnd = -1; |
||
149 | |||
150 | $lastVisibility = null; |
||
151 | $lastVisibilityEnd = -1; |
||
152 | |||
153 | for ($i = $position; $i < $endPosition; $i++){ |
||
154 | if ($tokens[$i]['type'] === 'T_CONST') { |
||
155 | $classElements[] = [ |
||
156 | 'type' => 'T_CONST', |
||
157 | 'visibility' => $i <= $lastVisibilityEnd ? $lastVisibility : 'T_PUBLIC', |
||
158 | 'name' => $tokens[$file->findNext(T_STRING, $i)]['content'] |
||
159 | ]; |
||
160 | } elseif($tokens[$i]['type'] === 'T_FUNCTION'){ |
||
161 | $type = 'T_FUNCTION'; |
||
162 | $name = $tokens[$file->findNext(T_STRING, $i)]['content']; |
||
163 | if (strcasecmp($name, '__destruct') === 0){ |
||
164 | $type .= '-DESTRUCT'; |
||
165 | } elseif (strcasecmp($name, '__construct') === 0){ |
||
166 | $type .= '-CONSTRUCT'; |
||
167 | } |
||
168 | |||
169 | $classElements[] = [ |
||
170 | 'type' => $type, |
||
171 | 'visibility' => $i <= $lastVisibilityEnd ? $lastVisibility : 'T_PUBLIC', |
||
172 | 'name' => $name |
||
173 | ]; |
||
174 | $functionStart = $i; |
||
175 | $functionEnd = $file->findEndOfStatement($i); |
||
176 | } elseif ($tokens[$i]['type'] === 'T_VARIABLE' && ($i < $functionStart || $i > $functionEnd)){ |
||
177 | $classElements[] = [ |
||
178 | 'type' => 'T_VARIABLE', |
||
179 | 'visibility' => $i <= $lastVisibilityEnd ? $lastVisibility : 'T_PUBLIC', |
||
180 | 'name' => $tokens[$i]['content'] |
||
181 | ]; |
||
182 | } elseif (in_array($tokens[$i]['type'], ['T_PRIVATE', 'T_PROTECTED', 'T_PUBLIC'])){ |
||
183 | $lastVisibility = $tokens[$i]['type']; |
||
184 | $lastVisibilityEnd = $file->findEndOfStatement($i); |
||
185 | } |
||
186 | } |
||
187 | |||
188 | $originalClassElements = $classElements; |
||
189 | usort($classElements, [$this, 'sort']); |
||
190 | |||
191 | if ($classElements !== $originalClassElements){ |
||
192 | self::throwFixableError($file, $position); |
||
193 | } |
||
194 | } |
||
195 | } |
||
196 |