Conditions | 32 |
Paths | 40 |
Total Lines | 141 |
Code Lines | 94 |
Lines | 32 |
Ratio | 22.7 % |
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 |
||
79 | public static function parse(Parser $parser, TokensList $list, array $options = array()) |
||
80 | { |
||
81 | $ret = new CaseExpression(); |
||
82 | |||
83 | /** |
||
84 | * State of parser |
||
85 | * |
||
86 | * @var int $parser |
||
87 | */ |
||
88 | $state = 0; |
||
89 | |||
90 | /** |
||
91 | * Syntax type (type 0 or type 1) |
||
92 | * |
||
93 | * @var int $type |
||
94 | */ |
||
95 | $type = 0; |
||
96 | |||
97 | ++$list->idx; // Skip 'CASE' |
||
98 | |||
99 | for (; $list->idx < $list->count; ++$list->idx) { |
||
100 | |||
101 | /** |
||
102 | * Token parsed at this moment. |
||
103 | * |
||
104 | * @var Token $token |
||
105 | */ |
||
106 | $token = $list->tokens[$list->idx]; |
||
107 | |||
108 | // Skipping whitespaces and comments. |
||
109 | if (($token->type === Token::TYPE_WHITESPACE) |
||
110 | || ($token->type === Token::TYPE_COMMENT) |
||
111 | ) { |
||
112 | continue; |
||
113 | } |
||
114 | |||
115 | if ($state === 0) { |
||
116 | if ($token->type === Token::TYPE_KEYWORD |
||
117 | && $token->value === 'WHEN' |
||
118 | ) { |
||
119 | ++$list->idx; // Skip 'WHEN' |
||
120 | $new_condition = Condition::parse($parser, $list); |
||
121 | $type = 1; |
||
122 | $state = 1; |
||
123 | $ret->conditions[] = $new_condition; |
||
124 | } elseif ($token->type === Token::TYPE_KEYWORD |
||
125 | && $token->value === 'ELSE' |
||
126 | ) { |
||
127 | ++$list->idx; // Skip 'ELSE' |
||
128 | $ret->else_result = Expression::parse($parser, $list); |
||
129 | $state = 0; // last clause of CASE expression |
||
130 | } elseif ($token->type === Token::TYPE_KEYWORD |
||
131 | && ($token->value === 'END' |
||
132 | || $token->value === 'end') |
||
133 | ) { |
||
134 | $state = 3; // end of CASE expression |
||
135 | ++$list->idx; |
||
136 | break; |
||
137 | } elseif ($token->type === Token::TYPE_KEYWORD) { |
||
138 | $parser->error(__('Unexpected keyword.'), $token); |
||
139 | break; |
||
140 | } else { |
||
141 | $ret->value = Expression::parse($parser, $list); |
||
142 | $type = 0; |
||
143 | $state = 1; |
||
144 | } |
||
145 | } elseif ($state === 1) { |
||
146 | if ($type === 0) { |
||
147 | if ($token->type === Token::TYPE_KEYWORD |
||
148 | && $token->value === 'WHEN' |
||
149 | ) { |
||
150 | ++$list->idx; // Skip 'WHEN' |
||
151 | $new_value = Expression::parse($parser, $list); |
||
152 | $state = 2; |
||
153 | $ret->compare_values[] = $new_value; |
||
154 | } elseif ($token->type === Token::TYPE_KEYWORD |
||
155 | && $token->value === 'ELSE' |
||
156 | ) { |
||
157 | ++$list->idx; // Skip 'ELSE' |
||
158 | $ret->else_result = Expression::parse($parser, $list); |
||
159 | $state = 0; // last clause of CASE expression |
||
160 | } elseif ($token->type === Token::TYPE_KEYWORD |
||
161 | && ($token->value === 'END' |
||
162 | || $token->value === 'end') |
||
163 | ) { |
||
164 | $state = 3; // end of CASE expression |
||
165 | ++$list->idx; |
||
166 | break; |
||
167 | } elseif ($token->type === Token::TYPE_KEYWORD) { |
||
168 | $parser->error(__('Unexpected keyword.'), $token); |
||
169 | break; |
||
170 | } else { |
||
171 | $parser->error(__('Unexpected token.'), $token); |
||
172 | break; |
||
173 | } |
||
174 | View Code Duplication | } else { |
|
175 | if ($token->type === Token::TYPE_KEYWORD |
||
176 | && $token->value === 'THEN' |
||
177 | ) { |
||
178 | ++$list->idx; // Skip 'THEN' |
||
179 | $new_result = Expression::parse($parser, $list); |
||
180 | $state = 0; |
||
181 | $ret->results[] = $new_result; |
||
182 | } elseif ($token->type === Token::TYPE_KEYWORD) { |
||
183 | $parser->error(__('Unexpected keyword.'), $token); |
||
184 | break; |
||
185 | } else { |
||
186 | $parser->error(__('Unexpected token.'), $token); |
||
187 | break; |
||
188 | } |
||
189 | } |
||
190 | } elseif ($state === 2) { |
||
191 | View Code Duplication | if ($type === 0) { |
|
192 | if ($token->type === Token::TYPE_KEYWORD |
||
193 | && $token->value === 'THEN' |
||
194 | ) { |
||
195 | ++$list->idx; // Skip 'THEN' |
||
196 | $new_result = Expression::parse($parser, $list); |
||
197 | $ret->results[] = $new_result; |
||
198 | $state = 1; |
||
199 | } elseif ($token->type === Token::TYPE_KEYWORD) { |
||
200 | $parser->error(__('Unexpected keyword.'), $token); |
||
201 | break; |
||
202 | } else { |
||
203 | $parser->error(__('Unexpected token.'), $token); |
||
204 | break; |
||
205 | } |
||
206 | } |
||
207 | } |
||
208 | } |
||
209 | |||
210 | if ($state !== 3) { |
||
211 | $parser->error( |
||
212 | __('Unexpected end of CASE expression'), |
||
213 | $list->tokens[$list->idx - 1] |
||
214 | ); |
||
215 | } |
||
216 | |||
217 | --$list->idx; |
||
218 | return $ret; |
||
219 | } |
||
220 | |||
260 |
This check compares the return type specified in the
@return
annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.