| Conditions | 32 |
| Paths | 40 |
| Total Lines | 132 |
| Code Lines | 85 |
| Lines | 26 |
| Ratio | 19.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 | } |
||
| 171 | View Code Duplication | } else { |
|
| 172 | if ($token->type === Token::TYPE_KEYWORD |
||
| 173 | && $token->value === 'THEN' |
||
| 174 | ) { |
||
| 175 | ++$list->idx; // Skip 'THEN' |
||
| 176 | $new_result = Expression::parse($parser, $list); |
||
| 177 | $state = 0; |
||
| 178 | $ret->results[] = $new_result; |
||
| 179 | } elseif ($token->type === Token::TYPE_KEYWORD) { |
||
| 180 | $parser->error(__('Unexpected keyword.'), $token); |
||
| 181 | break; |
||
| 182 | } |
||
| 183 | } |
||
| 184 | } elseif ($state === 2) { |
||
| 185 | View Code Duplication | if ($type === 0) { |
|
| 186 | if ($token->type === Token::TYPE_KEYWORD |
||
| 187 | && $token->value === 'THEN' |
||
| 188 | ) { |
||
| 189 | ++$list->idx; // Skip 'THEN' |
||
| 190 | $new_result = Expression::parse($parser, $list); |
||
| 191 | $ret->results[] = $new_result; |
||
| 192 | $state = 1; |
||
| 193 | } elseif ($token->type === Token::TYPE_KEYWORD) { |
||
| 194 | $parser->error(__('Unexpected keyword.'), $token); |
||
| 195 | break; |
||
| 196 | } |
||
| 197 | } |
||
| 198 | } |
||
| 199 | } |
||
| 200 | |||
| 201 | if ($state !== 3) { |
||
| 202 | $parser->error( |
||
| 203 | __('Unexpected end of CASE expression'), |
||
| 204 | $list->tokens[$list->idx - 1] |
||
| 205 | ); |
||
| 206 | } |
||
| 207 | |||
| 208 | --$list->idx; |
||
| 209 | return $ret; |
||
| 210 | } |
||
| 211 | |||
| 251 |
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.