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 |
||
| 22 | class Ruleset extends Collection implements ArrayAccess, ConfigProvider |
||
| 23 | { |
||
| 24 | /** |
||
| 25 | * Constructor |
||
| 26 | */ |
||
| 27 | public function __construct() |
||
| 32 | |||
| 33 | //========================================================================== |
||
| 34 | // ArrayAccess methods |
||
| 35 | //========================================================================== |
||
| 36 | |||
| 37 | /** |
||
| 38 | * Test whether a rule category exists |
||
| 39 | * |
||
| 40 | * @param string $k Rule name, e.g. "allowChild" or "isTransparent" |
||
| 41 | */ |
||
| 42 | public function offsetExists($k) |
||
| 46 | |||
| 47 | /** |
||
| 48 | * Return the content of a rule category |
||
| 49 | * |
||
| 50 | * @param string $k Rule name, e.g. "allowChild" or "isTransparent" |
||
| 51 | * @return mixed |
||
| 52 | */ |
||
| 53 | public function offsetGet($k) |
||
| 57 | |||
| 58 | /** |
||
| 59 | * Not supported |
||
| 60 | */ |
||
| 61 | public function offsetSet($k, $v) |
||
| 65 | |||
| 66 | /** |
||
| 67 | * Clear a subset of the rules |
||
| 68 | * |
||
| 69 | * @see clear() |
||
| 70 | * |
||
| 71 | * @param string $k Rule name, e.g. "allowChild" or "isTransparent" |
||
| 72 | */ |
||
| 73 | public function offsetUnset($k) |
||
| 77 | |||
| 78 | //========================================================================== |
||
| 79 | // Generic methods |
||
| 80 | //========================================================================== |
||
| 81 | |||
| 82 | /** |
||
| 83 | * {@inheritdoc} |
||
| 84 | */ |
||
| 85 | public function asConfig() |
||
| 142 | |||
| 143 | /** |
||
| 144 | * Merge a set of rules into this collection |
||
| 145 | * |
||
| 146 | * @param array|Ruleset $rules 2D array of rule definitions, or instance of Ruleset |
||
| 147 | * @param bool $overwrite Whether to overwrite scalar rules (e.g. boolean rules) |
||
| 148 | */ |
||
| 149 | public function merge($rules, $overwrite = true) |
||
| 172 | |||
| 173 | /** |
||
| 174 | * Remove a specific rule, or all the rules of a given type |
||
| 175 | * |
||
| 176 | * @param string $type Type of rules to clear |
||
| 177 | * @param string $tagName Name of the target tag, or none to remove all rules of given type |
||
| 178 | * @return void |
||
| 179 | */ |
||
| 180 | public function remove($type, $tagName = null) |
||
| 216 | |||
| 217 | //========================================================================== |
||
| 218 | // Rules |
||
| 219 | //========================================================================== |
||
| 220 | |||
| 221 | /** |
||
| 222 | * Add a boolean rule |
||
| 223 | * |
||
| 224 | * @param string $ruleName Name of the rule |
||
| 225 | * @param bool $bool Whether to enable or disable the rule |
||
| 226 | * @return self |
||
| 227 | */ |
||
| 228 | protected function addBooleanRule($ruleName, $bool) |
||
| 239 | |||
| 240 | /** |
||
| 241 | * Add a targeted rule |
||
| 242 | * |
||
| 243 | * @param string $ruleName Name of the rule |
||
| 244 | * @param string $tagName Name of the target tag |
||
| 245 | * @return self |
||
| 246 | */ |
||
| 247 | protected function addTargetedRule($ruleName, $tagName) |
||
| 253 | |||
| 254 | /** |
||
| 255 | * Add an allowChild rule |
||
| 256 | * |
||
| 257 | * @param string $tagName Name of the target tag |
||
| 258 | * @return self |
||
| 259 | */ |
||
| 260 | public function allowChild($tagName) |
||
| 264 | |||
| 265 | /** |
||
| 266 | * Add an allowDescendant rule |
||
| 267 | * |
||
| 268 | * @param string $tagName Name of the target tag |
||
| 269 | * @return self |
||
| 270 | */ |
||
| 271 | public function allowDescendant($tagName) |
||
| 275 | |||
| 276 | /** |
||
| 277 | * Add an autoClose rule |
||
| 278 | * |
||
| 279 | * NOTE: this rule exists so that plugins don't have to specifically handle tags whose end tag |
||
| 280 | * may/must be omitted such as <hr> or [img] |
||
| 281 | * |
||
| 282 | * @param bool $bool Whether or not the tag should automatically be closed if its start tag is not followed by an end tag |
||
| 283 | * @return self |
||
| 284 | */ |
||
| 285 | public function autoClose($bool = true) |
||
| 289 | |||
| 290 | /** |
||
| 291 | * Add an autoReopen rule |
||
| 292 | * |
||
| 293 | * @param bool $bool Whether or not the tag should automatically be reopened if closed by an end tag of a different name |
||
| 294 | * @return self |
||
| 295 | */ |
||
| 296 | public function autoReopen($bool = true) |
||
| 300 | |||
| 301 | /** |
||
| 302 | * Add a breakParagraph rule |
||
| 303 | * |
||
| 304 | * @param bool $bool Whether or not this tag breaks current paragraph if applicable |
||
| 305 | * @return self |
||
| 306 | */ |
||
| 307 | public function breakParagraph($bool = true) |
||
| 311 | |||
| 312 | /** |
||
| 313 | * Add a closeAncestor rule |
||
| 314 | * |
||
| 315 | * @param string $tagName Name of the target tag |
||
| 316 | * @return self |
||
| 317 | */ |
||
| 318 | public function closeAncestor($tagName) |
||
| 322 | |||
| 323 | /** |
||
| 324 | * Add a closeParent rule |
||
| 325 | * |
||
| 326 | * @param string $tagName Name of the target tag |
||
| 327 | * @return self |
||
| 328 | */ |
||
| 329 | public function closeParent($tagName) |
||
| 333 | |||
| 334 | /** |
||
| 335 | * Add a createChild rule |
||
| 336 | * |
||
| 337 | * @param string $tagName Name of the target tag |
||
| 338 | * @return self |
||
| 339 | */ |
||
| 340 | public function createChild($tagName) |
||
| 344 | |||
| 345 | /** |
||
| 346 | * Add a createParagraphs rule |
||
| 347 | * |
||
| 348 | * @param bool $bool Whether or not paragraphs should automatically be created to handle content |
||
| 349 | * @return self |
||
| 350 | */ |
||
| 351 | public function createParagraphs($bool = true) |
||
| 355 | |||
| 356 | /** |
||
| 357 | * Set the default child rule |
||
| 358 | * |
||
| 359 | * @param string $rule Either "allow" or "deny" |
||
| 360 | * @return self |
||
| 361 | */ |
||
| 362 | public function defaultChildRule($rule) |
||
| 373 | |||
| 374 | /** |
||
| 375 | * Set the default descendant rule |
||
| 376 | * |
||
| 377 | * @param string $rule Either "allow" or "deny" |
||
| 378 | * @return self |
||
| 379 | */ |
||
| 380 | public function defaultDescendantRule($rule) |
||
| 391 | |||
| 392 | /** |
||
| 393 | * Add a denyChild rule |
||
| 394 | * |
||
| 395 | * @param string $tagName Name of the target tag |
||
| 396 | * @return self |
||
| 397 | */ |
||
| 398 | public function denyChild($tagName) |
||
| 402 | |||
| 403 | /** |
||
| 404 | * Add a denyDescendant rule |
||
| 405 | * |
||
| 406 | * @param string $tagName Name of the target tag |
||
| 407 | * @return self |
||
| 408 | */ |
||
| 409 | public function denyDescendant($tagName) |
||
| 413 | |||
| 414 | /** |
||
| 415 | * Add a disableAutoLineBreaks rule |
||
| 416 | * |
||
| 417 | * @param bool $bool Whether or not automatic line breaks should be disabled |
||
| 418 | * @return self |
||
| 419 | */ |
||
| 420 | public function disableAutoLineBreaks($bool = true) |
||
| 424 | |||
| 425 | /** |
||
| 426 | * Add a enableAutoLineBreaks rule |
||
| 427 | * |
||
| 428 | * @param bool $bool Whether or not automatic line breaks should be enabled |
||
| 429 | * @return self |
||
| 430 | */ |
||
| 431 | public function enableAutoLineBreaks($bool = true) |
||
| 435 | |||
| 436 | /** |
||
| 437 | * Add a fosterParent rule |
||
| 438 | * |
||
| 439 | * @param string $tagName Name of the target tag |
||
| 440 | * @return self |
||
| 441 | */ |
||
| 442 | public function fosterParent($tagName) |
||
| 446 | |||
| 447 | /** |
||
| 448 | * Ignore (some) whitespace around tags |
||
| 449 | * |
||
| 450 | * When true, some whitespace around this tag will be ignored (not transformed to line breaks.) |
||
| 451 | * Up to 2 lines outside of a tag pair and 1 line inside of it: |
||
| 452 | * {2 lines}{START_TAG}{1 line}{CONTENT}{1 line}{END_TAG}{2 lines} |
||
| 453 | * |
||
| 454 | * @param bool $bool Whether whitespace around this tag should be ignored |
||
| 455 | * @return self |
||
| 456 | */ |
||
| 457 | public function ignoreSurroundingWhitespace($bool = true) |
||
| 461 | |||
| 462 | /** |
||
| 463 | * Add an ignoreTags rule |
||
| 464 | * |
||
| 465 | * @param bool $bool Whether to silently ignore all tags until current tag is closed |
||
| 466 | * @return self |
||
| 467 | */ |
||
| 468 | public function ignoreTags($bool = true) |
||
| 472 | |||
| 473 | /** |
||
| 474 | * Add an ignoreText rule |
||
| 475 | * |
||
| 476 | * @param bool $bool Whether or not the tag should ignore text nodes |
||
| 477 | * @return self |
||
| 478 | */ |
||
| 479 | public function ignoreText($bool = true) |
||
| 483 | |||
| 484 | /** |
||
| 485 | * Add a isTransparent rule |
||
| 486 | * |
||
| 487 | * @param bool $bool Whether or not the tag should use the "transparent" content model |
||
| 488 | * @return self |
||
| 489 | */ |
||
| 490 | public function isTransparent($bool = true) |
||
| 494 | |||
| 495 | /** |
||
| 496 | * Add a preventLineBreaks rule |
||
| 497 | * |
||
| 498 | * @param bool $bool Whether or not manual line breaks should be ignored in this tag's context |
||
| 499 | * @return self |
||
| 500 | */ |
||
| 501 | public function preventLineBreaks($bool = true) |
||
| 505 | |||
| 506 | /** |
||
| 507 | * Add a requireParent rule |
||
| 508 | * |
||
| 509 | * @param string $tagName Name of the target tag |
||
| 510 | * @return self |
||
| 511 | */ |
||
| 512 | public function requireParent($tagName) |
||
| 516 | |||
| 517 | /** |
||
| 518 | * Add a requireAncestor rule |
||
| 519 | * |
||
| 520 | * @param string $tagName Name of the target tag |
||
| 521 | * @return self |
||
| 522 | */ |
||
| 523 | public function requireAncestor($tagName) |
||
| 527 | |||
| 528 | /** |
||
| 529 | * Add a suspendAutoLineBreaks rule |
||
| 530 | * |
||
| 531 | * @param bool $bool Whether or not automatic line breaks should be temporarily suspended |
||
| 532 | * @return self |
||
| 533 | */ |
||
| 534 | public function suspendAutoLineBreaks($bool = true) |
||
| 538 | |||
| 539 | /** |
||
| 540 | * Add a trimFirstLine rule |
||
| 541 | * |
||
| 542 | * @param bool $bool Whether the white space inside this tag should be trimmed |
||
| 543 | * @return self |
||
| 544 | */ |
||
| 545 | public function trimFirstLine($bool = true) |
||
| 549 | } |