Complex classes like Expression often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Expression, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
24 | class Expression extends Component |
||
25 | { |
||
26 | /** |
||
27 | * List of allowed reserved keywords in expressions. |
||
28 | * |
||
29 | * @var array |
||
30 | */ |
||
31 | private static $ALLOWED_KEYWORDS = array( |
||
32 | 'AS' => 1, 'DUAL' => 1, 'NULL' => 1, 'REGEXP' => 1, 'CASE' => 1, |
||
33 | 'DIV' => 1, 'AND' => 1, 'OR' => 1, 'XOR' => 1, 'NOT' => 1, 'MOD' => 1, |
||
34 | ); |
||
35 | |||
36 | /** |
||
37 | * The name of this database. |
||
38 | * |
||
39 | * @var string |
||
40 | */ |
||
41 | public $database; |
||
42 | |||
43 | /** |
||
44 | * The name of this table. |
||
45 | * |
||
46 | * @var string |
||
47 | */ |
||
48 | public $table; |
||
49 | |||
50 | /** |
||
51 | * The name of the column. |
||
52 | * |
||
53 | * @var string |
||
54 | */ |
||
55 | public $column; |
||
56 | |||
57 | /** |
||
58 | * The sub-expression. |
||
59 | * |
||
60 | * @var string |
||
61 | */ |
||
62 | public $expr = ''; |
||
63 | |||
64 | /** |
||
65 | * The alias of this expression. |
||
66 | * |
||
67 | * @var string |
||
68 | */ |
||
69 | public $alias; |
||
70 | |||
71 | /** |
||
72 | * The name of the function. |
||
73 | * |
||
74 | * @var mixed |
||
75 | */ |
||
76 | public $function; |
||
77 | |||
78 | /** |
||
79 | * The type of subquery. |
||
80 | * |
||
81 | * @var string |
||
82 | */ |
||
83 | public $subquery; |
||
84 | |||
85 | /** |
||
86 | * Constructor. |
||
87 | * |
||
88 | * Syntax: |
||
89 | * new Expression('expr') |
||
90 | * new Expression('expr', 'alias') |
||
91 | * new Expression('database', 'table', 'column') |
||
92 | * new Expression('database', 'table', 'column', 'alias') |
||
93 | * |
||
94 | * If the database, table or column name is not required, pass an empty |
||
95 | * string. |
||
96 | * |
||
97 | * @param string $database The name of the database or the the expression. |
||
|
|||
98 | * the the expression. |
||
99 | * @param string $table The name of the table or the alias of the expression. |
||
100 | * the alias of the expression. |
||
101 | * @param string $column the name of the column |
||
102 | * @param string $alias the name of the alias |
||
103 | */ |
||
104 | 270 | public function __construct($database = null, $table = null, $column = null, $alias = null) |
|
116 | |||
117 | /** |
||
118 | * Possible options:. |
||
119 | * |
||
120 | * `field` |
||
121 | * |
||
122 | * First field to be filled. |
||
123 | * If this is not specified, it takes the value of `parseField`. |
||
124 | * |
||
125 | * `parseField` |
||
126 | * |
||
127 | * Specifies the type of the field parsed. It may be `database`, |
||
128 | * `table` or `column`. These expressions may not include |
||
129 | * parentheses. |
||
130 | * |
||
131 | * `breakOnAlias` |
||
132 | * |
||
133 | * If not empty, breaks when the alias occurs (it is not included). |
||
134 | * |
||
135 | * `breakOnParentheses` |
||
136 | * |
||
137 | * If not empty, breaks when the first parentheses occurs. |
||
138 | * |
||
139 | * `parenthesesDelimited` |
||
140 | * |
||
141 | * If not empty, breaks after last parentheses occurred. |
||
142 | * |
||
143 | * @param Parser $parser the parser that serves as context |
||
144 | * @param TokensList $list the list of tokens that are being parsed |
||
145 | * @param array $options parameters for parsing |
||
146 | * |
||
147 | * @return Expression |
||
148 | */ |
||
149 | 264 | public static function parse(Parser $parser, TokensList $list, array $options = array()) |
|
150 | { |
||
151 | 264 | $ret = new self(); |
|
152 | |||
153 | /** |
||
154 | * Whether current tokens make an expression or a table reference. |
||
155 | * |
||
156 | * @var bool |
||
157 | */ |
||
158 | 264 | $isExpr = false; |
|
159 | |||
160 | /** |
||
161 | * Whether a period was previously found. |
||
162 | * |
||
163 | * @var bool |
||
164 | */ |
||
165 | 264 | $dot = false; |
|
166 | |||
167 | /** |
||
168 | * Whether an alias is expected. Is 2 if `AS` keyword was found. |
||
169 | * |
||
170 | * @var bool |
||
171 | */ |
||
172 | 264 | $alias = false; |
|
173 | |||
174 | /** |
||
175 | * Counts brackets. |
||
176 | * |
||
177 | * @var int |
||
178 | */ |
||
179 | 264 | $brackets = 0; |
|
180 | |||
181 | /** |
||
182 | * Keeps track of the last two previous tokens. |
||
183 | * |
||
184 | * @var Token[] |
||
185 | */ |
||
186 | 264 | $prev = array(null, null); |
|
187 | |||
188 | // When a field is parsed, no parentheses are expected. |
||
189 | 264 | if (!empty($options['parseField'])) { |
|
190 | 127 | $options['breakOnParentheses'] = true; |
|
191 | 127 | $options['field'] = $options['parseField']; |
|
192 | } |
||
193 | |||
194 | 264 | for (; $list->idx < $list->count; ++$list->idx) { |
|
195 | /** |
||
196 | * Token parsed at this moment. |
||
197 | * |
||
198 | * @var Token |
||
199 | */ |
||
200 | 264 | $token = $list->tokens[$list->idx]; |
|
201 | |||
202 | // End of statement. |
||
203 | 264 | if ($token->type === Token::TYPE_DELIMITER) { |
|
204 | 105 | break; |
|
205 | } |
||
206 | |||
207 | // Skipping whitespaces and comments. |
||
208 | 263 | if (($token->type === Token::TYPE_WHITESPACE) |
|
209 | 263 | || ($token->type === Token::TYPE_COMMENT) |
|
210 | ) { |
||
211 | 214 | if ($isExpr) { |
|
212 | 104 | $ret->expr .= $token->token; |
|
213 | } |
||
214 | 214 | continue; |
|
215 | } |
||
216 | |||
217 | 263 | if ($token->type === Token::TYPE_KEYWORD) { |
|
218 | 197 | if (($brackets > 0) && (empty($ret->subquery)) |
|
219 | 25 | && (!empty(Parser::$STATEMENT_PARSERS[$token->keyword])) |
|
220 | ) { |
||
221 | // A `(` was previously found and this keyword is the |
||
222 | // beginning of a statement, so this is a subquery. |
||
223 | 20 | $ret->subquery = $token->keyword; |
|
224 | 196 | } elseif (($token->flags & Token::FLAG_KEYWORD_FUNCTION) |
|
225 | 28 | && (empty($options['parseField']) |
|
226 | 19 | && !$alias) |
|
227 | ) { |
||
228 | 17 | $isExpr = true; |
|
229 | 191 | } elseif (($token->flags & Token::FLAG_KEYWORD_RESERVED) |
|
230 | 184 | && ($brackets === 0) |
|
231 | ) { |
||
232 | 184 | if (empty(self::$ALLOWED_KEYWORDS[$token->keyword])) { |
|
233 | // A reserved keyword that is not allowed in the |
||
234 | // expression was found so the expression must have |
||
235 | // ended and a new clause is starting. |
||
236 | 173 | break; |
|
237 | } |
||
238 | 46 | if ($token->keyword === 'AS') { |
|
239 | 37 | if (!empty($options['breakOnAlias'])) { |
|
240 | 5 | break; |
|
241 | } |
||
242 | 32 | if ($alias) { |
|
243 | 1 | $parser->error( |
|
244 | 1 | 'An alias was expected.', |
|
245 | 1 | $token |
|
246 | ); |
||
247 | 1 | break; |
|
248 | } |
||
249 | 32 | $alias = true; |
|
250 | 32 | continue; |
|
251 | 10 | } elseif ($token->keyword === 'CASE') { |
|
252 | // For a use of CASE like |
||
253 | // 'SELECT a = CASE .... END, b=1, `id`, ... FROM ...' |
||
254 | 1 | $tempCaseExpr = CaseExpression::parse($parser, $list); |
|
255 | 1 | $ret->expr .= CaseExpression::build($tempCaseExpr); |
|
256 | 1 | $isExpr = true; |
|
257 | 1 | continue; |
|
258 | } |
||
259 | 9 | $isExpr = true; |
|
260 | 47 | } elseif ($brackets === 0 && strlen($ret->expr) > 0 && !$alias) { |
|
261 | /* End of expression */ |
||
262 | 28 | break; |
|
263 | } |
||
264 | } |
||
265 | |||
266 | 261 | if (($token->type === Token::TYPE_NUMBER) |
|
267 | 254 | || ($token->type === Token::TYPE_BOOL) |
|
268 | 254 | || (($token->type === Token::TYPE_SYMBOL) |
|
269 | 63 | && ($token->flags & Token::FLAG_SYMBOL_VARIABLE)) |
|
270 | 254 | || (($token->type === Token::TYPE_OPERATOR) |
|
271 | 173 | && ($token->value !== '.')) |
|
272 | ) { |
||
273 | 187 | if (!empty($options['parseField'])) { |
|
274 | 61 | break; |
|
275 | } |
||
276 | |||
277 | // Numbers, booleans and operators (except dot) are usually part |
||
278 | // of expressions. |
||
279 | 147 | $isExpr = true; |
|
280 | } |
||
281 | |||
282 | 261 | if ($token->type === Token::TYPE_OPERATOR) { |
|
283 | 136 | if ((!empty($options['breakOnParentheses'])) |
|
284 | 4 | && (($token->value === '(') || ($token->value === ')')) |
|
285 | ) { |
||
286 | // No brackets were expected. |
||
287 | 2 | break; |
|
288 | } |
||
289 | 135 | if ($token->value === '(') { |
|
290 | 45 | ++$brackets; |
|
291 | 45 | if ((empty($ret->function)) && ($prev[1] !== null) |
|
292 | 11 | && (($prev[1]->type === Token::TYPE_NONE) |
|
293 | 10 | || ($prev[1]->type === Token::TYPE_SYMBOL) |
|
294 | 10 | || (($prev[1]->type === Token::TYPE_KEYWORD) |
|
295 | 10 | && ($prev[1]->flags & Token::FLAG_KEYWORD_FUNCTION))) |
|
296 | ) { |
||
297 | 11 | $ret->function = $prev[1]->value; |
|
298 | } |
||
299 | 135 | } elseif ($token->value === ')' && $brackets == 0) { |
|
300 | // Not our bracket |
||
301 | 6 | break; |
|
302 | 131 | } elseif ($token->value === ')') { |
|
303 | 45 | --$brackets; |
|
304 | 45 | if ($brackets === 0) { |
|
305 | 45 | if (!empty($options['parenthesesDelimited'])) { |
|
306 | // The current token is the last bracket, the next |
||
307 | // one will be outside the expression. |
||
308 | 7 | $ret->expr .= $token->token; |
|
309 | 7 | ++$list->idx; |
|
310 | 7 | break; |
|
311 | } |
||
312 | 2 | } elseif ($brackets < 0) { |
|
313 | // $parser->error('Unexpected closing bracket.', $token); |
||
314 | // $brackets = 0; |
||
315 | break; |
||
316 | } |
||
317 | 124 | } elseif ($token->value === ',') { |
|
318 | // Expressions are comma-delimited. |
||
319 | 79 | if ($brackets === 0) { |
|
320 | 70 | break; |
|
321 | } |
||
322 | } |
||
323 | } |
||
324 | |||
325 | // Saving the previous tokens. |
||
326 | 260 | $prev[0] = $prev[1]; |
|
327 | 260 | $prev[1] = $token; |
|
328 | |||
329 | 260 | if ($alias) { |
|
330 | // An alias is expected (the keyword `AS` was previously found). |
||
331 | 31 | if (!empty($ret->alias)) { |
|
332 | 1 | $parser->error('An alias was previously found.', $token); |
|
333 | 1 | break; |
|
334 | } |
||
335 | 31 | $ret->alias = $token->value; |
|
336 | 31 | $alias = false; |
|
337 | 260 | } elseif ($isExpr) { |
|
338 | // Handling aliases. |
||
339 | 126 | if (/* (empty($ret->alias)) && */ ($brackets === 0) |
|
340 | 120 | && (($prev[0] === null) |
|
341 | 58 | || ((($prev[0]->type !== Token::TYPE_OPERATOR) |
|
342 | 25 | || ($prev[0]->token === ')')) |
|
343 | 49 | && (($prev[0]->type !== Token::TYPE_KEYWORD) |
|
344 | 2 | || (!($prev[0]->flags & Token::FLAG_KEYWORD_RESERVED))))) |
|
345 | 112 | && (($prev[1]->type === Token::TYPE_STRING) |
|
346 | 112 | || (($prev[1]->type === Token::TYPE_SYMBOL) |
|
347 | 7 | && (!($prev[1]->flags & Token::FLAG_SYMBOL_VARIABLE))) |
|
348 | 112 | || ($prev[1]->type === Token::TYPE_NONE)) |
|
349 | ) { |
||
350 | 6 | if (!empty($ret->alias)) { |
|
351 | 2 | $parser->error('An alias was previously found.', $token); |
|
352 | 2 | break; |
|
353 | } |
||
354 | 5 | $ret->alias = $prev[1]->value; |
|
355 | } else { |
||
356 | 126 | $ret->expr .= $token->token; |
|
357 | } |
||
358 | } elseif (!$isExpr) { |
||
359 | 240 | if (($token->type === Token::TYPE_OPERATOR) && ($token->value === '.')) { |
|
360 | // Found a `.` which means we expect a column name and |
||
361 | // the column name we parsed is actually the table name |
||
362 | // and the table name is actually a database name. |
||
363 | 29 | if ((!empty($ret->database)) || ($dot)) { |
|
364 | 2 | $parser->error('Unexpected dot.', $token); |
|
365 | } |
||
366 | 29 | $ret->database = $ret->table; |
|
367 | 29 | $ret->table = $ret->column; |
|
368 | 29 | $ret->column = null; |
|
369 | 29 | $dot = true; |
|
370 | 29 | $ret->expr .= $token->token; |
|
371 | } else { |
||
372 | 240 | $field = empty($options['field']) ? 'column' : $options['field']; |
|
373 | 240 | if (empty($ret->$field)) { |
|
374 | 240 | $ret->$field = $token->value; |
|
375 | 240 | $ret->expr .= $token->token; |
|
376 | 240 | $dot = false; |
|
377 | } else { |
||
378 | // No alias is expected. |
||
379 | 12 | if (!empty($options['breakOnAlias'])) { |
|
380 | 3 | break; |
|
381 | } |
||
382 | 9 | if (!empty($ret->alias)) { |
|
383 | 2 | $parser->error('An alias was previously found.', $token); |
|
384 | 2 | break; |
|
385 | } |
||
386 | 8 | $ret->alias = $token->value; |
|
387 | } |
||
388 | } |
||
389 | } |
||
390 | } |
||
391 | |||
392 | 264 | if ($alias) { |
|
393 | 3 | $parser->error( |
|
394 | 3 | 'An alias was expected.', |
|
395 | 3 | $list->tokens[$list->idx - 1] |
|
396 | ); |
||
397 | } |
||
398 | |||
399 | // White-spaces might be added at the end. |
||
400 | 264 | $ret->expr = trim($ret->expr); |
|
401 | |||
402 | 264 | if ($ret->expr === '') { |
|
403 | 10 | return null; |
|
404 | } |
||
405 | |||
406 | 260 | --$list->idx; |
|
407 | |||
408 | 260 | return $ret; |
|
409 | } |
||
410 | |||
411 | /** |
||
412 | * @param Expression|Expression[] $component the component to be built |
||
413 | * @param array $options parameters for building |
||
414 | * |
||
415 | * @return string |
||
416 | */ |
||
417 | 54 | public static function build($component, array $options = array()) |
|
445 | } |
||
446 |
This check looks for
@param
annotations where the type inferred by our type inference engine differs from the declared type.It makes a suggestion as to what type it considers more descriptive.
Most often this is a case of a parameter that can be null in addition to its declared types.