Complex classes like RuleParser 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 RuleParser, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
20 | class RuleParser extends Parser implements ArgumentParser { |
||
21 | const EXPLANATION_RE = "[/][*][*](([^*]|([*][^/]))*)[*][/]"; |
||
22 | const SINGLE_LINE_COMMENT_RE = "[/][/]([^\n]*)"; |
||
23 | const MULTI_LINE_COMMENT_RE = "[/][*](([^*]|([*][^/]))*)[*][/]"; |
||
24 | const ASSIGNMENT_RE = "(\w+)\s*=\s*"; |
||
25 | const STRING_RE = "[\"]((([\\\\][\"])|[^\"])+)[\"]"; |
||
26 | const RULE_MODE_RE = "must|can(not)?"; |
||
27 | |||
28 | /** |
||
29 | * @var V\Variable[] |
||
30 | */ |
||
31 | protected $predefined_variables; |
||
32 | |||
33 | /** |
||
34 | * @var R\Schema[] |
||
35 | */ |
||
36 | protected $schemas; |
||
37 | |||
38 | /** |
||
39 | * @var R\Property[] |
||
40 | */ |
||
41 | protected $properties; |
||
42 | |||
43 | /** |
||
44 | * @var V\Variable[] |
||
45 | */ |
||
46 | protected $variables = array(); |
||
47 | |||
48 | /** |
||
49 | * @var R\Rule[] |
||
50 | */ |
||
51 | protected $rules = array(); |
||
52 | |||
53 | /** |
||
54 | * @var string|null |
||
55 | */ |
||
56 | protected $last_explanation = null; |
||
57 | |||
58 | /** |
||
59 | * TODO: make arrays passed by reference as they get copied anyway. |
||
60 | * |
||
61 | * @param V\Variable[] $predefined_variables |
||
62 | * @param R\Schema[] $schemas |
||
63 | * @param V\Property[] $properties |
||
64 | */ |
||
65 | 30 | public function __construct( array $predefined_variables |
|
79 | |||
80 | // Definition of symbols in the parser |
||
81 | |||
82 | /** |
||
83 | * @inheritdocs |
||
84 | */ |
||
85 | 30 | protected function add_symbols_to(SymbolTable $table) { |
|
106 | |||
107 | /** |
||
108 | * @param SymbolTable |
||
109 | * @return null |
||
110 | */ |
||
111 | 30 | protected function add_symbols_for_comments(SymbolTable $table) { |
|
116 | |||
117 | /** |
||
118 | * @param SymbolTable |
||
119 | * @return null |
||
120 | */ |
||
121 | 30 | protected function add_symbols_for_variables_to(SymbolTable $table) { |
|
151 | |||
152 | /** |
||
153 | * @param SymbolTable $table |
||
154 | * @param V\Property[] $properties |
||
155 | * @return null |
||
156 | */ |
||
157 | 30 | protected function add_symbols_for_properties_to(SymbolTable $table, array &$properties) { |
|
172 | |||
173 | /** |
||
174 | * @param SymbolTable |
||
175 | * @return null |
||
176 | */ |
||
177 | 30 | protected function add_symbols_for_rules_to(SymbolTable $table) { |
|
195 | |||
196 | /** |
||
197 | * @param SymbolTable $table |
||
198 | * @param R\Schema[] $schemas |
||
199 | * @return null |
||
200 | */ |
||
201 | 30 | protected function add_symbols_for_schemas_to(SymbolTable $table, array &$schemas) { |
|
209 | |||
210 | // IMPLEMENTATION OF Parser |
||
211 | |||
212 | /** |
||
213 | * @return Ruleset |
||
214 | */ |
||
215 | 30 | public function parse($source) { |
|
221 | |||
222 | /** |
||
223 | * Parses the top level statements in the rules file. |
||
224 | * |
||
225 | * @return Ruleset |
||
226 | */ |
||
227 | 20 | protected function root() { |
|
263 | |||
264 | /** |
||
265 | * Returns currently matched whitespace or comment token if there is any. |
||
266 | * |
||
267 | * @return string|null |
||
268 | */ |
||
269 | 20 | public function is_current_token_to_be_dropped() { |
|
281 | |||
282 | /** |
||
283 | * @param string |
||
284 | * @return string |
||
285 | */ |
||
286 | 5 | protected function trim_explanation($content) { |
|
291 | |||
292 | // EXPRESSION TYPES |
||
293 | |||
294 | /** |
||
295 | * Fetch a rule mode from the stream. |
||
296 | * |
||
297 | * @return mixed |
||
298 | */ |
||
299 | 9 | protected function rule_mode() { |
|
307 | |||
308 | /** |
||
309 | * Fetch a string from the stream. |
||
310 | * |
||
311 | * @return string |
||
312 | */ |
||
313 | 12 | protected function string() { |
|
323 | |||
324 | /** |
||
325 | * Fetch a variable from the stream. |
||
326 | * |
||
327 | * @return V\Variable |
||
328 | */ |
||
329 | 25 | protected function variable($right_binding_power = 0) { |
|
348 | |||
349 | /** |
||
350 | * Fetch a rule schema and its arguments from the stream. |
||
351 | * |
||
352 | * @return array (R\Schema, array) |
||
353 | */ |
||
354 | 9 | protected function schema() { |
|
364 | |||
365 | // TOP LEVEL STATEMENTS |
||
366 | |||
367 | /** |
||
368 | * Process a variable assignment. |
||
369 | * |
||
370 | * @return null |
||
371 | */ |
||
372 | 11 | protected function variable_assignment() { |
|
381 | |||
382 | /** |
||
383 | * Process a rule declaration. |
||
384 | * |
||
385 | * @return null |
||
386 | */ |
||
387 | 9 | protected function rule_declaration() { |
|
403 | |||
404 | |||
405 | // HANDLING OF VARIABLES |
||
406 | |||
407 | /** |
||
408 | * Add a variable to the variables currently known. |
||
409 | * |
||
410 | * @param string $name |
||
411 | * @param V\Variable $def |
||
412 | * @return null |
||
413 | */ |
||
414 | 30 | protected function add_variable($name, V\Variable $def) { |
|
422 | |||
423 | /** |
||
424 | * Get a predefined variable. |
||
425 | * |
||
426 | * @param string $name |
||
427 | * @return V\Variable |
||
428 | */ |
||
429 | 25 | protected function get_variable($name) { |
|
435 | |||
436 | /** |
||
437 | * Add all predefined variables to the current set of variables. |
||
438 | * |
||
439 | * @return null |
||
440 | */ |
||
441 | 30 | protected function add_predefined_variables() { |
|
446 | |||
447 | /** |
||
448 | * Purge all predefined variables from the current set of variables. |
||
449 | * |
||
450 | * @return null |
||
451 | */ |
||
452 | 20 | protected function purge_predefined_variables() { |
|
457 | |||
458 | // IMPLEMENTATION OF ArgumentParser |
||
459 | |||
460 | /** |
||
461 | * @var bool |
||
462 | */ |
||
463 | protected $is_start_of_rule_arguments = false; |
||
464 | |||
465 | 12 | protected function maybe_fetch_argument_delimiter() { |
|
471 | |||
472 | /** |
||
473 | * @inheritdoc |
||
474 | */ |
||
475 | 8 | public function fetch_string() { |
|
479 | |||
480 | /** |
||
481 | * @inheritdoc |
||
482 | */ |
||
483 | 5 | public function fetch_variable() { |
|
487 | } |
||
488 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.