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 |
||
106 | public function process(File $file, $position) |
||
107 | { |
||
108 | $tokens = $file->getTokens(); |
||
109 | |||
110 | $endPosition = $file->findEndOfStatement($position); |
||
111 | |||
112 | $classElements = []; |
||
113 | $functionStart = -1; |
||
114 | $functionEnd = -1; |
||
115 | |||
116 | $lastVisibility = null; |
||
117 | $lastVisibilityEnd = -1; |
||
118 | |||
119 | for ($i = $position; $i < $endPosition; $i++){ |
||
120 | if ($tokens[$i]['type'] === 'T_CONST') { |
||
121 | $classElements[] = [ |
||
122 | 'type' => 'T_CONST', |
||
123 | 'visibility' => $i <= $lastVisibilityEnd ? $lastVisibility : 'T_PUBLIC', |
||
124 | 'name' => $tokens[$file->findNext(T_STRING, $i)]['content'] |
||
125 | ]; |
||
126 | } elseif($tokens[$i]['type'] === 'T_FUNCTION'){ |
||
127 | $type = 'T_FUNCTION'; |
||
128 | $name = $tokens[$file->findNext(T_STRING, $i)]['content']; |
||
129 | if (strcasecmp($name, '__destruct') === 0){ |
||
130 | $type .= '-DESTRUCT'; |
||
131 | } elseif (strcasecmp($name, '__construct') === 0){ |
||
132 | $type .= '-CONSTRUCT'; |
||
133 | } |
||
134 | |||
135 | $classElements[] = [ |
||
136 | 'type' => $type, |
||
137 | 'visibility' => $i <= $lastVisibilityEnd ? $lastVisibility : 'T_PUBLIC', |
||
138 | 'name' => $name |
||
139 | ]; |
||
140 | $functionStart = $i; |
||
141 | $functionEnd = $file->findEndOfStatement($i); |
||
142 | } elseif ($tokens[$i]['type'] === 'T_VARIABLE' && ($i < $functionStart || $i > $functionEnd)){ |
||
143 | $classElements[] = [ |
||
144 | 'type' => 'T_VARIABLE', |
||
145 | 'visibility' => $i <= $lastVisibilityEnd ? $lastVisibility : 'T_PUBLIC', |
||
146 | 'name' => $tokens[$i]['content'] |
||
147 | ]; |
||
148 | } elseif (in_array($tokens[$i]['type'], ['T_PRIVATE', 'T_PROTECTED', 'T_PUBLIC'])){ |
||
149 | $lastVisibility = $tokens[$i]['type']; |
||
150 | $lastVisibilityEnd = $file->findEndOfStatement($i); |
||
151 | } |
||
152 | } |
||
153 | |||
154 | $originalClassElements = $classElements; |
||
155 | usort($classElements, [$this, 'sort']); |
||
156 | |||
157 | if ($classElements !== $originalClassElements){ |
||
158 | self::throwError($file, $position); |
||
159 | } |
||
160 | } |
||
161 | } |
||
162 |