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