Complex classes like ShortcodeParser 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 ShortcodeParser, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 21 | class ShortcodeParser |
||
| 22 | { |
||
| 23 | use Injectable; |
||
| 24 | use Configurable; |
||
| 25 | use Extensible; |
||
| 26 | |||
| 27 | public function __construct() |
||
| 31 | |||
| 32 | public function img_shortcode($attrs) |
||
| 36 | |||
| 37 | protected static $instances = array(); |
||
| 38 | |||
| 39 | protected static $active_instance = 'default'; |
||
| 40 | |||
| 41 | // -------------------------------------------------------------------------------------------------------------- |
||
| 42 | |||
| 43 | /** |
||
| 44 | * Registered shortcodes. Items follow this structure: |
||
| 45 | * [shortcode_name] => Array( |
||
| 46 | * [0] => class_containing_handler |
||
| 47 | * [1] => name_of_shortcode_handler_method |
||
| 48 | * ) |
||
| 49 | */ |
||
| 50 | protected $shortcodes = array(); |
||
| 51 | |||
| 52 | // -------------------------------------------------------------------------------------------------------------- |
||
| 53 | |||
| 54 | /** |
||
| 55 | * Get the {@link ShortcodeParser} instance that is attached to a particular identifier. |
||
| 56 | * |
||
| 57 | * @param string $identifier Defaults to "default". |
||
| 58 | * @return ShortcodeParser |
||
| 59 | */ |
||
| 60 | public static function get($identifier = 'default') |
||
| 68 | |||
| 69 | /** |
||
| 70 | * Get the currently active/default {@link ShortcodeParser} instance. |
||
| 71 | * |
||
| 72 | * @return ShortcodeParser |
||
| 73 | */ |
||
| 74 | public static function get_active() |
||
| 78 | |||
| 79 | /** |
||
| 80 | * Set the identifier to use for the current active/default {@link ShortcodeParser} instance. |
||
| 81 | * |
||
| 82 | * @param string $identifier |
||
| 83 | */ |
||
| 84 | public static function set_active($identifier) |
||
| 88 | |||
| 89 | // -------------------------------------------------------------------------------------------------------------- |
||
| 90 | |||
| 91 | /** |
||
| 92 | * Register a shortcode, and attach it to a PHP callback. |
||
| 93 | * |
||
| 94 | * The callback for a shortcode will have the following arguments passed to it: |
||
| 95 | * - Any parameters attached to the shortcode as an associative array (keys are lower-case). |
||
| 96 | * - Any content enclosed within the shortcode (if it is an enclosing shortcode). Note that any content within |
||
| 97 | * this will not have been parsed, and can optionally be fed back into the parser. |
||
| 98 | * - The {@link ShortcodeParser} instance used to parse the content. |
||
| 99 | * - The shortcode tag name that was matched within the parsed content. |
||
| 100 | * - An associative array of extra information about the shortcode being parsed. |
||
| 101 | * |
||
| 102 | * @param string $shortcode The shortcode tag to map to the callback - normally in lowercase_underscore format. |
||
| 103 | * @param callback $callback The callback to replace the shortcode with. |
||
| 104 | * @return $this |
||
| 105 | */ |
||
| 106 | public function register($shortcode, $callback) |
||
| 115 | |||
| 116 | /** |
||
| 117 | * Check if a shortcode has been registered. |
||
| 118 | * |
||
| 119 | * @param string $shortcode |
||
| 120 | * @return bool |
||
| 121 | */ |
||
| 122 | public function registered($shortcode) |
||
| 126 | |||
| 127 | /** |
||
| 128 | * Remove a specific registered shortcode. |
||
| 129 | * |
||
| 130 | * @param string $shortcode |
||
| 131 | */ |
||
| 132 | public function unregister($shortcode) |
||
| 138 | |||
| 139 | /** |
||
| 140 | * Get an array containing information about registered shortcodes |
||
| 141 | * |
||
| 142 | * @return array |
||
| 143 | */ |
||
| 144 | public function getRegisteredShortcodes() |
||
| 148 | |||
| 149 | /** |
||
| 150 | * Remove all registered shortcodes. |
||
| 151 | */ |
||
| 152 | public function clear() |
||
| 156 | |||
| 157 | /** |
||
| 158 | * Call a shortcode and return its replacement text |
||
| 159 | * Returns false if the shortcode isn't registered |
||
| 160 | * |
||
| 161 | * @param string $tag |
||
| 162 | * @param array $attributes |
||
| 163 | * @param string $content |
||
| 164 | * @param array $extra |
||
| 165 | * @return mixed |
||
| 166 | */ |
||
| 167 | public function callShortcode($tag, $attributes, $content, $extra = array()) |
||
| 174 | |||
| 175 | /** |
||
| 176 | * Return the text to insert in place of a shoprtcode. |
||
| 177 | * Behaviour in the case of missing shortcodes depends on the setting of ShortcodeParser::$error_behavior. |
||
| 178 | * |
||
| 179 | * @param array $tag A map containing the the following keys: |
||
| 180 | * - 'open': The name of the tag |
||
| 181 | * - 'attrs': Attributes of the tag |
||
| 182 | * - 'content': Content of the tag |
||
| 183 | * @param array $extra Extra-meta data |
||
| 184 | * @param boolean $isHTMLAllowed A boolean indicating whether it's okay to insert HTML tags into the result |
||
| 185 | * |
||
| 186 | * @return bool|mixed|string |
||
| 187 | */ |
||
| 188 | public function getShortcodeReplacementText($tag, $extra = array(), $isHTMLAllowed = true) |
||
| 207 | |||
| 208 | // -------------------------------------------------------------------------------------------------------------- |
||
| 209 | |||
| 210 | protected function removeNode($node) |
||
| 214 | |||
| 215 | /** |
||
| 216 | * @param DOMElement $new |
||
| 217 | * @param DOMElement $after |
||
| 218 | */ |
||
| 219 | protected function insertAfter($new, $after) |
||
| 230 | |||
| 231 | /** |
||
| 232 | * @param DOMNodeList $new |
||
| 233 | * @param DOMElement $after |
||
| 234 | */ |
||
| 235 | protected function insertListAfter($new, $after) |
||
| 251 | |||
| 252 | /** |
||
| 253 | * @var string |
||
| 254 | */ |
||
| 255 | protected static $marker_class = '--ss-shortcode-marker'; |
||
| 256 | |||
| 257 | protected static $block_level_elements = array( |
||
| 258 | 'address', 'article', 'aside', 'audio', 'blockquote', 'canvas', 'dd', 'div', 'dl', 'fieldset', 'figcaption', |
||
| 259 | 'figure', 'footer', 'form', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'header', 'hgroup', 'ol', 'output', 'p', |
||
| 260 | 'pre', 'section', 'table', 'ul' |
||
| 261 | ); |
||
| 262 | |||
| 263 | protected static $attrrx = ' |
||
| 264 | ([^\s\/\'"=,]+) # Name |
||
| 265 | \s* = \s* |
||
| 266 | (?: |
||
| 267 | (?:\'([^\']+)\') | # Value surrounded by \' |
||
| 268 | (?:"([^"]+)") | # Value surrounded by " |
||
| 269 | ([^\s,\]]+) # Bare value |
||
| 270 | ) |
||
| 271 | '; |
||
| 272 | |||
| 273 | protected static function attrrx() |
||
| 277 | |||
| 278 | protected static $tagrx = ' |
||
| 279 | # HTML Tag |
||
| 280 | <(?<element>(?:"[^"]*"[\'"]*|\'[^\']*\'[\'"]*|[^\'">])+)> |
||
| 281 | |||
| 282 | | # Opening tag |
||
| 283 | (?<oesc>\[?) |
||
| 284 | \[ |
||
| 285 | (?<open>\w+) |
||
| 286 | [\s,]* |
||
| 287 | (?<attrs> (?: %s [\s,]*)* ) |
||
| 288 | \/?\] |
||
| 289 | (?<cesc1>\]?) |
||
| 290 | |||
| 291 | | # Closing tag |
||
| 292 | \[\/ |
||
| 293 | (?<close>\w+) |
||
| 294 | \] |
||
| 295 | (?<cesc2>\]?) |
||
| 296 | '; |
||
| 297 | |||
| 298 | protected static function tagrx() |
||
| 302 | |||
| 303 | const WARN = 'warn'; |
||
| 304 | const STRIP = 'strip'; |
||
| 305 | const LEAVE = 'leave'; |
||
| 306 | const ERROR = 'error'; |
||
| 307 | |||
| 308 | public static $error_behavior = self::LEAVE; |
||
| 309 | |||
| 310 | |||
| 311 | /** |
||
| 312 | * Look through a string that contains shortcode tags and pull out the locations and details |
||
| 313 | * of those tags |
||
| 314 | * |
||
| 315 | * Doesn't support nested shortcode tags |
||
| 316 | * |
||
| 317 | * @param string $content |
||
| 318 | * @return array - The list of tags found. When using an open/close pair, only one item will be in the array, |
||
| 319 | * with "content" set to the text between the tags |
||
| 320 | */ |
||
| 321 | public function extractTags($content) |
||
| 425 | |||
| 426 | /** |
||
| 427 | * Replaces the shortcode tags extracted by extractTags with HTML element "markers", so that |
||
| 428 | * we can parse the resulting string as HTML and easily mutate the shortcodes in the DOM |
||
| 429 | * |
||
| 430 | * @param string $content The HTML string with [tag] style shortcodes embedded |
||
| 431 | * @param array $tags The tags extracted by extractTags |
||
| 432 | * @param callable $generator |
||
| 433 | * @return string The HTML string with [tag] style shortcodes replaced by markers |
||
| 434 | */ |
||
| 435 | protected function replaceTagsWithText($content, $tags, $generator) |
||
| 461 | |||
| 462 | /** |
||
| 463 | * Replace the shortcodes in attribute values with the calculated content |
||
| 464 | * |
||
| 465 | * We don't use markers with attributes because there's no point, it's easier to do all the matching |
||
| 466 | * in-DOM after the XML parse |
||
| 467 | * |
||
| 468 | * @param HTMLValue $htmlvalue |
||
| 469 | */ |
||
| 470 | protected function replaceAttributeTagsWithContent($htmlvalue) |
||
| 491 | |||
| 492 | /** |
||
| 493 | * Replace the element-scoped tags with markers |
||
| 494 | * |
||
| 495 | * @param string $content |
||
| 496 | * @return array |
||
| 497 | */ |
||
| 498 | protected function replaceElementTagsWithMarkers($content) |
||
| 512 | |||
| 513 | /** |
||
| 514 | * @param DOMNodeList $nodes |
||
| 515 | * @return array |
||
| 516 | */ |
||
| 517 | protected function findParentsForMarkers($nodes) |
||
| 537 | |||
| 538 | const BEFORE = 'before'; |
||
| 539 | const AFTER = 'after'; |
||
| 540 | const SPLIT = 'split'; |
||
| 541 | const INLINE = 'inline'; |
||
| 542 | |||
| 543 | /** |
||
| 544 | * Given a node with represents a shortcode marker and a location string, mutates the DOM to put the |
||
| 545 | * marker in the compliant location |
||
| 546 | * |
||
| 547 | * For shortcodes inserted BEFORE, that location is just before the block container that |
||
| 548 | * the marker is in |
||
| 549 | * |
||
| 550 | * For shortcodes inserted AFTER, that location is just after the block container that |
||
| 551 | * the marker is in |
||
| 552 | * |
||
| 553 | * For shortcodes inserted SPLIT, that location is where the marker is, but the DOM |
||
| 554 | * is split around it up to the block container the marker is in - for instance, |
||
| 555 | * |
||
| 556 | * <p>A<span>B<marker />C</span>D</p> |
||
| 557 | * |
||
| 558 | * becomes |
||
| 559 | * |
||
| 560 | * <p>A<span>B</span></p><marker /><p><span>C</span>D</p> |
||
| 561 | * |
||
| 562 | * For shortcodes inserted INLINE, no modification is needed (but in that case the shortcode handler needs to |
||
| 563 | * generate only inline blocks) |
||
| 564 | * |
||
| 565 | * @param DOMElement $node |
||
| 566 | * @param DOMElement $parent |
||
| 567 | * @param int $location ShortcodeParser::BEFORE, ShortcodeParser::SPLIT or ShortcodeParser::INLINE |
||
| 568 | */ |
||
| 569 | protected function moveMarkerToCompliantHome($node, $parent, $location) |
||
| 613 | |||
| 614 | /** |
||
| 615 | * Given a node with represents a shortcode marker and some information about the shortcode, call the |
||
| 616 | * shortcode handler & replace the marker with the actual content |
||
| 617 | * |
||
| 618 | * @param DOMElement $node |
||
| 619 | * @param array $tag |
||
| 620 | */ |
||
| 621 | protected function replaceMarkerWithContent($node, $tag) |
||
| 636 | |||
| 637 | /** |
||
| 638 | * Parse a string, and replace any registered shortcodes within it with the result of the mapped callback. |
||
| 639 | * |
||
| 640 | * @param string $content |
||
| 641 | * @return string |
||
| 642 | */ |
||
| 643 | public function parse($content) |
||
| 746 | } |
||
| 747 |
This check looks for accesses to local static members using the fully qualified name instead of
self::.While this is perfectly valid, the fully qualified name of
Certificate::TRIPLEDES_CBCcould just as well be replaced byself::TRIPLEDES_CBC. Referencing local members withself::assured the access will still work when the class is renamed, makes it perfectly clear that the member is in fact local and will usually be shorter.