Complex classes like Html2Text 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 Html2Text, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 26 | class Html2Text |
||
| 27 | { |
||
| 28 | /** |
||
| 29 | * Contains the HTML content to convert. |
||
| 30 | * |
||
| 31 | * @type string |
||
| 32 | */ |
||
| 33 | protected $html; |
||
| 34 | |||
| 35 | /** |
||
| 36 | * Contains the converted, formatted text. |
||
| 37 | * |
||
| 38 | * @type string |
||
| 39 | */ |
||
| 40 | protected $text; |
||
| 41 | |||
| 42 | /** |
||
| 43 | * Maximum width of the formatted text, in columns. |
||
| 44 | * |
||
| 45 | * Set this value to 0 (or less) to ignore word wrapping |
||
| 46 | * and not constrain text to a fixed-width column. |
||
| 47 | * |
||
| 48 | * @type int |
||
| 49 | */ |
||
| 50 | protected $width = 70; |
||
| 51 | |||
| 52 | /** |
||
| 53 | * List of preg* regular expression patterns to search for, |
||
| 54 | * used in conjunction with $replace. |
||
| 55 | * |
||
| 56 | * @type array |
||
| 57 | * @see $replace |
||
| 58 | */ |
||
| 59 | protected $search = [ |
||
| 60 | "/\r/", // Non-legal carriage return |
||
| 61 | "/[\n\t]+/", // Newlines and tabs |
||
| 62 | '/<head[^>]*>.*?<\/head>/i', // <head> |
||
| 63 | '/<script[^>]*>.*?<\/script>/i', // <script>s -- which strip_tags supposedly has problems with |
||
| 64 | '/<style[^>]*>.*?<\/style>/i', // <style>s -- which strip_tags supposedly has problems with |
||
| 65 | '/<p[^>]*>/i', // <P> |
||
| 66 | '/<br[^>]*>/i', // <br> |
||
| 67 | '/<i[^>]*>(.*?)<\/i>/i', // <i> |
||
| 68 | '/<em[^>]*>(.*?)<\/em>/i', // <em> |
||
| 69 | '/(<ul[^>]*>|<\/ul>)/i', // <ul> and </ul> |
||
| 70 | '/(<ol[^>]*>|<\/ol>)/i', // <ol> and </ol> |
||
| 71 | '/(<dl[^>]*>|<\/dl>)/i', // <dl> and </dl> |
||
| 72 | '/<li[^>]*>(.*?)<\/li>/i', // <li> and </li> |
||
| 73 | '/<dd[^>]*>(.*?)<\/dd>/i', // <dd> and </dd> |
||
| 74 | '/<dt[^>]*>(.*?)<\/dt>/i', // <dt> and </dt> |
||
| 75 | '/<li[^>]*>/i', // <li> |
||
| 76 | '/<hr[^>]*>/i', // <hr> |
||
| 77 | '/<div[^>]*>/i', // <div> |
||
| 78 | '/(<table[^>]*>|<\/table>)/i', // <table> and </table> |
||
| 79 | '/(<tr[^>]*>|<\/tr>)/i', // <tr> and </tr> |
||
| 80 | '/<td[^>]*>(.*?)<\/td>/i', // <td> and </td> |
||
| 81 | '/<span class="_html2text_ignore">.+?<\/span>/i', // <span class="_html2text_ignore">...</span> |
||
| 82 | ]; |
||
| 83 | |||
| 84 | /** |
||
| 85 | * List of pattern replacements corresponding to patterns searched. |
||
| 86 | * |
||
| 87 | * @type array |
||
| 88 | * @see $search |
||
| 89 | */ |
||
| 90 | protected $replace = [ |
||
| 91 | '', // Non-legal carriage return |
||
| 92 | ' ', // Newlines and tabs |
||
| 93 | '', // <head> |
||
| 94 | '', // <script>s -- which strip_tags supposedly has problems with |
||
| 95 | '', // <style>s -- which strip_tags supposedly has problems with |
||
| 96 | "\n\n", // <P> |
||
| 97 | "\n", // <br> |
||
| 98 | '_\\1_', // <i> |
||
| 99 | '_\\1_', // <em> |
||
| 100 | "\n\n", // <ul> and </ul> |
||
| 101 | "\n\n", // <ol> and </ol> |
||
| 102 | "\n\n", // <dl> and </dl> |
||
| 103 | "\t* \\1\n", // <li> and </li> |
||
| 104 | " \\1\n", // <dd> and </dd> |
||
| 105 | "\t* \\1", // <dt> and </dt> |
||
| 106 | "\n\t* ", // <li> |
||
| 107 | "\n-------------------------\n", // <hr> |
||
| 108 | "<div>\n", // <div> |
||
| 109 | "\n\n", // <table> and </table> |
||
| 110 | "\n", // <tr> and </tr> |
||
| 111 | "\t\t\\1\n", // <td> and </td> |
||
| 112 | '', // <span class="_html2text_ignore">...</span> |
||
| 113 | ]; |
||
| 114 | |||
| 115 | /** |
||
| 116 | * List of preg* regular expression patterns to search for, |
||
| 117 | * used in conjunction with $ent_replace. |
||
| 118 | * |
||
| 119 | * @type array |
||
| 120 | * @see $ent_replace |
||
| 121 | */ |
||
| 122 | protected $ent_search = [ |
||
| 123 | '/&(nbsp|#160);/i', // Non-breaking space |
||
| 124 | '/&(quot|rdquo|ldquo|#8220|#8221|#147|#148);/i', |
||
| 125 | // Double quotes |
||
| 126 | '/&(apos|rsquo|lsquo|#8216|#8217);/i', // Single quotes |
||
| 127 | '/>/i', // Greater-than |
||
| 128 | '/</i', // Less-than |
||
| 129 | '/&(copy|#169);/i', // Copyright |
||
| 130 | '/&(trade|#8482|#153);/i', // Trademark |
||
| 131 | '/&(reg|#174);/i', // Registered |
||
| 132 | '/&(mdash|#151|#8212);/i', // mdash |
||
| 133 | '/&(ndash|minus|#8211|#8722);/i', // ndash |
||
| 134 | '/&(bull|#149|#8226);/i', // Bullet |
||
| 135 | '/&(pound|#163);/i', // Pound sign |
||
| 136 | '/&(euro|#8364);/i', // Euro sign |
||
| 137 | '/&(amp|#38);/i', // Ampersand: see _converter() |
||
| 138 | '/[ ]{2,}/', // Runs of spaces, post-handling |
||
| 139 | ]; |
||
| 140 | |||
| 141 | /** |
||
| 142 | * List of pattern replacements corresponding to patterns searched. |
||
| 143 | * |
||
| 144 | * @type array |
||
| 145 | * @see $ent_search |
||
| 146 | */ |
||
| 147 | protected $ent_replace = [ |
||
| 148 | ' ', // Non-breaking space |
||
| 149 | '"', // Double quotes |
||
| 150 | "'", // Single quotes |
||
| 151 | '>', |
||
| 152 | '<', |
||
| 153 | '(c)', |
||
| 154 | '(tm)', |
||
| 155 | '(R)', |
||
| 156 | '--', |
||
| 157 | '-', |
||
| 158 | '*', |
||
| 159 | '£', |
||
| 160 | 'EUR', // Euro sign. € ? |
||
| 161 | '|+|amp|+|', // Ampersand: see _converter() |
||
| 162 | ' ', // Runs of spaces, post-handling |
||
| 163 | ]; |
||
| 164 | |||
| 165 | /** |
||
| 166 | * List of preg* regular expression patterns to search for |
||
| 167 | * and replace using callback function. |
||
| 168 | * |
||
| 169 | * @type array |
||
| 170 | */ |
||
| 171 | protected $callback_search = [ |
||
| 172 | '/<(a) [^>]*href=("|\')([^"\']+)\2([^>]*)>(.*?)<\/a>/i', // <a href=""> |
||
| 173 | '/<(h)[123456]( [^>]*)?>(.*?)<\/h[123456]>/i', // h1 - h6 |
||
| 174 | '/<(b)( [^>]*)?>(.*?)<\/b>/i', // <b> |
||
| 175 | '/<(strong)( [^>]*)?>(.*?)<\/strong>/i', // <strong> |
||
| 176 | '/<(th)( [^>]*)?>(.*?)<\/th>/i', // <th> and </th> |
||
| 177 | ]; |
||
| 178 | |||
| 179 | /** |
||
| 180 | * List of preg* regular expression patterns to search for in PRE body, |
||
| 181 | * used in conjunction with $pre_replace. |
||
| 182 | * |
||
| 183 | * @type array |
||
| 184 | * @see $pre_replace |
||
| 185 | */ |
||
| 186 | protected $pre_search = [ |
||
| 187 | "/\n/", |
||
| 188 | "/\t/", |
||
| 189 | '/ /', |
||
| 190 | '/<pre[^>]*>/', |
||
| 191 | '/<\/pre>/', |
||
| 192 | ]; |
||
| 193 | |||
| 194 | /** |
||
| 195 | * List of pattern replacements corresponding to patterns searched for PRE body. |
||
| 196 | * |
||
| 197 | * @type array |
||
| 198 | * @see $pre_search |
||
| 199 | */ |
||
| 200 | protected $pre_replace = [ |
||
| 201 | '<br>', |
||
| 202 | ' ', |
||
| 203 | ' ', |
||
| 204 | '', |
||
| 205 | '', |
||
| 206 | ]; |
||
| 207 | |||
| 208 | /** |
||
| 209 | * Temporary workspace used during PRE processing. |
||
| 210 | * |
||
| 211 | * @type string |
||
| 212 | */ |
||
| 213 | protected $pre_content = ''; |
||
| 214 | |||
| 215 | /** |
||
| 216 | * Contains a list of HTML tags to allow in the resulting text. |
||
| 217 | * |
||
| 218 | * @type string |
||
| 219 | * @see set_allowed_tags() |
||
| 220 | */ |
||
| 221 | protected $allowed_tags = ''; |
||
| 222 | |||
| 223 | /** |
||
| 224 | * Contains the base URL that relative links should resolve to. |
||
| 225 | * |
||
| 226 | * @type string |
||
| 227 | */ |
||
| 228 | protected $url; |
||
| 229 | |||
| 230 | /** |
||
| 231 | * Indicates whether content in the $html variable has been converted yet. |
||
| 232 | * |
||
| 233 | * @type bool |
||
| 234 | * @see $html, $text |
||
| 235 | */ |
||
| 236 | protected $_converted = false; |
||
| 237 | |||
| 238 | /** |
||
| 239 | * Contains URL addresses from links to be rendered in plain text. |
||
| 240 | * |
||
| 241 | * @type array |
||
| 242 | * @see _build_link_list() |
||
| 243 | */ |
||
| 244 | protected $_link_list = []; |
||
| 245 | |||
| 246 | /** |
||
| 247 | * Various configuration options (able to be set in the constructor) |
||
| 248 | * |
||
| 249 | * @type array |
||
| 250 | */ |
||
| 251 | protected $_options = [ |
||
| 252 | // 'none' |
||
| 253 | // 'inline' (show links inline) |
||
| 254 | // 'nextline' (show links on the next line) |
||
| 255 | // 'table' (if a table of link URLs should be listed after the text. |
||
| 256 | 'do_links' => 'inline', |
||
| 257 | // Maximum width of the formatted text, in columns. |
||
| 258 | // Set this value to 0 (or less) to ignore word wrapping |
||
| 259 | // and not constrain text to a fixed-width column. |
||
| 260 | 'width' => 70, |
||
| 261 | ]; |
||
| 262 | |||
| 263 | /** |
||
| 264 | * Constructor. |
||
| 265 | * |
||
| 266 | * If the HTML source string (or file) is supplied, the class |
||
| 267 | * will instantiate with that source propagated, all that has |
||
| 268 | * to be done it to call get_text(). |
||
| 269 | * |
||
| 270 | * @param string $source HTML content |
||
| 271 | * @param bool $from_file Indicates $source is a file to pull content from |
||
| 272 | * @param array $options Set configuration options |
||
| 273 | */ |
||
| 274 | public function __construct($source = '', $from_file = false, $options = []) |
||
| 284 | |||
| 285 | /** |
||
| 286 | * Loads source HTML into memory, either from $source string or a file. |
||
| 287 | * |
||
| 288 | * @param string $source HTML content |
||
| 289 | * @param bool $from_file Indicates $source is a file to pull content from |
||
| 290 | */ |
||
| 291 | public function set_html($source, $from_file = false) |
||
| 301 | |||
| 302 | /** |
||
| 303 | * Returns the text, converted from HTML. |
||
| 304 | * |
||
| 305 | * @return string |
||
| 306 | */ |
||
| 307 | public function get_text() |
||
| 315 | |||
| 316 | /** |
||
| 317 | * Prints the text, converted from HTML. |
||
| 318 | */ |
||
| 319 | public function print_text() |
||
| 323 | |||
| 324 | /** |
||
| 325 | * Alias to print_text(), operates identically. |
||
| 326 | * |
||
| 327 | * @see print_text() |
||
| 328 | */ |
||
| 329 | public function p() |
||
| 333 | |||
| 334 | /** |
||
| 335 | * Sets the allowed HTML tags to pass through to the resulting text. |
||
| 336 | * |
||
| 337 | * Tags should be in the form "<p>", with no corresponding closing tag. |
||
| 338 | * |
||
| 339 | * @param string $allowed_tags |
||
| 340 | */ |
||
| 341 | public function set_allowed_tags($allowed_tags = '') |
||
| 347 | |||
| 348 | /** |
||
| 349 | * Sets a base URL to handle relative links. |
||
| 350 | * |
||
| 351 | * @param string $url |
||
| 352 | */ |
||
| 353 | public function set_base_url($url = '') |
||
| 370 | |||
| 371 | /** |
||
| 372 | * Workhorse function that does actual conversion (calls _converter() method). |
||
| 373 | */ |
||
| 374 | protected function _convert() |
||
| 396 | |||
| 397 | /** |
||
| 398 | * Workhorse function that does actual conversion. |
||
| 399 | * |
||
| 400 | * First performs custom tag replacement specified by $search and |
||
| 401 | * $replace arrays. Then strips any remaining HTML tags, reduces whitespace |
||
| 402 | * and newlines to a readable format, and word wraps the text to |
||
| 403 | * $this->_options['width'] characters. |
||
| 404 | * |
||
| 405 | * @param string $text Reference to HTML content string |
||
| 406 | */ |
||
| 407 | protected function _converter(&$text) |
||
| 451 | |||
| 452 | /** |
||
| 453 | * Helper function called by preg_replace() on link replacement. |
||
| 454 | * |
||
| 455 | * Maintains an internal list of links to be displayed at the end of the |
||
| 456 | * text, with numeric indices to the original point in the text they |
||
| 457 | * appeared. Also makes an effort at identifying and handling absolute |
||
| 458 | * and relative links. |
||
| 459 | * |
||
| 460 | * @param string $link URL of the link |
||
| 461 | * @param string $display Part of the text to associate number with |
||
| 462 | * @param string|null $link_override |
||
| 463 | * |
||
| 464 | * @return string |
||
| 465 | */ |
||
| 466 | protected function _build_link_list($link, $display, $link_override = null) |
||
| 501 | |||
| 502 | /** |
||
| 503 | * Helper function for PRE body conversion. |
||
| 504 | * |
||
| 505 | * @param string $text HTML content |
||
| 506 | */ |
||
| 507 | protected function _convert_pre(&$text) |
||
| 526 | |||
| 527 | /** |
||
| 528 | * Helper function for BLOCKQUOTE body conversion. |
||
| 529 | * |
||
| 530 | * @param string $text HTML content |
||
| 531 | */ |
||
| 532 | protected function _convert_blockquotes(&$text) |
||
| 581 | |||
| 582 | /** |
||
| 583 | * Callback function for preg_replace_callback use. |
||
| 584 | * |
||
| 585 | * @param array $matches PREG matches |
||
| 586 | * |
||
| 587 | * @return string |
||
| 588 | */ |
||
| 589 | protected function _preg_callback($matches) |
||
| 613 | |||
| 614 | /** |
||
| 615 | * Callback function for preg_replace_callback use in PRE content handler. |
||
| 616 | * |
||
| 617 | * @param array $matches PREG matches |
||
| 618 | * |
||
| 619 | * @return string |
||
| 620 | */ |
||
| 621 | protected function _preg_pre_callback( |
||
| 627 | |||
| 628 | /** |
||
| 629 | * Strtoupper function with HTML tags and entities handling. |
||
| 630 | * |
||
| 631 | * @param string $str Text to convert |
||
| 632 | * |
||
| 633 | * @return string Converted text |
||
| 634 | */ |
||
| 635 | private function _toupper($str) |
||
| 649 | |||
| 650 | /** |
||
| 651 | * Strtoupper multibyte wrapper function with HTML entities handling. |
||
| 652 | * Forces mb_strtoupper-call to UTF-8. |
||
| 653 | * |
||
| 654 | * @param string $str Text to convert |
||
| 655 | * |
||
| 656 | * @return string Converted text |
||
| 657 | */ |
||
| 658 | private function _strtoupper($str) |
||
| 672 | } |
||
| 673 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.