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 |
||
| 21 | class RuleParser extends Parser implements ArgumentParser { |
||
| 22 | const ASSIGNMENT_RE = "(\w+)\s*=\s*"; |
||
| 23 | const STRING_RE = "[\"]((([\\\\][\"])|[^\"])+)[\"]"; |
||
| 24 | const RULE_MODE_RE = "must|can(not)?"; |
||
| 25 | |||
| 26 | /** |
||
| 27 | * @var Variable[] |
||
| 28 | */ |
||
| 29 | protected $predefined_variables; |
||
| 30 | |||
| 31 | 25 | public function __construct() { |
|
| 116 | |||
| 117 | // IMPLEMENTATION OF Parser |
||
| 118 | |||
| 119 | /** |
||
| 120 | * @return Ruleset |
||
| 121 | */ |
||
| 122 | 25 | public function parse($source) { |
|
| 128 | |||
| 129 | /** |
||
| 130 | * Parses the top level statements in the rules file. |
||
| 131 | * |
||
| 132 | * @return Ruleset |
||
| 133 | */ |
||
| 134 | 16 | protected function root() { |
|
| 162 | |||
| 163 | // EXPRESSION TYPES |
||
| 164 | |||
| 165 | /** |
||
| 166 | * Fetch a rule mode from the stream. |
||
| 167 | * |
||
| 168 | * @return mixed |
||
| 169 | */ |
||
| 170 | 11 | protected function rule_mode() { |
|
| 178 | |||
| 179 | /** |
||
| 180 | * Fetch a string from the stream. |
||
| 181 | * |
||
| 182 | * @return string |
||
| 183 | */ |
||
| 184 | 15 | protected function string($right_binding_power = 0) { |
|
| 194 | |||
| 195 | /** |
||
| 196 | * Fetch a variable from the stream. |
||
| 197 | * |
||
| 198 | * @return V\Variable |
||
| 199 | */ |
||
| 200 | 20 | protected function variable($right_binding_power = 0) { |
|
| 219 | |||
| 220 | /** |
||
| 221 | * Fetch a rule schema and its arguments from the stream. |
||
| 222 | * |
||
| 223 | * @return array (R\Schema, array) |
||
| 224 | */ |
||
| 225 | 11 | protected function rule_schema($right_binding_power = 0) { |
|
| 235 | |||
| 236 | // TOP LEVEL STATEMENTS |
||
| 237 | |||
| 238 | /** |
||
| 239 | * Process a variable assignment. |
||
| 240 | * |
||
| 241 | * @return null |
||
| 242 | */ |
||
| 243 | 8 | protected function variable_assignment() { |
|
| 249 | |||
| 250 | /** |
||
| 251 | * Process a rule declaration. |
||
| 252 | * |
||
| 253 | * @return null |
||
| 254 | */ |
||
| 255 | 11 | protected function rule_declaration() { |
|
| 267 | |||
| 268 | |||
| 269 | // HANDLING OF VARIABLES |
||
| 270 | |||
| 271 | /** |
||
| 272 | * Add a variable to the variables currently known. |
||
| 273 | * |
||
| 274 | * @param string $name |
||
| 275 | * @param V\Variable $def |
||
| 276 | * @return null |
||
| 277 | */ |
||
| 278 | 25 | protected function add_variable($name, V\Variable $def) { |
|
| 286 | |||
| 287 | /** |
||
| 288 | * Get a predefined variable. |
||
| 289 | * |
||
| 290 | * @param string $name |
||
| 291 | * @return V\Variable |
||
| 292 | */ |
||
| 293 | 20 | protected function get_variable($name) { |
|
| 299 | |||
| 300 | /** |
||
| 301 | * Add all predefined variables to the current set of variables. |
||
| 302 | * |
||
| 303 | * @return null |
||
| 304 | */ |
||
| 305 | 25 | protected function add_predefined_variables() { |
|
| 310 | |||
| 311 | /** |
||
| 312 | * Purge all predefined variables from the current set of variables. |
||
| 313 | * |
||
| 314 | * @return null |
||
| 315 | */ |
||
| 316 | 16 | protected function purge_predefined_variables() { |
|
| 321 | |||
| 322 | // HANDLING OF SCHEMAS |
||
| 323 | |||
| 324 | /** |
||
| 325 | * Add a list of schemas to the parser. |
||
| 326 | * |
||
| 327 | * @param Schema[] |
||
| 328 | * @return null |
||
| 329 | */ |
||
| 330 | 25 | protected function add_schemas(array &$schemas) { |
|
| 335 | |||
| 336 | /** |
||
| 337 | * Add a schema to the parser. |
||
| 338 | * |
||
| 339 | * @param R/Schema |
||
| 340 | * @return null |
||
| 341 | */ |
||
| 342 | 25 | protected function add_schema(R\Schema $schema) { |
|
| 348 | |||
| 349 | // IMPLEMENTATION OF ArgumentParser |
||
| 350 | |||
| 351 | /** |
||
| 352 | * @var bool |
||
| 353 | */ |
||
| 354 | protected $is_start_of_rule_arguments = false; |
||
| 355 | |||
| 356 | 11 | protected function maybe_fetch_argument_delimiter() { |
|
| 362 | |||
| 363 | /** |
||
| 364 | * @inheritdoc |
||
| 365 | */ |
||
| 366 | 5 | public function fetch_string() { |
|
| 370 | |||
| 371 | /** |
||
| 372 | * @inheritdoc |
||
| 373 | */ |
||
| 374 | 6 | public function fetch_variable() { |
|
| 378 | } |
||
| 379 |
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..