Complex classes like HtmlFormatter 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 HtmlFormatter, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 38 | class HtmlFormatter {
|
||
| 39 | |||
| 40 | /** |
||
| 41 | * Determines which elements start on a new line and which function as block |
||
| 42 | * @var array('element' => array('new_line' => true, 'as_block' => true, 'format_inside' => true))
|
||
| 43 | */ |
||
| 44 | var $block_elements = array( |
||
|
|
|||
| 45 | 'p' => array('new_line' => true, 'as_block' => true, 'format_inside' => true),
|
||
| 46 | 'h1' => array('new_line' => true, 'as_block' => true, 'format_inside' => true),
|
||
| 47 | 'h2' => array('new_line' => true, 'as_block' => true, 'format_inside' => true),
|
||
| 48 | 'h3' => array('new_line' => true, 'as_block' => true, 'format_inside' => true),
|
||
| 49 | 'h4' => array('new_line' => true, 'as_block' => true, 'format_inside' => true),
|
||
| 50 | 'h5' => array('new_line' => true, 'as_block' => true, 'format_inside' => true),
|
||
| 51 | 'h6' => array('new_line' => true, 'as_block' => true, 'format_inside' => true),
|
||
| 52 | |||
| 53 | 'form' => array('new_line' => true, 'as_block' => true, 'format_inside' => true),
|
||
| 54 | 'fieldset' => array('new_line' => true, 'as_block' => true, 'format_inside' => true),
|
||
| 55 | 'legend' => array('new_line' => true, 'as_block' => false, 'format_inside' => true),
|
||
| 56 | 'dl' => array('new_line' => true, 'as_block' => false, 'format_inside' => true),
|
||
| 57 | 'dt' => array('new_line' => true, 'as_block' => false, 'format_inside' => true),
|
||
| 58 | 'dd' => array('new_line' => true, 'as_block' => true, 'format_inside' => true),
|
||
| 59 | 'ol' => array('new_line' => true, 'as_block' => true, 'format_inside' => true),
|
||
| 60 | 'ul' => array('new_line' => true, 'as_block' => true, 'format_inside' => true),
|
||
| 61 | 'li' => array('new_line' => true, 'as_block' => false, 'format_inside' => true),
|
||
| 62 | |||
| 63 | 'table' => array('new_line' => true, 'as_block' => true, 'format_inside' => true),
|
||
| 64 | 'tr' => array('new_line' => true, 'as_block' => true, 'format_inside' => true),
|
||
| 65 | |||
| 66 | 'dir' => array('new_line' => true, 'as_block' => true, 'format_inside' => true),
|
||
| 67 | 'menu' => array('new_line' => true, 'as_block' => true, 'format_inside' => true),
|
||
| 68 | 'address' => array('new_line' => true, 'as_block' => true, 'format_inside' => true),
|
||
| 69 | 'blockquote' => array('new_line' => true, 'as_block' => true, 'format_inside' => true),
|
||
| 70 | 'center' => array('new_line' => true, 'as_block' => true, 'format_inside' => true),
|
||
| 71 | 'del' => array('new_line' => true, 'as_block' => false, 'format_inside' => true),
|
||
| 72 | //'div' => array('new_line' => false, 'as_block' => true, 'format_inside' => true),
|
||
| 73 | 'hr' => array('new_line' => true, 'as_block' => true, 'format_inside' => true),
|
||
| 74 | 'ins' => array('new_line' => true, 'as_block' => true, 'format_inside' => true),
|
||
| 75 | 'noscript' => array('new_line' => true, 'as_block' => true, 'format_inside' => true),
|
||
| 76 | 'pre' => array('new_line' => true, 'as_block' => true, 'format_inside' => false),
|
||
| 77 | 'script' => array('new_line' => true, 'as_block' => true, 'format_inside' => true),
|
||
| 78 | 'style' => array('new_line' => true, 'as_block' => true, 'format_inside' => true),
|
||
| 79 | |||
| 80 | 'html' => array('new_line' => true, 'as_block' => true, 'format_inside' => true),
|
||
| 81 | 'head' => array('new_line' => true, 'as_block' => true, 'format_inside' => true),
|
||
| 82 | 'body' => array('new_line' => true, 'as_block' => true, 'format_inside' => true),
|
||
| 83 | 'title' => array('new_line' => true, 'as_block' => false, 'format_inside' => false)
|
||
| 84 | ); |
||
| 85 | |||
| 86 | /** |
||
| 87 | * Determines which characters are considered whitespace |
||
| 88 | * @var array("\t" => true) True to recognize as new line
|
||
| 89 | */ |
||
| 90 | var $whitespace = array( |
||
| 91 | ' ' => false, |
||
| 92 | "\t" => false, |
||
| 93 | "\x0B" => false, |
||
| 94 | "\0" => false, |
||
| 95 | "\n" => true, |
||
| 96 | "\r" => true |
||
| 97 | ); |
||
| 98 | |||
| 99 | /** |
||
| 100 | * String that is used to generate correct indenting |
||
| 101 | * @var string |
||
| 102 | */ |
||
| 103 | var $indent_string = ' '; |
||
| 104 | |||
| 105 | /** |
||
| 106 | * String that is used to break lines |
||
| 107 | * @var string |
||
| 108 | */ |
||
| 109 | var $linebreak_string = "\n"; |
||
| 110 | |||
| 111 | /** |
||
| 112 | * Other formatting options |
||
| 113 | * @var array |
||
| 114 | */ |
||
| 115 | public $options = array( |
||
| 116 | 'img_alt' => '', |
||
| 117 | 'self_close_str' => null, |
||
| 118 | 'attribute_shorttag' => false, |
||
| 119 | 'sort_attributes' => false, |
||
| 120 | 'attributes_case' => CASE_LOWER, |
||
| 121 | 'minify_script' => true |
||
| 122 | ); |
||
| 123 | |||
| 124 | /** |
||
| 125 | * Errors found during formatting |
||
| 126 | * @var array |
||
| 127 | */ |
||
| 128 | var $errors = array(); |
||
| 129 | |||
| 130 | |||
| 131 | /** |
||
| 132 | * Class constructor |
||
| 133 | * @param array $options {@link $options}
|
||
| 134 | */ |
||
| 135 | function __construct($options = array()) {
|
||
| 144 | |||
| 145 | #php4 PHP4 class constructor compatibility |
||
| 146 | #function HtmlFormatter($options = array()) {return $this->__construct($options);}
|
||
| 147 | #php4e |
||
| 148 | |||
| 149 | /** |
||
| 150 | * Class magic invoke method, performs {@link format()}
|
||
| 151 | * @access private |
||
| 152 | */ |
||
| 153 | function __invoke(&$node) {
|
||
| 156 | |||
| 157 | /** |
||
| 158 | * Minifies HTML / removes unneeded whitespace |
||
| 159 | * @param DomNode $root |
||
| 160 | * @param bool $strip_comments |
||
| 161 | * @param bool $recursive |
||
| 162 | */ |
||
| 163 | static function minify_html(&$root, $strip_comments = true, $recursive = true) {
|
||
| 179 | |||
| 180 | /** |
||
| 181 | * Minifies javascript using JSMin+ |
||
| 182 | * @param DomNode $root |
||
| 183 | * @param string $indent_string |
||
| 184 | * @param bool $wrap_comment Wrap javascript in HTML comments (<!-- ~text~ //-->) |
||
| 185 | * @param bool $recursive |
||
| 186 | * @return bool|array Array of errors on failure, true on succes |
||
| 187 | */ |
||
| 188 | static function minify_javascript(&$root, $indent_string = ' ', $wrap_comment = true, $recursive = true) {
|
||
| 236 | |||
| 237 | /** |
||
| 238 | * Formats HTML |
||
| 239 | * @param DomNode $root |
||
| 240 | * @param bool $recursive |
||
| 241 | * @access private |
||
| 242 | */ |
||
| 243 | function format_html(&$root, $recursive = null) {
|
||
| 361 | |||
| 362 | /** |
||
| 363 | * Formats HTML/Javascript |
||
| 364 | * @param DomNode $root |
||
| 365 | * @see format_html() |
||
| 366 | */ |
||
| 367 | function format(&$node) {
|
||
| 379 | } |
||
| 380 | |||
| 381 | ?> |
||
The PSR-2 coding standard requires that all properties in a class have their visibility explicitly declared. If you declare a property using
the property is implicitly global.
To learn more about the PSR-2, please see the PHP-FIG site on the PSR-2.