Conditions | 19 |
Paths | 10 |
Total Lines | 107 |
Code Lines | 48 |
Lines | 22 |
Ratio | 20.56 % |
Tests | 50 |
CRAP Score | 19.4434 |
Changes | 10 | ||
Bugs | 2 | Features | 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 |
||
130 | 5 | public static function parse(Parser $parser, TokensList $list, array $options = array()) |
|
131 | { |
||
132 | 5 | $ret = new AlterOperation(); |
|
133 | |||
134 | /** |
||
135 | * Counts brackets. |
||
136 | * |
||
137 | * @var int $brackets |
||
138 | */ |
||
139 | 5 | $brackets = 0; |
|
140 | |||
141 | /** |
||
142 | * The state of the parser. |
||
143 | * |
||
144 | * Below are the states of the parser. |
||
145 | * |
||
146 | * 0 ---------------------[ options ]---------------------> 1 |
||
147 | * |
||
148 | * 1 ----------------------[ field ]----------------------> 2 |
||
149 | * |
||
150 | * 2 -------------------------[ , ]-----------------------> 0 |
||
151 | * |
||
152 | * @var int $state |
||
153 | */ |
||
154 | 5 | $state = 0; |
|
155 | |||
156 | 5 | for (; $list->idx < $list->count; ++$list->idx) { |
|
1 ignored issue
–
show
|
|||
157 | /** |
||
158 | * Token parsed at this moment. |
||
159 | * |
||
160 | * @var Token $token |
||
161 | */ |
||
162 | 5 | $token = $list->tokens[$list->idx]; |
|
163 | |||
164 | // End of statement. |
||
165 | 5 | if ($token->type === Token::TYPE_DELIMITER) { |
|
166 | 5 | break; |
|
167 | } |
||
168 | |||
169 | // Skipping comments. |
||
170 | 5 | if ($token->type === Token::TYPE_COMMENT) { |
|
171 | 1 | continue; |
|
172 | } |
||
173 | |||
174 | // Skipping whitespaces. |
||
175 | 5 | if ($token->type === Token::TYPE_WHITESPACE) { |
|
176 | 2 | if ($state === 2) { |
|
177 | // When parsing the unknown part, the whitespaces are |
||
178 | // included to not break anything. |
||
179 | 2 | $ret->unknown[] = $token; |
|
180 | 2 | } |
|
181 | 2 | continue; |
|
182 | } |
||
183 | |||
184 | 5 | if ($state === 0) { |
|
185 | 5 | $ret->options = OptionsArray::parse($parser, $list, $options); |
|
186 | |||
187 | 5 | if ($ret->options->has('AS')) { |
|
188 | View Code Duplication | for (; $list->idx < $list->count; ++$list->idx) { |
|
1 ignored issue
–
show
|
|||
189 | if ($list->tokens[$list->idx]->type === Token::TYPE_DELIMITER) { |
||
190 | break; |
||
191 | } |
||
192 | $ret->unknown[] = $list->tokens[$list->idx]; |
||
193 | } |
||
194 | break; |
||
195 | } |
||
196 | |||
197 | 5 | $state = 1; |
|
198 | 5 | View Code Duplication | } elseif ($state === 1) { |
1 ignored issue
–
show
|
|||
199 | 5 | $ret->field = Expression::parse( |
|
200 | 5 | $parser, |
|
201 | 5 | $list, |
|
202 | array( |
||
203 | 5 | 'breakOnAlias' => true, |
|
204 | 5 | 'parseField' => 'column', |
|
205 | ) |
||
206 | 5 | ); |
|
207 | 5 | if ($ret->field === null) { |
|
208 | // No field was read. We go back one token so the next |
||
209 | // iteration will parse the same token, but in state 2. |
||
210 | 3 | --$list->idx; |
|
211 | 3 | } |
|
212 | 5 | $state = 2; |
|
213 | 5 | } elseif ($state === 2) { |
|
214 | 4 | if ($token->type === Token::TYPE_OPERATOR) { |
|
215 | 3 | if ($token->value === '(') { |
|
216 | 3 | ++$brackets; |
|
217 | 3 | } elseif ($token->value === ')') { |
|
218 | 3 | --$brackets; |
|
219 | 3 | } elseif (($token->value === ',') && ($brackets === 0)) { |
|
220 | 3 | break; |
|
221 | } |
||
222 | 3 | } |
|
223 | 4 | $ret->unknown[] = $token; |
|
224 | 4 | } |
|
225 | 5 | } |
|
226 | |||
227 | 5 | if ($ret->options->isEmpty()) { |
|
228 | 1 | $parser->error( |
|
229 | 1 | __('Unrecognized alter operation.'), |
|
230 | 1 | $list->tokens[$list->idx] |
|
231 | 1 | ); |
|
232 | 1 | } |
|
233 | |||
234 | 5 | --$list->idx; |
|
235 | 5 | return $ret; |
|
236 | } |
||
237 | |||
254 |