Complex classes like HTMLText 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 HTMLText, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 13 | class HTMLText extends Text { |
||
| 14 | private static $escape_type = 'xml'; |
||
| 15 | |||
| 16 | private static $casting = array( |
||
| 17 | "AbsoluteLinks" => "HTMLText", |
||
| 18 | "BigSummary" => "HTMLText", |
||
| 19 | "ContextSummary" => "HTMLText", |
||
| 20 | "FirstParagraph" => "HTMLText", |
||
| 21 | "FirstSentence" => "HTMLText", |
||
| 22 | "LimitCharacters" => "HTMLText", |
||
| 23 | "LimitSentences" => "HTMLText", |
||
| 24 | "Lower" => "HTMLText", |
||
| 25 | "LowerCase" => "HTMLText", |
||
| 26 | "Summary" => "HTMLText", |
||
| 27 | "Upper" => "HTMLText", |
||
| 28 | "UpperCase" => "HTMLText", |
||
| 29 | 'EscapeXML' => 'HTMLText', |
||
| 30 | 'LimitWordCount' => 'HTMLText', |
||
| 31 | 'LimitWordCountXML' => 'HTMLText', |
||
| 32 | 'NoHTML' => 'Text', |
||
| 33 | ); |
||
| 34 | |||
| 35 | protected $processShortcodes = true; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * Check if shortcodes are enabled |
||
| 39 | * |
||
| 40 | * @return bool |
||
| 41 | */ |
||
| 42 | public function getProcessShortcodes() { |
||
| 45 | |||
| 46 | /** |
||
| 47 | * Set shortcodes on or off by default |
||
| 48 | * |
||
| 49 | * @param bool $process |
||
| 50 | * @return $this |
||
| 51 | */ |
||
| 52 | public function setProcessShortcodes($process) { |
||
| 56 | |||
| 57 | protected $whitelist = false; |
||
| 58 | |||
| 59 | public function __construct($name = null, $options = array()) { |
||
| 66 | |||
| 67 | /** |
||
| 68 | * @param array $options |
||
| 69 | * |
||
| 70 | * Options accepted in addition to those provided by Text: |
||
| 71 | * |
||
| 72 | * - shortcodes: If true, shortcodes will be turned into the appropriate HTML. |
||
| 73 | * If false, shortcodes will not be processed. |
||
| 74 | * |
||
| 75 | * - whitelist: If provided, a comma-separated list of elements that will be allowed to be stored |
||
| 76 | * (be careful on relying on this for XSS protection - some seemingly-safe elements allow |
||
| 77 | * attributes that can be exploited, for instance <img onload="exploiting_code();" src="..." />) |
||
| 78 | * Text nodes outside of HTML tags are filtered out by default, but may be included by adding |
||
| 79 | * the text() directive. E.g. 'link,meta,text()' will allow only <link /> <meta /> and text at |
||
| 80 | * the root level. |
||
| 81 | */ |
||
| 82 | public function setOptions(array $options = array()) { |
||
| 98 | |||
| 99 | /** |
||
| 100 | * Create a summary of the content. This will be some section of the first paragraph, limited by |
||
| 101 | * $maxWords. All internal tags are stripped out - the return value is a string |
||
| 102 | * |
||
| 103 | * This is sort of the HTML aware equivilent to Text#Summary, although the logic for summarising is not exactly |
||
| 104 | * the same |
||
| 105 | * |
||
| 106 | * @param int $maxWords Maximum number of words to return - may return less, but never more. Pass -1 for no limit |
||
| 107 | * @param int $flex Number of words to search through when looking for a nice cut point |
||
| 108 | * @param string $add What to add to the end of the summary if we cut at a less-than-ideal cut point |
||
| 109 | * @return string A nice(ish) summary with no html tags (but possibly still some html entities) |
||
| 110 | * |
||
| 111 | * @see framework/core/model/fieldtypes/Text#Summary($maxWords) |
||
| 112 | */ |
||
| 113 | public function Summary($maxWords = 50, $flex = 15, $add = '...') { |
||
| 174 | |||
| 175 | /** |
||
| 176 | * Returns the first sentence from the first paragraph. If it can't figure out what the first paragraph is (or |
||
| 177 | * there isn't one), it returns the same as Summary() |
||
| 178 | * |
||
| 179 | * This is the HTML aware equivilent to Text#FirstSentence |
||
| 180 | * |
||
| 181 | * @see framework/core/model/fieldtypes/Text#FirstSentence() |
||
| 182 | */ |
||
| 183 | public function FirstSentence() { |
||
| 199 | |||
| 200 | public function RAW() { |
||
| 208 | |||
| 209 | /** |
||
| 210 | * Return the value of the field with relative links converted to absolute urls (with placeholders parsed). |
||
| 211 | * @return string |
||
| 212 | */ |
||
| 213 | public function AbsoluteLinks() { |
||
| 216 | |||
| 217 | public function forTemplate() { |
||
| 220 | |||
| 221 | public function prepValueForDB($value) { |
||
| 224 | |||
| 225 | /** |
||
| 226 | * Filter the given $value string through the whitelist filter |
||
| 227 | * |
||
| 228 | * @param string $value Input html content |
||
| 229 | * @return string Value with all non-whitelisted content stripped (if applicable) |
||
| 230 | */ |
||
| 231 | public function whitelistContent($value) { |
||
| 253 | |||
| 254 | /** |
||
| 255 | * Returns true if the field has meaningful content. |
||
| 256 | * Excludes null content like <h1></h1>, <p></p> ,etc |
||
| 257 | * |
||
| 258 | * @return boolean |
||
| 259 | */ |
||
| 260 | public function exists() { |
||
| 281 | |||
| 282 | public function scaffoldFormField($title = null, $params = null) { |
||
| 285 | |||
| 286 | public function scaffoldSearchField($title = null, $params = null) { |
||
| 289 | |||
| 290 | } |
||
| 291 | |||
| 293 |