| 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 |
||
| 126 | public function process(File $file, $position) |
||
| 127 | { |
||
| 128 | $tokens = $file->getTokens(); |
||
| 129 | |||
| 130 | $endPosition = $file->findEndOfStatement($position); |
||
| 131 | |||
| 132 | $classElements = []; |
||
| 133 | $functionStart = -1; |
||
| 134 | $functionEnd = -1; |
||
| 135 | |||
| 136 | $lastVisibility = null; |
||
| 137 | $lastVisibilityEnd = -1; |
||
| 138 | |||
| 139 | for ($i = $position; $i < $endPosition; $i++){ |
||
| 140 | if ($tokens[$i]['type'] === 'T_CONST') { |
||
| 141 | $classElements[] = [ |
||
| 142 | 'type' => 'T_CONST', |
||
| 143 | 'visibility' => $i <= $lastVisibilityEnd ? $lastVisibility : 'T_PUBLIC', |
||
| 144 | 'name' => $tokens[$file->findNext(T_STRING, $i)]['content'] |
||
| 145 | ]; |
||
| 146 | } elseif($tokens[$i]['type'] === 'T_FUNCTION'){ |
||
| 147 | $type = 'T_FUNCTION'; |
||
| 148 | $name = $tokens[$file->findNext(T_STRING, $i)]['content']; |
||
| 149 | if (strcasecmp($name, '__destruct') === 0){ |
||
| 150 | $type .= '-DESTRUCT'; |
||
| 151 | } elseif (strcasecmp($name, '__construct') === 0){ |
||
| 152 | $type .= '-CONSTRUCT'; |
||
| 153 | } |
||
| 154 | |||
| 155 | $classElements[] = [ |
||
| 156 | 'type' => $type, |
||
| 157 | 'visibility' => $i <= $lastVisibilityEnd ? $lastVisibility : 'T_PUBLIC', |
||
| 158 | 'name' => $name |
||
| 159 | ]; |
||
| 160 | $functionStart = $i; |
||
| 161 | $functionEnd = $file->findEndOfStatement($i); |
||
| 162 | } elseif ($tokens[$i]['type'] === 'T_VARIABLE' && ($i < $functionStart || $i > $functionEnd)){ |
||
| 163 | $classElements[] = [ |
||
| 164 | 'type' => 'T_VARIABLE', |
||
| 165 | 'visibility' => $i <= $lastVisibilityEnd ? $lastVisibility : 'T_PUBLIC', |
||
| 166 | 'name' => $tokens[$i]['content'] |
||
| 167 | ]; |
||
| 168 | } elseif (in_array($tokens[$i]['type'], ['T_PRIVATE', 'T_PROTECTED', 'T_PUBLIC'])){ |
||
| 169 | $lastVisibility = $tokens[$i]['type']; |
||
| 170 | $lastVisibilityEnd = $file->findEndOfStatement($i); |
||
| 171 | } |
||
| 172 | } |
||
| 173 | |||
| 174 | $originalClassElements = $classElements; |
||
| 175 | usort($classElements, [$this, 'sort']); |
||
| 176 | |||
| 177 | if ($classElements !== $originalClassElements){ |
||
| 178 | self::throwError($file, $position); |
||
| 179 | } |
||
| 180 | } |
||
| 181 | } |
||
| 182 |