Complex classes like Analyzer 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 Analyzer, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
26 | class Analyzer |
||
27 | { |
||
28 | /** |
||
29 | * Tokens representing rules. |
||
30 | * @var array |
||
31 | */ |
||
32 | protected $tokens; |
||
33 | |||
34 | /** |
||
35 | * @var array|RuleDelegate[] |
||
36 | */ |
||
37 | protected $rules = []; |
||
38 | |||
39 | /** |
||
40 | * Parsed rules. |
||
41 | * @var array|AbstractBuilder[] |
||
42 | */ |
||
43 | protected $parsedRules; |
||
44 | |||
45 | /** |
||
46 | * Counter to auto-name transitional rules. |
||
47 | * @var int |
||
48 | */ |
||
49 | protected $transitionalRuleCounter = 0; |
||
50 | |||
51 | /** |
||
52 | * Rule name being analyzed. |
||
53 | * @var string |
||
54 | */ |
||
55 | private $ruleName; |
||
56 | |||
57 | /** |
||
58 | * Analyzer constructor. |
||
59 | * @param LexerInterface $lexer |
||
60 | */ |
||
61 | public function __construct(LexerInterface $lexer) |
||
65 | |||
66 | /** |
||
67 | * @param RuleDelegate $delegate |
||
68 | */ |
||
69 | public function addRuleDelegate(RuleDelegate $delegate): void |
||
73 | |||
74 | /** |
||
75 | * Build the analyzer of the rules (does not analyze the rules). |
||
76 | * |
||
77 | * @return Rule[]|\Traversable |
||
78 | * @throws GrammarException |
||
79 | */ |
||
80 | public function analyze(): iterable |
||
115 | |||
116 | /** |
||
117 | * Implementation of “rule”. |
||
118 | * |
||
119 | * @param LookaheadIterator $tokens |
||
120 | * @param string|null $pNodeId |
||
121 | * @return string|int|null |
||
122 | * @throws GrammarException |
||
123 | */ |
||
124 | protected function rule(LookaheadIterator $tokens, &$pNodeId) |
||
128 | |||
129 | /** |
||
130 | * Implementation of “choice”. |
||
131 | * |
||
132 | * @param LookaheadIterator $tokens |
||
133 | * @param string|null $pNodeId |
||
134 | * @return string|int|null |
||
135 | * @throws GrammarException |
||
136 | */ |
||
137 | protected function choice(LookaheadIterator $tokens, &$pNodeId) |
||
186 | |||
187 | /** |
||
188 | * Implementation of “concatenation”. |
||
189 | * |
||
190 | * @param LookaheadIterator $tokens |
||
191 | * @param string|null $pNodeId |
||
192 | * @return string|int|null |
||
193 | * @throws GrammarException |
||
194 | */ |
||
195 | protected function concatenation(LookaheadIterator $tokens, &$pNodeId) |
||
225 | |||
226 | /** |
||
227 | * Implementation of “repetition”. |
||
228 | * |
||
229 | * @param LookaheadIterator $tokens |
||
230 | * @param string|null $pNodeId |
||
231 | * @return string|int|null |
||
232 | * @throws GrammarException |
||
233 | */ |
||
234 | protected function repetition(LookaheadIterator $tokens, &$pNodeId) |
||
307 | |||
308 | /** |
||
309 | * Implementation of “simple”. |
||
310 | * |
||
311 | * @param LookaheadIterator $tokens |
||
312 | * @param int|string|null $pNodeId |
||
313 | * @return string|int|null |
||
314 | * @throws GrammarException |
||
315 | */ |
||
316 | protected function simple(LookaheadIterator $tokens, &$pNodeId) |
||
335 | |||
336 | /** |
||
337 | * @param LookaheadIterator $tokens |
||
338 | * @param int|string|null $pNodeId |
||
339 | * @return int|null|string |
||
340 | * @throws GrammarException |
||
341 | */ |
||
342 | protected function group(LookaheadIterator $tokens, &$pNodeId) |
||
359 | |||
360 | /** |
||
361 | * @param LookaheadIterator $tokens |
||
362 | * @param bool $kept |
||
363 | * @return int|string|null |
||
364 | * @throws GrammarException |
||
365 | */ |
||
366 | protected function token(LookaheadIterator $tokens, bool $kept = true) |
||
382 | |||
383 | /** |
||
384 | * @param LookaheadIterator $tokens |
||
385 | * @return int|string |
||
386 | * @throws GrammarException |
||
387 | */ |
||
388 | protected function invoke(LookaheadIterator $tokens) |
||
414 | } |
||
415 |
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.
For example, imagine you have a variable
$accountId
that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to theid
property of an instance of theAccount
class. This class holds a proper account, so the id value must no longer be false.Either this assignment is in error or a type check should be added for that assignment.