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