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 |
||
30 | class Analyzer |
||
31 | { |
||
32 | /** |
||
33 | * A list of parsed rules |
||
34 | * |
||
35 | * @var array|Symbol[]|Terminal[]|Production[] |
||
36 | */ |
||
37 | private $parsed = []; |
||
38 | |||
39 | /** |
||
40 | * @var array|Builder[] |
||
41 | */ |
||
42 | private $builders = []; |
||
43 | |||
44 | /** |
||
45 | * @var Mapping |
||
46 | */ |
||
47 | private $mapping; |
||
48 | |||
49 | /** |
||
50 | * @var array |
||
51 | */ |
||
52 | private $ruleTokens = []; |
||
53 | |||
54 | /** |
||
55 | * @var ProvideTokens |
||
56 | */ |
||
57 | private $tokens; |
||
58 | |||
59 | /** |
||
60 | * @var Readable |
||
61 | */ |
||
62 | private $file; |
||
63 | |||
64 | /** |
||
65 | * @var ProvideRules |
||
66 | */ |
||
67 | private $rules; |
||
68 | |||
69 | /** |
||
70 | * @var Builder |
||
71 | */ |
||
72 | private $lastRule; |
||
73 | |||
74 | /** |
||
75 | * Analyzer constructor. |
||
76 | * @param ProvideTokens $tokens |
||
77 | * @param ProvideRules $rules |
||
78 | */ |
||
79 | public function __construct(ProvideTokens $tokens, ProvideRules $rules) |
||
85 | |||
86 | /** |
||
87 | * @param string $rule |
||
88 | * @param iterable $tokens |
||
89 | */ |
||
90 | public function add(string $rule, iterable $tokens): void |
||
94 | |||
95 | /** |
||
96 | * @param string $rule |
||
97 | * @return bool |
||
98 | */ |
||
99 | public function isCompleted(string $rule): bool |
||
103 | |||
104 | /** |
||
105 | * @return array |
||
106 | * @throws \InvalidArgumentException |
||
107 | * @throws \Railt\Io\Exception\ExternalFileException |
||
108 | */ |
||
109 | public function getResult(): array |
||
120 | |||
121 | /** |
||
122 | * @throws \InvalidArgumentException |
||
123 | * @throws \Railt\Io\Exception\ExternalFileException |
||
124 | */ |
||
125 | private function parse(): void |
||
140 | |||
141 | /** |
||
142 | * @param Builder $builder |
||
143 | * @return Builder |
||
144 | */ |
||
145 | private function store(Builder $builder): Builder |
||
149 | |||
150 | /** |
||
151 | * @param LookaheadIterator $tokens |
||
152 | * @return Builder |
||
153 | * @throws \InvalidArgumentException |
||
154 | * @throws \Railt\Io\Exception\ExternalFileException |
||
155 | */ |
||
156 | private function choice(LookaheadIterator $tokens): Builder |
||
181 | |||
182 | /** |
||
183 | * @return Mapping |
||
184 | */ |
||
185 | public function getMapping(): Mapping |
||
189 | |||
190 | /** |
||
191 | * @param LookaheadIterator $tokens |
||
192 | * @return Builder |
||
193 | * @throws \InvalidArgumentException |
||
194 | * @throws \Railt\Io\Exception\ExternalFileException |
||
195 | */ |
||
196 | private function sequence(LookaheadIterator $tokens): Builder |
||
219 | |||
220 | private function repeat(LookaheadIterator $tokens): Builder |
||
224 | |||
225 | /** |
||
226 | * @param LookaheadIterator $tokens |
||
227 | * @return Builder |
||
228 | * @throws \InvalidArgumentException |
||
229 | * @throws \Railt\Io\Exception\ExternalFileException |
||
230 | */ |
||
231 | private function group(LookaheadIterator $tokens): Builder |
||
243 | |||
244 | /** |
||
245 | * @param LookaheadIterator $tokens |
||
246 | * @return null|Builder |
||
247 | * @throws \InvalidArgumentException |
||
248 | * @throws \Railt\Io\Exception\ExternalFileException |
||
249 | */ |
||
250 | private function terminal(LookaheadIterator $tokens): ?Builder |
||
288 | |||
289 | /** |
||
290 | * @param TokenInterface $token |
||
291 | * @param bool $keep |
||
292 | * @return TokenBuilder |
||
293 | * @throws \Railt\Io\Exception\ExternalFileException |
||
294 | */ |
||
295 | private function token(TokenInterface $token, bool $keep): TokenBuilder |
||
307 | |||
308 | /** |
||
309 | * @param TokenInterface $invocation |
||
310 | * @return Builder |
||
311 | * @throws \Railt\Io\Exception\ExternalFileException |
||
312 | */ |
||
313 | private function invoke(TokenInterface $invocation): Builder |
||
325 | } |
||
326 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.