Complex classes like ParserAbstract 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 ParserAbstract, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
11 | abstract class ParserAbstract implements Parser |
||
12 | { |
||
13 | const SYMBOL_NONE = -1; |
||
14 | |||
15 | /* |
||
16 | * The following members will be filled with generated parsing data: |
||
17 | */ |
||
18 | |||
19 | /** @var int Size of $tokenToSymbol map */ |
||
20 | protected $tokenToSymbolMapSize; |
||
21 | /** @var int Size of $action table */ |
||
22 | protected $actionTableSize; |
||
23 | /** @var int Size of $goto table */ |
||
24 | protected $gotoTableSize; |
||
25 | |||
26 | /** @var int Symbol number signifying an invalid token */ |
||
27 | protected $invalidSymbol; |
||
28 | /** @var int Symbol number of error recovery token */ |
||
29 | protected $errorSymbol; |
||
30 | /** @var int Action number signifying default action */ |
||
31 | protected $defaultAction; |
||
32 | /** @var int Rule number signifying that an unexpected token was encountered */ |
||
33 | protected $unexpectedTokenRule; |
||
34 | |||
35 | protected $YY2TBLSTATE; |
||
36 | protected $YYNLSTATES; |
||
37 | |||
38 | /** @var array Map of lexer tokens to internal symbols */ |
||
39 | protected $tokenToSymbol; |
||
40 | /** @var array Map of symbols to their names */ |
||
41 | protected $symbolToName; |
||
42 | /** @var array Names of the production rules (only necessary for debugging) */ |
||
43 | protected $productions; |
||
44 | |||
45 | /** @var array Map of states to a displacement into the $action table. The corresponding action for this |
||
46 | * state/symbol pair is $action[$actionBase[$state] + $symbol]. If $actionBase[$state] is 0, the |
||
47 | action is defaulted, i.e. $actionDefault[$state] should be used instead. */ |
||
48 | protected $actionBase; |
||
49 | /** @var array Table of actions. Indexed according to $actionBase comment. */ |
||
50 | protected $action; |
||
51 | /** @var array Table indexed analogously to $action. If $actionCheck[$actionBase[$state] + $symbol] != $symbol |
||
52 | * then the action is defaulted, i.e. $actionDefault[$state] should be used instead. */ |
||
53 | protected $actionCheck; |
||
54 | /** @var array Map of states to their default action */ |
||
55 | protected $actionDefault; |
||
56 | |||
57 | /** @var array Map of non-terminals to a displacement into the $goto table. The corresponding goto state for this |
||
58 | * non-terminal/state pair is $goto[$gotoBase[$nonTerminal] + $state] (unless defaulted) */ |
||
59 | protected $gotoBase; |
||
60 | /** @var array Table of states to goto after reduction. Indexed according to $gotoBase comment. */ |
||
61 | protected $goto; |
||
62 | /** @var array Table indexed analogously to $goto. If $gotoCheck[$gotoBase[$nonTerminal] + $state] != $nonTerminal |
||
63 | * then the goto state is defaulted, i.e. $gotoDefault[$nonTerminal] should be used. */ |
||
64 | protected $gotoCheck; |
||
65 | /** @var array Map of non-terminals to the default state to goto after their reduction */ |
||
66 | protected $gotoDefault; |
||
67 | |||
68 | /** @var array Map of rules to the non-terminal on their left-hand side, i.e. the non-terminal to use for |
||
69 | * determining the state to goto after reduction. */ |
||
70 | protected $ruleToNonTerminal; |
||
71 | /** @var array Map of rules to the length of their right-hand side, which is the number of elements that have to |
||
72 | * be popped from the stack(s) on reduction. */ |
||
73 | protected $ruleToLength; |
||
74 | |||
75 | /* |
||
76 | * The following members are part of the parser state: |
||
77 | */ |
||
78 | |||
79 | /** @var Lexer Lexer that is used when parsing */ |
||
80 | protected $lexer; |
||
81 | /** @var mixed Temporary value containing the result of last semantic action (reduction) */ |
||
82 | protected $semValue; |
||
83 | /** @var int Position in stacks (state stack, semantic value stack, attribute stack) */ |
||
84 | protected $stackPos; |
||
85 | /** @var array Semantic value stack (contains values of tokens and semantic action results) */ |
||
86 | protected $semStack; |
||
87 | /** @var array[] Start attribute stack */ |
||
88 | protected $startAttributeStack; |
||
89 | /** @var array End attributes of last *shifted* token */ |
||
90 | protected $endAttributes; |
||
91 | |||
92 | /** @var bool Whether to throw on first error */ |
||
93 | protected $throwOnError; |
||
94 | /** @var Error[] Errors collected during last parse */ |
||
95 | protected $errors; |
||
96 | |||
97 | /** |
||
98 | * Creates a parser instance. |
||
99 | * |
||
100 | * @param Lexer $lexer A lexer |
||
101 | * @param array $options Options array. The boolean 'throwOnError' option determines whether an exception should be |
||
102 | * thrown on first error, or if the parser should try to continue parsing the remaining code |
||
103 | * and build a partial AST. |
||
104 | */ |
||
105 | public function __construct(Lexer $lexer, array $options = array()) { |
||
106 | $this->lexer = $lexer; |
||
107 | $this->errors = array(); |
||
108 | $this->throwOnError = isset($options['throwOnError']) ? $options['throwOnError'] : true; |
||
109 | } |
||
110 | |||
111 | /** |
||
112 | * Get array of errors that occurred during the last parse. |
||
113 | * |
||
114 | * This method may only return multiple errors if the 'throwOnError' option is disabled. |
||
115 | * |
||
116 | * @return Error[] |
||
117 | */ |
||
118 | public function getErrors() { |
||
121 | |||
122 | /** |
||
123 | * Parses PHP code into a node tree. |
||
124 | * |
||
125 | * @param string $code The source code to parse |
||
126 | * |
||
127 | * @return Node[]|null Array of statements (or null if the 'throwOnError' option is disabled and the parser was |
||
128 | * unable to recover from an error). |
||
129 | */ |
||
130 | public function parse($code) { |
||
330 | |||
331 | protected function getErrorMessage($symbol, $state) { |
||
339 | |||
340 | protected function getExpectedTokens($state) { |
||
364 | |||
365 | /* |
||
366 | * Tracing functions used for debugging the parser. |
||
367 | */ |
||
368 | |||
369 | /* |
||
370 | protected function traceNewState($state, $symbol) { |
||
371 | echo '% State ' . $state |
||
372 | . ', Lookahead ' . ($symbol == self::SYMBOL_NONE ? '--none--' : $this->symbolToName[$symbol]) . "\n"; |
||
373 | } |
||
374 | |||
375 | protected function traceRead($symbol) { |
||
376 | echo '% Reading ' . $this->symbolToName[$symbol] . "\n"; |
||
377 | } |
||
378 | |||
379 | protected function traceShift($symbol) { |
||
380 | echo '% Shift ' . $this->symbolToName[$symbol] . "\n"; |
||
381 | } |
||
382 | |||
383 | protected function traceAccept() { |
||
384 | echo "% Accepted.\n"; |
||
385 | } |
||
386 | |||
387 | protected function traceReduce($n) { |
||
388 | echo '% Reduce by (' . $n . ') ' . $this->productions[$n] . "\n"; |
||
389 | } |
||
390 | |||
391 | protected function tracePop($state) { |
||
392 | echo '% Recovering, uncovered state ' . $state . "\n"; |
||
393 | } |
||
394 | |||
395 | protected function traceDiscard($symbol) { |
||
396 | echo '% Discard ' . $this->symbolToName[$symbol] . "\n"; |
||
397 | } |
||
398 | */ |
||
399 | |||
400 | /* |
||
401 | * Helper functions invoked by semantic actions |
||
402 | */ |
||
403 | |||
404 | /** |
||
405 | * Moves statements of semicolon-style namespaces into $ns->stmts and checks various error conditions. |
||
406 | * |
||
407 | * @param Node[] $stmts |
||
408 | * @return Node[] |
||
409 | */ |
||
410 | protected function handleNamespaces(array $stmts) { |
||
445 | |||
446 | private function getNamespacingStyle(array $stmts) { |
||
478 | |||
479 | protected function handleScalarTypes(Name $name) { |
||
494 | } |
||
495 |
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..