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 | 32 | public function __construct( array $predefined_variables |
|
79 | |||
80 | // Definition of symbols in the parser |
||
81 | |||
82 | /** |
||
83 | * @inheritdocs |
||
84 | */ |
||
85 | 32 | protected function add_symbols_to(SymbolTable $table) { |
|
106 | |||
107 | /** |
||
108 | * @param SymbolTable |
||
109 | * @return null |
||
110 | */ |
||
111 | 32 | protected function add_symbols_for_comments(SymbolTable $table) { |
|
116 | |||
117 | /** |
||
118 | * @param SymbolTable |
||
119 | * @return null |
||
120 | */ |
||
121 | 32 | 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 | 32 | protected function add_symbols_for_properties_to(SymbolTable $table, array &$properties) { |
|
172 | |||
173 | /** |
||
174 | * @param SymbolTable |
||
175 | * @return null |
||
176 | */ |
||
177 | 32 | 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 | 32 | protected function add_symbols_for_schemas_to(SymbolTable $table, array &$schemas) { |
|
209 | |||
210 | // IMPLEMENTATION OF Parser |
||
211 | |||
212 | /** |
||
213 | * @return Ruleset |
||
214 | */ |
||
215 | 32 | public function parse($source) { |
|
221 | |||
222 | /** |
||
223 | * Parses the top level statements in the rules file. |
||
224 | * |
||
225 | * @return Ruleset |
||
226 | */ |
||
227 | 23 | protected function root() { |
|
270 | |||
271 | /** |
||
272 | * @param string |
||
273 | * @return string |
||
274 | */ |
||
275 | 5 | protected function trim_explanation($content) { |
|
280 | |||
281 | // EXPRESSION TYPES |
||
282 | |||
283 | /** |
||
284 | * Fetch a rule mode from the stream. |
||
285 | * |
||
286 | * @return mixed |
||
287 | */ |
||
288 | 12 | protected function rule_mode() { |
|
296 | |||
297 | /** |
||
298 | * Fetch a string from the stream. |
||
299 | * |
||
300 | * @return string |
||
301 | */ |
||
302 | 15 | protected function string() { |
|
312 | |||
313 | /** |
||
314 | * Fetch a variable from the stream. |
||
315 | * |
||
316 | * @return V\Variable |
||
317 | */ |
||
318 | 27 | protected function variable($right_binding_power = 0) { |
|
337 | |||
338 | /** |
||
339 | * Fetch a rule schema and its arguments from the stream. |
||
340 | * |
||
341 | * @return array (R\Schema, array) |
||
342 | */ |
||
343 | 12 | protected function schema() { |
|
353 | |||
354 | // TOP LEVEL STATEMENTS |
||
355 | |||
356 | /** |
||
357 | * Process a variable assignment. |
||
358 | * |
||
359 | * @return null |
||
360 | */ |
||
361 | 14 | protected function variable_assignment() { |
|
370 | |||
371 | /** |
||
372 | * Process a rule declaration. |
||
373 | * |
||
374 | * @return null |
||
375 | */ |
||
376 | 12 | protected function rule_declaration() { |
|
392 | |||
393 | |||
394 | // HANDLING OF VARIABLES |
||
395 | |||
396 | /** |
||
397 | * Add a variable to the variables currently known. |
||
398 | * |
||
399 | * @param string $name |
||
400 | * @param V\Variable $def |
||
401 | * @return null |
||
402 | */ |
||
403 | 32 | protected function add_variable($name, V\Variable $def) { |
|
411 | |||
412 | /** |
||
413 | * Get a predefined variable. |
||
414 | * |
||
415 | * @param string $name |
||
416 | * @return V\Variable |
||
417 | */ |
||
418 | 27 | protected function get_variable($name) { |
|
424 | |||
425 | /** |
||
426 | * Add all predefined variables to the current set of variables. |
||
427 | * |
||
428 | * @return null |
||
429 | */ |
||
430 | 32 | protected function add_predefined_variables() { |
|
435 | |||
436 | /** |
||
437 | * Purge all predefined variables from the current set of variables. |
||
438 | * |
||
439 | * @return null |
||
440 | */ |
||
441 | 23 | protected function purge_predefined_variables() { |
|
446 | |||
447 | // IMPLEMENTATION OF ArgumentParser |
||
448 | |||
449 | /** |
||
450 | * @var bool |
||
451 | */ |
||
452 | protected $is_start_of_rule_arguments = false; |
||
453 | |||
454 | 14 | protected function maybe_fetch_argument_delimiter() { |
|
460 | |||
461 | /** |
||
462 | * @inheritdoc |
||
463 | */ |
||
464 | 11 | public function fetch_string() { |
|
468 | |||
469 | /** |
||
470 | * @inheritdoc |
||
471 | */ |
||
472 | 7 | public function fetch_variable() { |
|
476 | } |
||
477 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.