Complex classes like Normalizer 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 Normalizer, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 15 | class Normalizer extends IRProcessor |
||
| 16 | { |
||
| 17 | /** |
||
| 18 | * @var Optimizer |
||
| 19 | */ |
||
| 20 | protected $optimizer; |
||
| 21 | |||
| 22 | /** |
||
| 23 | * @var string Regexp that matches the names of all void elements |
||
| 24 | * @link http://www.w3.org/TR/html-markup/syntax.html#void-elements |
||
| 25 | */ |
||
| 26 | public $voidRegexp = '/^(?:area|base|br|col|command|embed|hr|img|input|keygen|link|meta|param|source|track|wbr)$/Di'; |
||
| 27 | |||
| 28 | /** |
||
| 29 | * @param Optimizer $optimizer |
||
| 30 | * @return void |
||
| 31 | */ |
||
| 32 | public function __construct(Optimizer $optimizer) |
||
| 36 | |||
| 37 | /** |
||
| 38 | * Normalize an IR |
||
| 39 | * |
||
| 40 | * @param DOMDocument $ir |
||
| 41 | * @return void |
||
| 42 | */ |
||
| 43 | public function normalize(DOMDocument $ir) |
||
| 55 | |||
| 56 | /** |
||
| 57 | * Add <closeTag/> elements everywhere an open start tag should be closed |
||
| 58 | * |
||
| 59 | * @param DOMDocument $ir |
||
| 60 | * @return void |
||
| 61 | */ |
||
| 62 | protected function addCloseTagElements(DOMDocument $ir) |
||
| 88 | |||
| 89 | /** |
||
| 90 | * Add an empty default <case/> to <switch/> nodes that don't have one |
||
| 91 | * |
||
| 92 | * @param DOMDocument $ir |
||
| 93 | * @return void |
||
| 94 | */ |
||
| 95 | protected function addDefaultCase(DOMDocument $ir) |
||
| 102 | |||
| 103 | /** |
||
| 104 | * Add an id attribute to <element/> nodes |
||
| 105 | * |
||
| 106 | * @param DOMDocument $ir |
||
| 107 | * @return void |
||
| 108 | */ |
||
| 109 | protected function addElementIds(DOMDocument $ir) |
||
| 117 | |||
| 118 | /** |
||
| 119 | * Get the context type for given output element |
||
| 120 | * |
||
| 121 | * @param DOMNode $output |
||
| 122 | * @return string |
||
| 123 | */ |
||
| 124 | protected function getOutputContext(DOMNode $output) |
||
| 141 | |||
| 142 | /** |
||
| 143 | * Get the ID of the closest "element" ancestor |
||
| 144 | * |
||
| 145 | * @param DOMNode $node Context node |
||
| 146 | * @return string|null |
||
| 147 | */ |
||
| 148 | protected function getParentElementId(DOMNode $node) |
||
| 160 | |||
| 161 | /** |
||
| 162 | * Test whether given element will be empty at runtime (no content, no children) |
||
| 163 | * |
||
| 164 | * @param DOMElement $ir Element in the IR |
||
| 165 | * @return string 'yes', 'maybe' or 'no' |
||
| 166 | */ |
||
| 167 | protected function isEmpty(DOMElement $ir) |
||
| 209 | |||
| 210 | /** |
||
| 211 | * Mark switch elements that are used as branch tables |
||
| 212 | * |
||
| 213 | * If a switch is used for a series of equality tests against the same attribute or variable, the |
||
| 214 | * attribute/variable is stored within the switch as "branch-key" and the values it is compared |
||
| 215 | * against are stored JSON-encoded in the case as "branch-values". It can be used to create |
||
| 216 | * optimized branch tables |
||
| 217 | * |
||
| 218 | * @param DOMDocument $ir |
||
| 219 | * @return void |
||
| 220 | */ |
||
| 221 | protected function markBranchTables(DOMDocument $ir) |
||
| 269 | |||
| 270 | /** |
||
| 271 | * Mark conditional <closeTag/> nodes |
||
| 272 | * |
||
| 273 | * @param DOMDocument $ir |
||
| 274 | * @return void |
||
| 275 | */ |
||
| 276 | protected function markConditionalCloseTagElements(DOMDocument $ir) |
||
| 299 | |||
| 300 | /** |
||
| 301 | * Mark void elements and elements with no content |
||
| 302 | * |
||
| 303 | * @param DOMDocument $ir |
||
| 304 | * @return void |
||
| 305 | */ |
||
| 306 | protected function markEmptyElements(DOMDocument $ir) |
||
| 331 | |||
| 332 | /** |
||
| 333 | * Fill in output context |
||
| 334 | * |
||
| 335 | * @param DOMDocument $ir |
||
| 336 | * @return void |
||
| 337 | */ |
||
| 338 | protected function setOutputContext(DOMDocument $ir) |
||
| 345 | } |