Complex classes like Serializer 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 Serializer, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 16 | class Serializer |
||
| 17 | { |
||
| 18 | /** |
||
| 19 | * @var XPathConvertor XPath-to-PHP convertor |
||
| 20 | */ |
||
| 21 | public $convertor; |
||
| 22 | |||
| 23 | /** |
||
| 24 | * @var bool Whether to use the mbstring functions as a replacement for XPath expressions |
||
| 25 | */ |
||
| 26 | public $useMultibyteStringFunctions = false; |
||
| 27 | |||
| 28 | /** |
||
| 29 | * @var DOMXPath |
||
| 30 | */ |
||
| 31 | protected $xpath; |
||
| 32 | |||
| 33 | /** |
||
| 34 | * Constructor |
||
| 35 | */ |
||
| 36 | public function __construct() |
||
| 40 | |||
| 41 | /** |
||
| 42 | * Convert an XPath expression (used in a condition) into PHP code |
||
| 43 | * |
||
| 44 | * This method is similar to convertXPath() but it selectively replaces some simple conditions |
||
| 45 | * with the corresponding DOM method for performance reasons |
||
| 46 | * |
||
| 47 | * @param string $expr XPath expression |
||
| 48 | * @return string PHP code |
||
| 49 | */ |
||
| 50 | public function convertCondition($expr) |
||
| 56 | |||
| 57 | /** |
||
| 58 | * Convert an XPath expression (used as value) into PHP code |
||
| 59 | * |
||
| 60 | * @param string $expr XPath expression |
||
| 61 | * @return string PHP code |
||
| 62 | */ |
||
| 63 | public function convertXPath($expr) |
||
| 69 | |||
| 70 | /** |
||
| 71 | * Serialize the internal representation of a template into PHP |
||
| 72 | * |
||
| 73 | * @param DOMElement $ir Internal representation |
||
| 74 | * @return string |
||
| 75 | */ |
||
| 76 | public function serialize(DOMElement $ir) |
||
| 82 | |||
| 83 | /** |
||
| 84 | * Convert an attribute value template into PHP |
||
| 85 | * |
||
| 86 | * NOTE: escaping must be performed by the caller |
||
| 87 | * |
||
| 88 | * @link https://www.w3.org/TR/1999/REC-xslt-19991116#dt-attribute-value-template |
||
| 89 | * |
||
| 90 | * @param string $attrValue Attribute value template |
||
| 91 | * @return string |
||
| 92 | */ |
||
| 93 | protected function convertAttributeValueTemplate($attrValue) |
||
| 110 | |||
| 111 | /** |
||
| 112 | * Escape given literal |
||
| 113 | * |
||
| 114 | * @param string $text Literal |
||
| 115 | * @param string $context Either "raw", "text" or "attribute" |
||
| 116 | * @return string Escaped literal |
||
| 117 | */ |
||
| 118 | protected function escapeLiteral($text, $context) |
||
| 129 | |||
| 130 | /** |
||
| 131 | * Escape the output of given PHP expression |
||
| 132 | * |
||
| 133 | * @param string $php PHP expression |
||
| 134 | * @param string $context Either "raw", "text" or "attribute" |
||
| 135 | * @return string PHP expression, including escaping mechanism |
||
| 136 | */ |
||
| 137 | protected function escapePHPOutput($php, $context) |
||
| 148 | |||
| 149 | /** |
||
| 150 | * Test whether given switch has more than one non-default case |
||
| 151 | * |
||
| 152 | * @param DOMElement $switch <switch/> node |
||
| 153 | * @return bool |
||
| 154 | */ |
||
| 155 | protected function hasMultipleCases(DOMElement $switch) |
||
| 159 | |||
| 160 | /** |
||
| 161 | * Serialize an <applyTemplates/> node |
||
| 162 | * |
||
| 163 | * @param DOMElement $applyTemplates <applyTemplates/> node |
||
| 164 | * @return string |
||
| 165 | */ |
||
| 166 | protected function serializeApplyTemplates(DOMElement $applyTemplates) |
||
| 177 | |||
| 178 | /** |
||
| 179 | * Serialize an <attribute/> node |
||
| 180 | * |
||
| 181 | * @param DOMElement $attribute <attribute/> node |
||
| 182 | * @return string |
||
| 183 | */ |
||
| 184 | protected function serializeAttribute(DOMElement $attribute) |
||
| 198 | |||
| 199 | /** |
||
| 200 | * Serialize all the children of given node into PHP |
||
| 201 | * |
||
| 202 | * @param DOMElement $ir Internal representation |
||
| 203 | * @return string |
||
| 204 | */ |
||
| 205 | protected function serializeChildren(DOMElement $ir) |
||
| 219 | |||
| 220 | /** |
||
| 221 | * Serialize a <closeTag/> node |
||
| 222 | * |
||
| 223 | * @param DOMElement $closeTag <closeTag/> node |
||
| 224 | * @return string |
||
| 225 | */ |
||
| 226 | protected function serializeCloseTag(DOMElement $closeTag) |
||
| 262 | |||
| 263 | /** |
||
| 264 | * Serialize a <comment/> node |
||
| 265 | * |
||
| 266 | * @param DOMElement $comment <comment/> node |
||
| 267 | * @return string |
||
| 268 | */ |
||
| 269 | protected function serializeComment(DOMElement $comment) |
||
| 275 | |||
| 276 | /** |
||
| 277 | * Serialize a <copyOfAttributes/> node |
||
| 278 | * |
||
| 279 | * @param DOMElement $copyOfAttributes <copyOfAttributes/> node |
||
| 280 | * @return string |
||
| 281 | */ |
||
| 282 | protected function serializeCopyOfAttributes(DOMElement $copyOfAttributes) |
||
| 293 | |||
| 294 | /** |
||
| 295 | * Serialize an <element/> node |
||
| 296 | * |
||
| 297 | * @param DOMElement $element <element/> node |
||
| 298 | * @return string |
||
| 299 | */ |
||
| 300 | protected function serializeElement(DOMElement $element) |
||
| 355 | |||
| 356 | /** |
||
| 357 | * Serialize a <switch/> node that has a branch-key attribute |
||
| 358 | * |
||
| 359 | * @param DOMElement $switch <switch/> node |
||
| 360 | * @return string |
||
| 361 | */ |
||
| 362 | protected function serializeHash(DOMElement $switch) |
||
| 383 | |||
| 384 | /** |
||
| 385 | * Serialize an <output/> node |
||
| 386 | * |
||
| 387 | * @param DOMElement $output <output/> node |
||
| 388 | * @return string |
||
| 389 | */ |
||
| 390 | protected function serializeOutput(DOMElement $output) |
||
| 407 | |||
| 408 | /** |
||
| 409 | * Serialize a <switch/> node |
||
| 410 | * |
||
| 411 | * @param DOMElement $switch <switch/> node |
||
| 412 | * @return string |
||
| 413 | */ |
||
| 414 | protected function serializeSwitch(DOMElement $switch) |
||
| 441 | } |