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 ASSIGNMENT_RE = "(\w+)\s*=\s*"; |
||
| 22 | const STRING_RE = "[\"]((([\\\\][\"])|[^\"])+)[\"]"; |
||
| 23 | const RULE_MODE_RE = "must|can(not)?"; |
||
| 24 | |||
| 25 | /** |
||
| 26 | * @var V\Variable[] |
||
| 27 | */ |
||
| 28 | protected $predefined_variables; |
||
| 29 | |||
| 30 | /** |
||
| 31 | * @var R\Schema[] |
||
| 32 | */ |
||
| 33 | protected $schemas; |
||
| 34 | |||
| 35 | /** |
||
| 36 | * @var R\Property[] |
||
| 37 | */ |
||
| 38 | protected $properties; |
||
| 39 | |||
| 40 | /** |
||
| 41 | * @var V\Variable[] |
||
| 42 | */ |
||
| 43 | protected $variables = array(); |
||
| 44 | |||
| 45 | /** |
||
| 46 | * @var R\Rule[] |
||
| 47 | */ |
||
| 48 | protected $rules = array(); |
||
| 49 | |||
| 50 | /** |
||
| 51 | * TODO: make arrays passed by reference as they get copied anyway. |
||
| 52 | * |
||
| 53 | * @param V\Variable[] $predefined_variables |
||
| 54 | * @param R\Schema[] $schemas |
||
| 55 | * @param V\Property[] $properties |
||
| 56 | */ |
||
| 57 | 25 | public function __construct( array $predefined_variables |
|
| 71 | |||
| 72 | // Definition of symbols in the parser |
||
| 73 | |||
| 74 | /** |
||
| 75 | * @inheritdocs |
||
| 76 | */ |
||
| 77 | 25 | protected function add_symbols_to(SymbolTable $table) { |
|
| 96 | |||
| 97 | /** |
||
| 98 | * @param SymbolTable |
||
| 99 | * @return null |
||
| 100 | */ |
||
| 101 | 25 | protected function add_symbols_for_variables_to(SymbolTable $table) { |
|
| 131 | |||
| 132 | /** |
||
| 133 | * @param SymbolTable $table |
||
| 134 | * @param V\Property[] $properties |
||
| 135 | * @return null |
||
| 136 | */ |
||
| 137 | 25 | protected function add_symbols_for_properties_to(SymbolTable $table, array &$properties) { |
|
| 152 | |||
| 153 | /** |
||
| 154 | * @param SymbolTable |
||
| 155 | * @return null |
||
| 156 | */ |
||
| 157 | 25 | protected function add_symbols_for_rules_to(SymbolTable $table) { |
|
| 175 | |||
| 176 | /** |
||
| 177 | * @param SymbolTable $table |
||
| 178 | * @param R\Schema[] $schemas |
||
| 179 | * @return null |
||
| 180 | */ |
||
| 181 | 25 | protected function add_symbols_for_schemas_to(SymbolTable $table, array &$schemas) { |
|
| 189 | |||
| 190 | // IMPLEMENTATION OF Parser |
||
| 191 | |||
| 192 | /** |
||
| 193 | * @return Ruleset |
||
| 194 | */ |
||
| 195 | 25 | public function parse($source) { |
|
| 201 | |||
| 202 | /** |
||
| 203 | * Parses the top level statements in the rules file. |
||
| 204 | * |
||
| 205 | * @return Ruleset |
||
| 206 | */ |
||
| 207 | 16 | protected function root() { |
|
| 235 | |||
| 236 | // EXPRESSION TYPES |
||
| 237 | |||
| 238 | /** |
||
| 239 | * Fetch a rule mode from the stream. |
||
| 240 | * |
||
| 241 | * @return mixed |
||
| 242 | */ |
||
| 243 | 11 | protected function rule_mode() { |
|
| 251 | |||
| 252 | /** |
||
| 253 | * Fetch a string from the stream. |
||
| 254 | * |
||
| 255 | * @return string |
||
| 256 | */ |
||
| 257 | 15 | protected function string() { |
|
| 267 | |||
| 268 | /** |
||
| 269 | * Fetch a variable from the stream. |
||
| 270 | * |
||
| 271 | * @return V\Variable |
||
| 272 | */ |
||
| 273 | 20 | protected function variable($right_binding_power = 0) { |
|
| 292 | |||
| 293 | /** |
||
| 294 | * Fetch a rule schema and its arguments from the stream. |
||
| 295 | * |
||
| 296 | * @return array (R\Schema, array) |
||
| 297 | */ |
||
| 298 | 11 | protected function schema() { |
|
| 308 | |||
| 309 | // TOP LEVEL STATEMENTS |
||
| 310 | |||
| 311 | /** |
||
| 312 | * Process a variable assignment. |
||
| 313 | * |
||
| 314 | * @return null |
||
| 315 | */ |
||
| 316 | 8 | protected function variable_assignment() { |
|
| 322 | |||
| 323 | /** |
||
| 324 | * Process a rule declaration. |
||
| 325 | * |
||
| 326 | * @return null |
||
| 327 | */ |
||
| 328 | 11 | protected function rule_declaration() { |
|
| 340 | |||
| 341 | |||
| 342 | // HANDLING OF VARIABLES |
||
| 343 | |||
| 344 | /** |
||
| 345 | * Add a variable to the variables currently known. |
||
| 346 | * |
||
| 347 | * @param string $name |
||
| 348 | * @param V\Variable $def |
||
| 349 | * @return null |
||
| 350 | */ |
||
| 351 | 25 | protected function add_variable($name, V\Variable $def) { |
|
| 359 | |||
| 360 | /** |
||
| 361 | * Get a predefined variable. |
||
| 362 | * |
||
| 363 | * @param string $name |
||
| 364 | * @return V\Variable |
||
| 365 | */ |
||
| 366 | 20 | protected function get_variable($name) { |
|
| 372 | |||
| 373 | /** |
||
| 374 | * Add all predefined variables to the current set of variables. |
||
| 375 | * |
||
| 376 | * @return null |
||
| 377 | */ |
||
| 378 | 25 | protected function add_predefined_variables() { |
|
| 383 | |||
| 384 | /** |
||
| 385 | * Purge all predefined variables from the current set of variables. |
||
| 386 | * |
||
| 387 | * @return null |
||
| 388 | */ |
||
| 389 | 16 | protected function purge_predefined_variables() { |
|
| 394 | |||
| 395 | // IMPLEMENTATION OF ArgumentParser |
||
| 396 | |||
| 397 | /** |
||
| 398 | * @var bool |
||
| 399 | */ |
||
| 400 | protected $is_start_of_rule_arguments = false; |
||
| 401 | |||
| 402 | 13 | protected function maybe_fetch_argument_delimiter() { |
|
| 408 | |||
| 409 | /** |
||
| 410 | * @inheritdoc |
||
| 411 | */ |
||
| 412 | 11 | public function fetch_string() { |
|
| 416 | |||
| 417 | /** |
||
| 418 | * @inheritdoc |
||
| 419 | */ |
||
| 420 | 6 | public function fetch_variable() { |
|
| 424 | } |
||
| 425 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.