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 | * Constructor |
||
| 30 | */ |
||
| 31 | public function __construct() |
||
| 35 | |||
| 36 | /** |
||
| 37 | * Convert an attribute value template into PHP |
||
| 38 | * |
||
| 39 | * NOTE: escaping must be performed by the caller |
||
| 40 | * |
||
| 41 | * @link http://www.w3.org/TR/xslt#dt-attribute-value-template |
||
| 42 | * |
||
| 43 | * @param string $attrValue Attribute value template |
||
| 44 | * @return string |
||
| 45 | */ |
||
| 46 | protected function convertAttributeValueTemplate($attrValue) |
||
| 63 | |||
| 64 | /** |
||
| 65 | * Convert an XPath expression (used in a condition) into PHP code |
||
| 66 | * |
||
| 67 | * This method is similar to convertXPath() but it selectively replaces some simple conditions |
||
| 68 | * with the corresponding DOM method for performance reasons |
||
| 69 | * |
||
| 70 | * @param string $expr XPath expression |
||
| 71 | * @return string PHP code |
||
| 72 | */ |
||
| 73 | public function convertCondition($expr) |
||
| 79 | |||
| 80 | /** |
||
| 81 | * Convert an XPath expression (used as value) into PHP code |
||
| 82 | * |
||
| 83 | * @param string $expr XPath expression |
||
| 84 | * @return string PHP code |
||
| 85 | */ |
||
| 86 | public function convertXPath($expr) |
||
| 92 | |||
| 93 | /** |
||
| 94 | * Escape given literal |
||
| 95 | * |
||
| 96 | * @param string $text Literal |
||
| 97 | * @param string $context Either "raw", "text" or "attribute" |
||
| 98 | * @return string Escaped literal |
||
| 99 | */ |
||
| 100 | protected function escapeLiteral($text, $context) |
||
| 111 | |||
| 112 | /** |
||
| 113 | * Escape the output of given PHP expression |
||
| 114 | * |
||
| 115 | * @param string $php PHP expression |
||
| 116 | * @param string $context Either "raw", "text" or "attribute" |
||
| 117 | * @return string PHP expression, including escaping mechanism |
||
| 118 | */ |
||
| 119 | protected function escapePHPOutput($php, $context) |
||
| 130 | |||
| 131 | /** |
||
| 132 | * Test whether given switch has more than one non-default case |
||
| 133 | * |
||
| 134 | * @param DOMElement $switch <switch/> node |
||
| 135 | * @return bool |
||
| 136 | */ |
||
| 137 | protected function hasMultipleCases(DOMElement $switch) |
||
| 143 | |||
| 144 | /** |
||
| 145 | * Serialize an <applyTemplates/> node |
||
| 146 | * |
||
| 147 | * @param DOMElement $applyTemplates <applyTemplates/> node |
||
| 148 | * @return string |
||
| 149 | */ |
||
| 150 | protected function serializeApplyTemplates(DOMElement $applyTemplates) |
||
| 161 | |||
| 162 | /** |
||
| 163 | * Serialize an <attribute/> node |
||
| 164 | * |
||
| 165 | * @param DOMElement $attribute <attribute/> node |
||
| 166 | * @return string |
||
| 167 | */ |
||
| 168 | protected function serializeAttribute(DOMElement $attribute) |
||
| 182 | |||
| 183 | /** |
||
| 184 | * Serialize the internal representation of a template into PHP |
||
| 185 | * |
||
| 186 | * @param DOMElement $ir Internal representation |
||
| 187 | * @return string |
||
| 188 | */ |
||
| 189 | public function serialize(DOMElement $ir) |
||
| 193 | |||
| 194 | /** |
||
| 195 | * Serialize all the children of given node into PHP |
||
| 196 | * |
||
| 197 | * @param DOMElement $ir Internal representation |
||
| 198 | * @return string |
||
| 199 | */ |
||
| 200 | protected function serializeChildren(DOMElement $ir) |
||
| 211 | |||
| 212 | /** |
||
| 213 | * Serialize a <closeTag/> node |
||
| 214 | * |
||
| 215 | * @param DOMElement $closeTag <closeTag/> node |
||
| 216 | * @return string |
||
| 217 | */ |
||
| 218 | protected function serializeCloseTag(DOMElement $closeTag) |
||
| 256 | |||
| 257 | /** |
||
| 258 | * Serialize a <comment/> node |
||
| 259 | * |
||
| 260 | * @param DOMElement $comment <comment/> node |
||
| 261 | * @return string |
||
| 262 | */ |
||
| 263 | protected function serializeComment(DOMElement $comment) |
||
| 269 | |||
| 270 | /** |
||
| 271 | * Serialize a <copyOfAttributes/> node |
||
| 272 | * |
||
| 273 | * @param DOMElement $copyOfAttributes <copyOfAttributes/> node |
||
| 274 | * @return string |
||
| 275 | */ |
||
| 276 | protected function serializeCopyOfAttributes(DOMElement $copyOfAttributes) |
||
| 287 | |||
| 288 | /** |
||
| 289 | * Serialize an <element/> node |
||
| 290 | * |
||
| 291 | * @param DOMElement $element <element/> node |
||
| 292 | * @return string |
||
| 293 | */ |
||
| 294 | protected function serializeElement(DOMElement $element) |
||
| 349 | |||
| 350 | /** |
||
| 351 | * Serialize a <switch/> node that has a branch-key attribute |
||
| 352 | * |
||
| 353 | * @param DOMElement $switch <switch/> node |
||
| 354 | * @return string |
||
| 355 | */ |
||
| 356 | protected function serializeHash(DOMElement $switch) |
||
| 387 | |||
| 388 | /** |
||
| 389 | * Serialize an <output/> node |
||
| 390 | * |
||
| 391 | * @param DOMElement $output <output/> node |
||
| 392 | * @return string |
||
| 393 | */ |
||
| 394 | protected function serializeOutput(DOMElement $output) |
||
| 411 | |||
| 412 | /** |
||
| 413 | * Serialize a <switch/> node |
||
| 414 | * |
||
| 415 | * @param DOMElement $switch <switch/> node |
||
| 416 | * @return string |
||
| 417 | */ |
||
| 418 | protected function serializeSwitch(DOMElement $switch) |
||
| 453 | } |