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 | // ArrayAccess methods |
||
| 33 | //========================================================================== |
||
| 34 | |||
| 35 | /** |
||
| 36 | * Test whether a rule category exists |
||
| 37 | * |
||
| 38 | * @param string $k Rule name, e.g. "allowChild" or "isTransparent" |
||
| 39 | */ |
||
| 40 | public function offsetExists($k) |
||
| 44 | |||
| 45 | /** |
||
| 46 | * Return the content of a rule category |
||
| 47 | * |
||
| 48 | * @param string $k Rule name, e.g. "allowChild" or "isTransparent" |
||
| 49 | * @return mixed |
||
| 50 | */ |
||
| 51 | public function offsetGet($k) |
||
| 55 | |||
| 56 | /** |
||
| 57 | * Not supported |
||
| 58 | */ |
||
| 59 | public function offsetSet($k, $v) |
||
| 63 | |||
| 64 | /** |
||
| 65 | * Clear a subset of the rules |
||
| 66 | * |
||
| 67 | * @see clear() |
||
| 68 | * |
||
| 69 | * @param string $k Rule name, e.g. "allowChild" or "isTransparent" |
||
| 70 | */ |
||
| 71 | public function offsetUnset($k) |
||
| 75 | |||
| 76 | //========================================================================== |
||
| 77 | // Generic methods |
||
| 78 | //========================================================================== |
||
| 79 | |||
| 80 | /** |
||
| 81 | * {@inheritdoc} |
||
| 82 | */ |
||
| 83 | public function asConfig() |
||
| 138 | |||
| 139 | /** |
||
| 140 | * Merge a set of rules into this collection |
||
| 141 | * |
||
| 142 | * @param array|Ruleset $rules 2D array of rule definitions, or instance of Ruleset |
||
| 143 | * @param bool $overwrite Whether to overwrite scalar rules (e.g. boolean rules) |
||
| 144 | */ |
||
| 145 | public function merge($rules, $overwrite = true) |
||
| 168 | |||
| 169 | /** |
||
| 170 | * Remove a specific rule, or all the rules of a given type |
||
| 171 | * |
||
| 172 | * @param string $type Type of rules to clear |
||
| 173 | * @param string $tagName Name of the target tag, or none to remove all rules of given type |
||
| 174 | * @return void |
||
| 175 | */ |
||
| 176 | public function remove($type, $tagName = null) |
||
| 212 | |||
| 213 | //========================================================================== |
||
| 214 | // Rules |
||
| 215 | //========================================================================== |
||
| 216 | |||
| 217 | /** |
||
| 218 | * Add a boolean rule |
||
| 219 | * |
||
| 220 | * @param string $ruleName Name of the rule |
||
| 221 | * @param bool $bool Whether to enable or disable the rule |
||
| 222 | * @return self |
||
| 223 | */ |
||
| 224 | protected function addBooleanRule($ruleName, $bool) |
||
| 235 | |||
| 236 | /** |
||
| 237 | * Add a targeted rule |
||
| 238 | * |
||
| 239 | * @param string $ruleName Name of the rule |
||
| 240 | * @param string $tagName Name of the target tag |
||
| 241 | * @return self |
||
| 242 | */ |
||
| 243 | protected function addTargetedRule($ruleName, $tagName) |
||
| 249 | |||
| 250 | /** |
||
| 251 | * Add an allowChild rule |
||
| 252 | * |
||
| 253 | * @param string $tagName Name of the target tag |
||
| 254 | * @return self |
||
| 255 | */ |
||
| 256 | public function allowChild($tagName) |
||
| 260 | |||
| 261 | /** |
||
| 262 | * Add an allowDescendant rule |
||
| 263 | * |
||
| 264 | * @param string $tagName Name of the target tag |
||
| 265 | * @return self |
||
| 266 | */ |
||
| 267 | public function allowDescendant($tagName) |
||
| 271 | |||
| 272 | /** |
||
| 273 | * Add an autoClose rule |
||
| 274 | * |
||
| 275 | * NOTE: this rule exists so that plugins don't have to specifically handle tags whose end tag |
||
| 276 | * may/must be omitted such as <hr> or [img] |
||
| 277 | * |
||
| 278 | * @param bool $bool Whether or not the tag should automatically be closed if its start tag is not followed by an end tag |
||
| 279 | * @return self |
||
| 280 | */ |
||
| 281 | public function autoClose($bool = true) |
||
| 285 | |||
| 286 | /** |
||
| 287 | * Add an autoReopen rule |
||
| 288 | * |
||
| 289 | * @param bool $bool Whether or not the tag should automatically be reopened if closed by an end tag of a different name |
||
| 290 | * @return self |
||
| 291 | */ |
||
| 292 | public function autoReopen($bool = true) |
||
| 296 | |||
| 297 | /** |
||
| 298 | * Add a breakParagraph rule |
||
| 299 | * |
||
| 300 | * @param bool $bool Whether or not this tag breaks current paragraph if applicable |
||
| 301 | * @return self |
||
| 302 | */ |
||
| 303 | public function breakParagraph($bool = true) |
||
| 307 | |||
| 308 | /** |
||
| 309 | * Add a closeAncestor rule |
||
| 310 | * |
||
| 311 | * @param string $tagName Name of the target tag |
||
| 312 | * @return self |
||
| 313 | */ |
||
| 314 | public function closeAncestor($tagName) |
||
| 318 | |||
| 319 | /** |
||
| 320 | * Add a closeParent rule |
||
| 321 | * |
||
| 322 | * @param string $tagName Name of the target tag |
||
| 323 | * @return self |
||
| 324 | */ |
||
| 325 | public function closeParent($tagName) |
||
| 329 | |||
| 330 | /** |
||
| 331 | * Add a createChild rule |
||
| 332 | * |
||
| 333 | * @param string $tagName Name of the target tag |
||
| 334 | * @return self |
||
| 335 | */ |
||
| 336 | public function createChild($tagName) |
||
| 340 | |||
| 341 | /** |
||
| 342 | * Add a createParagraphs rule |
||
| 343 | * |
||
| 344 | * @param bool $bool Whether or not paragraphs should automatically be created to handle content |
||
| 345 | * @return self |
||
| 346 | */ |
||
| 347 | public function createParagraphs($bool = true) |
||
| 351 | |||
| 352 | /** |
||
| 353 | * Add a denyChild rule |
||
| 354 | * |
||
| 355 | * @param string $tagName Name of the target tag |
||
| 356 | * @return self |
||
| 357 | */ |
||
| 358 | public function denyChild($tagName) |
||
| 362 | |||
| 363 | /** |
||
| 364 | * Add a denyDescendant rule |
||
| 365 | * |
||
| 366 | * @param string $tagName Name of the target tag |
||
| 367 | * @return self |
||
| 368 | */ |
||
| 369 | public function denyDescendant($tagName) |
||
| 373 | |||
| 374 | /** |
||
| 375 | * Add a disableAutoLineBreaks rule |
||
| 376 | * |
||
| 377 | * @param bool $bool Whether or not automatic line breaks should be disabled |
||
| 378 | * @return self |
||
| 379 | */ |
||
| 380 | public function disableAutoLineBreaks($bool = true) |
||
| 384 | |||
| 385 | /** |
||
| 386 | * Add a enableAutoLineBreaks rule |
||
| 387 | * |
||
| 388 | * @param bool $bool Whether or not automatic line breaks should be enabled |
||
| 389 | * @return self |
||
| 390 | */ |
||
| 391 | public function enableAutoLineBreaks($bool = true) |
||
| 395 | |||
| 396 | /** |
||
| 397 | * Add a fosterParent rule |
||
| 398 | * |
||
| 399 | * @param string $tagName Name of the target tag |
||
| 400 | * @return self |
||
| 401 | */ |
||
| 402 | public function fosterParent($tagName) |
||
| 406 | |||
| 407 | /** |
||
| 408 | * Ignore (some) whitespace around tags |
||
| 409 | * |
||
| 410 | * When true, some whitespace around this tag will be ignored (not transformed to line breaks.) |
||
| 411 | * Up to 2 lines outside of a tag pair and 1 line inside of it: |
||
| 412 | * {2 lines}{START_TAG}{1 line}{CONTENT}{1 line}{END_TAG}{2 lines} |
||
| 413 | * |
||
| 414 | * @param bool $bool Whether whitespace around this tag should be ignored |
||
| 415 | * @return self |
||
| 416 | */ |
||
| 417 | public function ignoreSurroundingWhitespace($bool = true) |
||
| 421 | |||
| 422 | /** |
||
| 423 | * Add an ignoreTags rule |
||
| 424 | * |
||
| 425 | * @param bool $bool Whether to silently ignore all tags until current tag is closed |
||
| 426 | * @return self |
||
| 427 | */ |
||
| 428 | public function ignoreTags($bool = true) |
||
| 432 | |||
| 433 | /** |
||
| 434 | * Add an ignoreText rule |
||
| 435 | * |
||
| 436 | * @param bool $bool Whether or not the tag should ignore text nodes |
||
| 437 | * @return self |
||
| 438 | */ |
||
| 439 | public function ignoreText($bool = true) |
||
| 443 | |||
| 444 | /** |
||
| 445 | * Add a isTransparent rule |
||
| 446 | * |
||
| 447 | * @param bool $bool Whether or not the tag should use the "transparent" content model |
||
| 448 | * @return self |
||
| 449 | */ |
||
| 450 | public function isTransparent($bool = true) |
||
| 454 | |||
| 455 | /** |
||
| 456 | * Add a preventLineBreaks rule |
||
| 457 | * |
||
| 458 | * @param bool $bool Whether or not manual line breaks should be ignored in this tag's context |
||
| 459 | * @return self |
||
| 460 | */ |
||
| 461 | public function preventLineBreaks($bool = true) |
||
| 465 | |||
| 466 | /** |
||
| 467 | * Add a requireParent rule |
||
| 468 | * |
||
| 469 | * @param string $tagName Name of the target tag |
||
| 470 | * @return self |
||
| 471 | */ |
||
| 472 | public function requireParent($tagName) |
||
| 476 | |||
| 477 | /** |
||
| 478 | * Add a requireAncestor rule |
||
| 479 | * |
||
| 480 | * @param string $tagName Name of the target tag |
||
| 481 | * @return self |
||
| 482 | */ |
||
| 483 | public function requireAncestor($tagName) |
||
| 487 | |||
| 488 | /** |
||
| 489 | * Add a suspendAutoLineBreaks rule |
||
| 490 | * |
||
| 491 | * @param bool $bool Whether or not automatic line breaks should be temporarily suspended |
||
| 492 | * @return self |
||
| 493 | */ |
||
| 494 | public function suspendAutoLineBreaks($bool = true) |
||
| 498 | |||
| 499 | /** |
||
| 500 | * Add a trimFirstLine rule |
||
| 501 | * |
||
| 502 | * @param bool $bool Whether the white space inside this tag should be trimmed |
||
| 503 | * @return self |
||
| 504 | */ |
||
| 505 | public function trimFirstLine($bool = true) |
||
| 509 | } |