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 $known_schemas; |
||
| 34 | |||
| 35 | /** |
||
| 36 | * @var R\Property[] |
||
| 37 | */ |
||
| 38 | protected $known_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 | 25 | public function __construct() { |
|
| 69 | |||
| 70 | // Definition of symbols in the parser |
||
| 71 | |||
| 72 | /** |
||
| 73 | * @inheritdocs |
||
| 74 | */ |
||
| 75 | 25 | protected function add_symbols_to(SymbolTable $table) { |
|
| 94 | |||
| 95 | /** |
||
| 96 | * @param SymbolTable |
||
| 97 | * @return null |
||
| 98 | */ |
||
| 99 | 25 | protected function add_symbols_for_variables_to(SymbolTable $table) { |
|
| 129 | |||
| 130 | /** |
||
| 131 | * @param SymbolTable $table |
||
| 132 | * @param V\Property[] $properties |
||
| 133 | * @return null |
||
| 134 | */ |
||
| 135 | 25 | protected function add_symbols_for_properties_to(SymbolTable $table, array &$properties) { |
|
| 150 | |||
| 151 | /** |
||
| 152 | * @param SymbolTable |
||
| 153 | * @return null |
||
| 154 | */ |
||
| 155 | 25 | protected function add_symbols_for_rules_to(SymbolTable $table) { |
|
| 173 | |||
| 174 | /** |
||
| 175 | * @param SymbolTable $table |
||
| 176 | * @param R\Schema[] $schemas |
||
| 177 | * @return null |
||
| 178 | */ |
||
| 179 | 25 | protected function add_symbols_for_schemas_to(SymbolTable $table, array &$schemas) { |
|
| 187 | |||
| 188 | // IMPLEMENTATION OF Parser |
||
| 189 | |||
| 190 | /** |
||
| 191 | * @return Ruleset |
||
| 192 | */ |
||
| 193 | 25 | public function parse($source) { |
|
| 199 | |||
| 200 | /** |
||
| 201 | * Parses the top level statements in the rules file. |
||
| 202 | * |
||
| 203 | * @return Ruleset |
||
| 204 | */ |
||
| 205 | 16 | protected function root() { |
|
| 233 | |||
| 234 | // EXPRESSION TYPES |
||
| 235 | |||
| 236 | /** |
||
| 237 | * Fetch a rule mode from the stream. |
||
| 238 | * |
||
| 239 | * @return mixed |
||
| 240 | */ |
||
| 241 | 11 | protected function rule_mode() { |
|
| 249 | |||
| 250 | /** |
||
| 251 | * Fetch a string from the stream. |
||
| 252 | * |
||
| 253 | * @return string |
||
| 254 | */ |
||
| 255 | 15 | protected function string() { |
|
| 265 | |||
| 266 | /** |
||
| 267 | * Fetch a variable from the stream. |
||
| 268 | * |
||
| 269 | * @return V\Variable |
||
| 270 | */ |
||
| 271 | 20 | protected function variable($right_binding_power = 0) { |
|
| 290 | |||
| 291 | /** |
||
| 292 | * Fetch a rule schema and its arguments from the stream. |
||
| 293 | * |
||
| 294 | * @return array (R\Schema, array) |
||
| 295 | */ |
||
| 296 | 11 | protected function rule_schema() { |
|
| 306 | |||
| 307 | // TOP LEVEL STATEMENTS |
||
| 308 | |||
| 309 | /** |
||
| 310 | * Process a variable assignment. |
||
| 311 | * |
||
| 312 | * @return null |
||
| 313 | */ |
||
| 314 | 8 | protected function variable_assignment() { |
|
| 320 | |||
| 321 | /** |
||
| 322 | * Process a rule declaration. |
||
| 323 | * |
||
| 324 | * @return null |
||
| 325 | */ |
||
| 326 | 11 | protected function rule_declaration() { |
|
| 338 | |||
| 339 | |||
| 340 | // HANDLING OF VARIABLES |
||
| 341 | |||
| 342 | /** |
||
| 343 | * Add a variable to the variables currently known. |
||
| 344 | * |
||
| 345 | * @param string $name |
||
| 346 | * @param V\Variable $def |
||
| 347 | * @return null |
||
| 348 | */ |
||
| 349 | 25 | protected function add_variable($name, V\Variable $def) { |
|
| 357 | |||
| 358 | /** |
||
| 359 | * Get a predefined variable. |
||
| 360 | * |
||
| 361 | * @param string $name |
||
| 362 | * @return V\Variable |
||
| 363 | */ |
||
| 364 | 20 | protected function get_variable($name) { |
|
| 370 | |||
| 371 | /** |
||
| 372 | * Add all predefined variables to the current set of variables. |
||
| 373 | * |
||
| 374 | * @return null |
||
| 375 | */ |
||
| 376 | 25 | protected function add_predefined_variables() { |
|
| 381 | |||
| 382 | /** |
||
| 383 | * Purge all predefined variables from the current set of variables. |
||
| 384 | * |
||
| 385 | * @return null |
||
| 386 | */ |
||
| 387 | 16 | protected function purge_predefined_variables() { |
|
| 392 | |||
| 393 | // IMPLEMENTATION OF ArgumentParser |
||
| 394 | |||
| 395 | /** |
||
| 396 | * @var bool |
||
| 397 | */ |
||
| 398 | protected $is_start_of_rule_arguments = false; |
||
| 399 | |||
| 400 | 13 | protected function maybe_fetch_argument_delimiter() { |
|
| 406 | |||
| 407 | /** |
||
| 408 | * @inheritdoc |
||
| 409 | */ |
||
| 410 | 11 | public function fetch_string() { |
|
| 414 | |||
| 415 | /** |
||
| 416 | * @inheritdoc |
||
| 417 | */ |
||
| 418 | 6 | public function fetch_variable() { |
|
| 422 | } |
||
| 423 |
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..