Complex classes like Ruleset 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 Ruleset, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 21 | class Ruleset extends Collection implements ArrayAccess, ConfigProvider |
||
| 22 | { |
||
| 23 | /** |
||
| 24 | * Constructor |
||
| 25 | */ |
||
| 26 | public function __construct() |
||
| 30 | |||
| 31 | /** |
||
| 32 | * {@inheritdoc} |
||
| 33 | */ |
||
| 34 | public function clear() |
||
| 40 | |||
| 41 | //========================================================================== |
||
| 42 | // ArrayAccess methods |
||
| 43 | //========================================================================== |
||
| 44 | |||
| 45 | /** |
||
| 46 | * Test whether a rule category exists |
||
| 47 | * |
||
| 48 | * @param string $k Rule name, e.g. "allowChild" or "isTransparent" |
||
| 49 | */ |
||
| 50 | public function offsetExists($k) |
||
| 54 | |||
| 55 | /** |
||
| 56 | * Return the content of a rule category |
||
| 57 | * |
||
| 58 | * @param string $k Rule name, e.g. "allowChild" or "isTransparent" |
||
| 59 | * @return mixed |
||
| 60 | */ |
||
| 61 | public function offsetGet($k) |
||
| 65 | |||
| 66 | /** |
||
| 67 | * Not supported |
||
| 68 | */ |
||
| 69 | public function offsetSet($k, $v) |
||
| 73 | |||
| 74 | /** |
||
| 75 | * Clear a subset of the rules |
||
| 76 | * |
||
| 77 | * @see clear() |
||
| 78 | * |
||
| 79 | * @param string $k Rule name, e.g. "allowChild" or "isTransparent" |
||
| 80 | */ |
||
| 81 | public function offsetUnset($k) |
||
| 85 | |||
| 86 | //========================================================================== |
||
| 87 | // Generic methods |
||
| 88 | //========================================================================== |
||
| 89 | |||
| 90 | /** |
||
| 91 | * {@inheritdoc} |
||
| 92 | */ |
||
| 93 | public function asConfig() |
||
| 150 | |||
| 151 | /** |
||
| 152 | * Merge a set of rules into this collection |
||
| 153 | * |
||
| 154 | * @param array|Ruleset $rules 2D array of rule definitions, or instance of Ruleset |
||
| 155 | * @param bool $overwrite Whether to overwrite scalar rules (e.g. boolean rules) |
||
| 156 | */ |
||
| 157 | public function merge($rules, $overwrite = true) |
||
| 180 | |||
| 181 | /** |
||
| 182 | * Remove a specific rule, or all the rules of a given type |
||
| 183 | * |
||
| 184 | * @param string $type Type of rules to clear |
||
| 185 | * @param string $tagName Name of the target tag, or none to remove all rules of given type |
||
| 186 | * @return void |
||
| 187 | */ |
||
| 188 | public function remove($type, $tagName = null) |
||
| 224 | |||
| 225 | //========================================================================== |
||
| 226 | // Rules |
||
| 227 | //========================================================================== |
||
| 228 | |||
| 229 | /** |
||
| 230 | * Add a boolean rule |
||
| 231 | * |
||
| 232 | * @param string $ruleName Name of the rule |
||
| 233 | * @param bool $bool Whether to enable or disable the rule |
||
| 234 | * @return self |
||
| 235 | */ |
||
| 236 | protected function addBooleanRule($ruleName, $bool) |
||
| 247 | |||
| 248 | /** |
||
| 249 | * Add a targeted rule |
||
| 250 | * |
||
| 251 | * @param string $ruleName Name of the rule |
||
| 252 | * @param string $tagName Name of the target tag |
||
| 253 | * @return self |
||
| 254 | */ |
||
| 255 | protected function addTargetedRule($ruleName, $tagName) |
||
| 261 | |||
| 262 | /** |
||
| 263 | * Add an allowChild rule |
||
| 264 | * |
||
| 265 | * @param string $tagName Name of the target tag |
||
| 266 | * @return self |
||
| 267 | */ |
||
| 268 | public function allowChild($tagName) |
||
| 272 | |||
| 273 | /** |
||
| 274 | * Add an allowDescendant rule |
||
| 275 | * |
||
| 276 | * @param string $tagName Name of the target tag |
||
| 277 | * @return self |
||
| 278 | */ |
||
| 279 | public function allowDescendant($tagName) |
||
| 283 | |||
| 284 | /** |
||
| 285 | * Add an autoClose rule |
||
| 286 | * |
||
| 287 | * NOTE: this rule exists so that plugins don't have to specifically handle tags whose end tag |
||
| 288 | * may/must be omitted such as <hr> or [img] |
||
| 289 | * |
||
| 290 | * @param bool $bool Whether or not the tag should automatically be closed if its start tag is not followed by an end tag |
||
| 291 | * @return self |
||
| 292 | */ |
||
| 293 | public function autoClose($bool = true) |
||
| 297 | |||
| 298 | /** |
||
| 299 | * Add an autoReopen rule |
||
| 300 | * |
||
| 301 | * @param bool $bool Whether or not the tag should automatically be reopened if closed by an end tag of a different name |
||
| 302 | * @return self |
||
| 303 | */ |
||
| 304 | public function autoReopen($bool = true) |
||
| 308 | |||
| 309 | /** |
||
| 310 | * Add a breakParagraph rule |
||
| 311 | * |
||
| 312 | * @param bool $bool Whether or not this tag breaks current paragraph if applicable |
||
| 313 | * @return self |
||
| 314 | */ |
||
| 315 | public function breakParagraph($bool = true) |
||
| 319 | |||
| 320 | /** |
||
| 321 | * Add a closeAncestor rule |
||
| 322 | * |
||
| 323 | * @param string $tagName Name of the target tag |
||
| 324 | * @return self |
||
| 325 | */ |
||
| 326 | public function closeAncestor($tagName) |
||
| 330 | |||
| 331 | /** |
||
| 332 | * Add a closeParent rule |
||
| 333 | * |
||
| 334 | * @param string $tagName Name of the target tag |
||
| 335 | * @return self |
||
| 336 | */ |
||
| 337 | public function closeParent($tagName) |
||
| 341 | |||
| 342 | /** |
||
| 343 | * Add a createChild rule |
||
| 344 | * |
||
| 345 | * @param string $tagName Name of the target tag |
||
| 346 | * @return self |
||
| 347 | */ |
||
| 348 | public function createChild($tagName) |
||
| 352 | |||
| 353 | /** |
||
| 354 | * Add a createParagraphs rule |
||
| 355 | * |
||
| 356 | * @param bool $bool Whether or not paragraphs should automatically be created to handle content |
||
| 357 | * @return self |
||
| 358 | */ |
||
| 359 | public function createParagraphs($bool = true) |
||
| 363 | |||
| 364 | /** |
||
| 365 | * Set the default child rule |
||
| 366 | * |
||
| 367 | * @param string $rule Either "allow" or "deny" |
||
| 368 | * @return self |
||
| 369 | */ |
||
| 370 | public function defaultChildRule($rule) |
||
| 381 | |||
| 382 | /** |
||
| 383 | * Set the default descendant rule |
||
| 384 | * |
||
| 385 | * @param string $rule Either "allow" or "deny" |
||
| 386 | * @return self |
||
| 387 | */ |
||
| 388 | public function defaultDescendantRule($rule) |
||
| 399 | |||
| 400 | /** |
||
| 401 | * Add a denyChild rule |
||
| 402 | * |
||
| 403 | * @param string $tagName Name of the target tag |
||
| 404 | * @return self |
||
| 405 | */ |
||
| 406 | public function denyChild($tagName) |
||
| 410 | |||
| 411 | /** |
||
| 412 | * Add a denyDescendant rule |
||
| 413 | * |
||
| 414 | * @param string $tagName Name of the target tag |
||
| 415 | * @return self |
||
| 416 | */ |
||
| 417 | public function denyDescendant($tagName) |
||
| 421 | |||
| 422 | /** |
||
| 423 | * Add a disableAutoLineBreaks rule |
||
| 424 | * |
||
| 425 | * @param bool $bool Whether or not automatic line breaks should be disabled |
||
| 426 | * @return self |
||
| 427 | */ |
||
| 428 | public function disableAutoLineBreaks($bool = true) |
||
| 432 | |||
| 433 | /** |
||
| 434 | * Add a enableAutoLineBreaks rule |
||
| 435 | * |
||
| 436 | * @param bool $bool Whether or not automatic line breaks should be enabled |
||
| 437 | * @return self |
||
| 438 | */ |
||
| 439 | public function enableAutoLineBreaks($bool = true) |
||
| 443 | |||
| 444 | /** |
||
| 445 | * Add a fosterParent rule |
||
| 446 | * |
||
| 447 | * @param string $tagName Name of the target tag |
||
| 448 | * @return self |
||
| 449 | */ |
||
| 450 | public function fosterParent($tagName) |
||
| 454 | |||
| 455 | /** |
||
| 456 | * Ignore (some) whitespace around tags |
||
| 457 | * |
||
| 458 | * When true, some whitespace around this tag will be ignored (not transformed to line breaks.) |
||
| 459 | * Up to 2 lines outside of a tag pair and 1 line inside of it: |
||
| 460 | * {2 lines}{START_TAG}{1 line}{CONTENT}{1 line}{END_TAG}{2 lines} |
||
| 461 | * |
||
| 462 | * @param bool $bool Whether whitespace around this tag should be ignored |
||
| 463 | * @return self |
||
| 464 | */ |
||
| 465 | public function ignoreSurroundingWhitespace($bool = true) |
||
| 469 | |||
| 470 | /** |
||
| 471 | * Add an ignoreTags rule |
||
| 472 | * |
||
| 473 | * @param bool $bool Whether to silently ignore all tags until current tag is closed |
||
| 474 | * @return self |
||
| 475 | */ |
||
| 476 | public function ignoreTags($bool = true) |
||
| 480 | |||
| 481 | /** |
||
| 482 | * Add an ignoreText rule |
||
| 483 | * |
||
| 484 | * @param bool $bool Whether or not the tag should ignore text nodes |
||
| 485 | * @return self |
||
| 486 | */ |
||
| 487 | public function ignoreText($bool = true) |
||
| 491 | |||
| 492 | /** |
||
| 493 | * Add a isTransparent rule |
||
| 494 | * |
||
| 495 | * @param bool $bool Whether or not the tag should use the "transparent" content model |
||
| 496 | * @return self |
||
| 497 | */ |
||
| 498 | public function isTransparent($bool = true) |
||
| 502 | |||
| 503 | /** |
||
| 504 | * Add a preventLineBreaks rule |
||
| 505 | * |
||
| 506 | * @param bool $bool Whether or not manual line breaks should be ignored in this tag's context |
||
| 507 | * @return self |
||
| 508 | */ |
||
| 509 | public function preventLineBreaks($bool = true) |
||
| 513 | |||
| 514 | /** |
||
| 515 | * Add a requireParent rule |
||
| 516 | * |
||
| 517 | * @param string $tagName Name of the target tag |
||
| 518 | * @return self |
||
| 519 | */ |
||
| 520 | public function requireParent($tagName) |
||
| 524 | |||
| 525 | /** |
||
| 526 | * Add a requireAncestor rule |
||
| 527 | * |
||
| 528 | * @param string $tagName Name of the target tag |
||
| 529 | * @return self |
||
| 530 | */ |
||
| 531 | public function requireAncestor($tagName) |
||
| 535 | |||
| 536 | /** |
||
| 537 | * Add a suspendAutoLineBreaks rule |
||
| 538 | * |
||
| 539 | * @param bool $bool Whether or not automatic line breaks should be temporarily suspended |
||
| 540 | * @return self |
||
| 541 | */ |
||
| 542 | public function suspendAutoLineBreaks($bool = true) |
||
| 546 | |||
| 547 | /** |
||
| 548 | * Add a trimFirstLine rule |
||
| 549 | * |
||
| 550 | * @param bool $bool Whether the white space inside this tag should be trimmed |
||
| 551 | * @return self |
||
| 552 | */ |
||
| 553 | public function trimFirstLine($bool = true) |
||
| 557 | } |