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 | * @param V\Variable[] $predefined_variables |
||
| 60 | * @param R\Schema[] $schemas |
||
| 61 | * @param V\Property[] $properties |
||
| 62 | */ |
||
| 63 | 35 | public function __construct( array $predefined_variables |
|
| 77 | |||
| 78 | // Definition of symbols in the parser |
||
| 79 | |||
| 80 | /** |
||
| 81 | * @inheritdocs |
||
| 82 | */ |
||
| 83 | 35 | protected function add_symbols_to(SymbolTable $table) { |
|
| 104 | |||
| 105 | /** |
||
| 106 | * @param SymbolTable |
||
| 107 | * @return null |
||
| 108 | */ |
||
| 109 | 35 | protected function add_symbols_for_comments(SymbolTable $table) { |
|
| 114 | |||
| 115 | /** |
||
| 116 | * @param SymbolTable |
||
| 117 | * @return null |
||
| 118 | */ |
||
| 119 | 35 | protected function add_symbols_for_variables_to(SymbolTable $table) { |
|
| 149 | |||
| 150 | /** |
||
| 151 | * @param SymbolTable $table |
||
| 152 | * @param V\Property[] $properties |
||
| 153 | * @return null |
||
| 154 | */ |
||
| 155 | 35 | protected function add_symbols_for_properties_to(SymbolTable $table, array &$properties) { |
|
| 170 | |||
| 171 | /** |
||
| 172 | * @param SymbolTable |
||
| 173 | * @return null |
||
| 174 | */ |
||
| 175 | 35 | protected function add_symbols_for_rules_to(SymbolTable $table) { |
|
| 193 | |||
| 194 | /** |
||
| 195 | * @param SymbolTable $table |
||
| 196 | * @param R\Schema[] $schemas |
||
| 197 | * @return null |
||
| 198 | */ |
||
| 199 | 35 | protected function add_symbols_for_schemas_to(SymbolTable $table, array &$schemas) { |
|
| 207 | |||
| 208 | // IMPLEMENTATION OF Parser |
||
| 209 | |||
| 210 | /** |
||
| 211 | * @return Ruleset |
||
| 212 | */ |
||
| 213 | 35 | public function parse($source) { |
|
| 219 | |||
| 220 | /** |
||
| 221 | * Root expression for the parser is some whitespace or comment where a |
||
| 222 | * top level statement is in the middle. |
||
| 223 | * |
||
| 224 | * @return Ruleset |
||
| 225 | */ |
||
| 226 | 25 | protected function root() { |
|
| 241 | |||
| 242 | /** |
||
| 243 | * Parses the top level statements in the rules file. |
||
| 244 | * |
||
| 245 | * @return null |
||
| 246 | */ |
||
| 247 | 24 | public function top_level_statement() { |
|
| 266 | |||
| 267 | /** |
||
| 268 | * Returns currently matched whitespace or comment token if there is any. |
||
| 269 | * |
||
| 270 | * @return string|null |
||
| 271 | */ |
||
| 272 | 25 | public function is_current_token_to_be_dropped() { |
|
| 284 | |||
| 285 | /** |
||
| 286 | * @param string |
||
| 287 | * @return string |
||
| 288 | */ |
||
| 289 | 5 | protected function trim_explanation($content) { |
|
| 294 | |||
| 295 | // EXPRESSION TYPES |
||
| 296 | |||
| 297 | /** |
||
| 298 | * Fetch a rule mode from the stream. |
||
| 299 | * |
||
| 300 | * @return mixed |
||
| 301 | */ |
||
| 302 | 11 | protected function rule_mode() { |
|
| 310 | |||
| 311 | /** |
||
| 312 | * Fetch a string from the stream. |
||
| 313 | * |
||
| 314 | * @return string |
||
| 315 | */ |
||
| 316 | 14 | protected function string() { |
|
| 326 | |||
| 327 | /** |
||
| 328 | * Fetch a variable from the stream. |
||
| 329 | * |
||
| 330 | * @return V\Variable |
||
| 331 | */ |
||
| 332 | 30 | protected function variable($right_binding_power = 0) { |
|
| 351 | |||
| 352 | /** |
||
| 353 | * Fetch a rule schema and its arguments from the stream. |
||
| 354 | * |
||
| 355 | * @return array (R\Schema, array) |
||
| 356 | */ |
||
| 357 | 11 | protected function schema() { |
|
| 367 | |||
| 368 | // TOP LEVEL STATEMENTS |
||
| 369 | |||
| 370 | /** |
||
| 371 | * Process a variable assignment. |
||
| 372 | * |
||
| 373 | * @return null |
||
| 374 | */ |
||
| 375 | 14 | protected function variable_assignment() { |
|
| 384 | |||
| 385 | /** |
||
| 386 | * Process a rule declaration. |
||
| 387 | * |
||
| 388 | * @return null |
||
| 389 | */ |
||
| 390 | 11 | protected function rule_declaration() { |
|
| 406 | |||
| 407 | |||
| 408 | // HANDLING OF VARIABLES |
||
| 409 | |||
| 410 | /** |
||
| 411 | * Add a variable to the variables currently known. |
||
| 412 | * |
||
| 413 | * @param string $name |
||
| 414 | * @param V\Variable $def |
||
| 415 | * @return null |
||
| 416 | */ |
||
| 417 | 35 | protected function add_variable($name, V\Variable $def) { |
|
| 425 | |||
| 426 | /** |
||
| 427 | * Get a predefined variable. |
||
| 428 | * |
||
| 429 | * @param string $name |
||
| 430 | * @return V\Variable |
||
| 431 | */ |
||
| 432 | 30 | protected function get_variable($name) { |
|
| 438 | |||
| 439 | /** |
||
| 440 | * Add all predefined variables to the current set of variables. |
||
| 441 | * |
||
| 442 | * @return null |
||
| 443 | */ |
||
| 444 | 35 | protected function add_predefined_variables() { |
|
| 449 | |||
| 450 | /** |
||
| 451 | * Purge all predefined variables from the current set of variables. |
||
| 452 | * |
||
| 453 | * @return null |
||
| 454 | */ |
||
| 455 | 25 | protected function purge_predefined_variables() { |
|
| 460 | |||
| 461 | // IMPLEMENTATION OF ArgumentParser |
||
| 462 | |||
| 463 | /** |
||
| 464 | * @var bool |
||
| 465 | */ |
||
| 466 | protected $is_start_of_rule_arguments = false; |
||
| 467 | |||
| 468 | 14 | protected function maybe_fetch_argument_delimiter() { |
|
| 474 | |||
| 475 | /** |
||
| 476 | * @inheritdoc |
||
| 477 | */ |
||
| 478 | 10 | public function fetch_string() { |
|
| 482 | |||
| 483 | /** |
||
| 484 | * @inheritdoc |
||
| 485 | */ |
||
| 486 | 6 | public function fetch_variable() { |
|
| 490 | } |
||
| 491 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.