Complex classes like BaseParser 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 BaseParser, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
22 | abstract class BaseParser |
||
23 | { |
||
24 | private const SYMBOL_NONE = -1; |
||
25 | |||
26 | /* |
||
27 | * The following members will be filled with generated parsing data: |
||
28 | */ |
||
29 | |||
30 | /** @var int Size of $tokenToSymbol map */ |
||
31 | protected $tokenToSymbolMapSize; |
||
32 | /** @var int Size of $action table */ |
||
33 | protected $actionTableSize; |
||
34 | /** @var int Size of $goto table */ |
||
35 | protected $gotoTableSize; |
||
36 | |||
37 | /** @var int Symbol number signifying an invalid token */ |
||
38 | protected $invalidSymbol; |
||
39 | /** @var int Symbol number of error recovery token */ |
||
40 | protected $errorSymbol; |
||
41 | /** @var int Action number signifying default action */ |
||
42 | protected $defaultAction; |
||
43 | /** @var int Rule number signifying that an unexpected token was encountered */ |
||
44 | protected $unexpectedTokenRule; |
||
45 | /** @var int states */ |
||
46 | protected $YY2TBLSTATE; |
||
47 | /** @var int Number of non-leaf states */ |
||
48 | protected $numNonLeafStates; |
||
49 | |||
50 | /** @var int[] Map of lexer tokens to internal symbols */ |
||
51 | protected $tokenToSymbol; |
||
52 | /** @var string[] Map of symbols to their names */ |
||
53 | protected $symbolToName; |
||
54 | /** @var string[] Names of the production rules (only necessary for debugging) */ |
||
55 | protected $productions; |
||
56 | |||
57 | /** |
||
58 | * @var int[] Map of states to a displacement into the $action table. The corresponding action for this |
||
59 | * state/symbol pair is $action[$actionBase[$state] + $symbol]. If $actionBase[$state] is 0, the |
||
60 | * action is defaulted, i.e. $actionDefault[$state] should be used instead. |
||
61 | */ |
||
62 | protected $actionBase; |
||
63 | /** @var int[] Table of actions. Indexed according to $actionBase comment. */ |
||
64 | protected $action; |
||
65 | /** |
||
66 | * @var int[] Table indexed analogously to $action. If $actionCheck[$actionBase[$state] + $symbol] != $symbol |
||
67 | * then the action is defaulted, i.e. $actionDefault[$state] should be used instead. |
||
68 | */ |
||
69 | protected $actionCheck; |
||
70 | /** @var int[] Map of states to their default action */ |
||
71 | protected $actionDefault; |
||
72 | /** @var callable[] Semantic action callbacks */ |
||
73 | protected $reduceCallbacks; |
||
74 | |||
75 | |||
76 | /** @var TypeLexer */ |
||
77 | private $lexer; |
||
78 | |||
79 | /** @var FqsenResolver */ |
||
80 | protected $fqsenResolver; |
||
81 | |||
82 | /** @var Context */ |
||
83 | protected $context; |
||
84 | |||
85 | /** @var Type|Type[]|null Temporary value containing the result of last semantic action (reduction) */ |
||
86 | protected $semValue; |
||
87 | |||
88 | /** @var Type[] Semantic value stack (contains values of tokens and semantic action results) */ |
||
89 | protected $semStack; |
||
90 | |||
91 | /** |
||
92 | * @return void |
||
93 | */ |
||
94 | //phpcs:ignore SlevomatCodingStandard.TypeHints.ReturnTypeHint.MissingNativeTypeHint |
||
95 | abstract protected function initReduceCallbacks(); |
||
96 | |||
97 | public function __construct() |
||
102 | |||
103 | public function parse(string $type) |
||
217 | |||
218 | /** |
||
219 | * Format error message including expected tokens. |
||
220 | * |
||
221 | * @param int $symbol Unexpected symbol |
||
222 | * @param int $state State at time of error |
||
223 | * |
||
224 | * @return string Formatted error message |
||
225 | */ |
||
226 | protected function getErrorMessage(int $symbol, int $state) : string |
||
235 | |||
236 | /** |
||
237 | * Get limited number of expected tokens in given state. |
||
238 | * |
||
239 | * @param int $state State |
||
240 | * |
||
241 | * @return string[] Expected tokens. If too many, an empty array is returned. |
||
242 | */ |
||
243 | protected function getExpectedTokens(int $state) : array |
||
282 | |||
283 | protected function traceNewState($state, $symbol) : void |
||
288 | |||
289 | protected function traceRead($symbol, $value) : void |
||
293 | |||
294 | protected function traceShift($symbol) : void |
||
298 | |||
299 | protected function traceAccept() : void |
||
303 | |||
304 | protected function traceReduce($n) : void |
||
308 | |||
309 | protected function tracePop($state) : void |
||
313 | |||
314 | protected function traceDiscard($symbol) : void |
||
318 | } |
||
319 |
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..