Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like SaxParser 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 SaxParser, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 17 | class SaxParser |
||
| 18 | { |
||
| 19 | public $level; |
||
| 20 | public $parser; |
||
| 21 | |||
| 22 | public $isCaseFolding; |
||
| 23 | public $targetEncoding; |
||
| 24 | |||
| 25 | /* Custom Handler Variables */ |
||
| 26 | public $tagHandlers = array(); |
||
| 27 | |||
| 28 | /* Tag stack */ |
||
| 29 | public $tags = array(); |
||
| 30 | |||
| 31 | /* Xml Source Input */ |
||
| 32 | public $xmlInput; |
||
| 33 | |||
| 34 | public $errors = array(); |
||
| 35 | |||
| 36 | /** |
||
| 37 | * Creates a SaxParser object using a FileInput to represent the stream |
||
| 38 | * of XML data to parse. Use the static methods createFileInput or |
||
| 39 | * createStringInput to construct xml input source objects to supply |
||
| 40 | * to the constructor, or the implementor can construct them individually. |
||
| 41 | * |
||
| 42 | * @param $input |
||
| 43 | */ |
||
| 44 | 60 | public function __construct(&$input) |
|
| 60 | |||
| 61 | /*--------------------------------------------------------------------------- |
||
| 62 | Property Methods |
||
| 63 | ---------------------------------------------------------------------------*/ |
||
| 64 | |||
| 65 | /** |
||
| 66 | * @return int |
||
| 67 | */ |
||
| 68 | 4 | public function getCurrentLevel() |
|
| 72 | |||
| 73 | /** |
||
| 74 | * @param boolean $isCaseFolding |
||
| 75 | * @return void |
||
| 76 | */ |
||
| 77 | 60 | public function setCaseFolding($isCaseFolding) |
|
| 84 | |||
| 85 | /** |
||
| 86 | * @return void |
||
| 87 | */ |
||
| 88 | public function useIsoEncoding() |
||
| 93 | |||
| 94 | /** |
||
| 95 | * @return void |
||
| 96 | */ |
||
| 97 | public function useAsciiEncoding() |
||
| 102 | |||
| 103 | /** |
||
| 104 | * @return void |
||
| 105 | */ |
||
| 106 | 60 | public function useUtfEncoding() |
|
| 111 | |||
| 112 | /** |
||
| 113 | * Returns the name of the xml tag being parsed |
||
| 114 | * @return string |
||
| 115 | */ |
||
| 116 | public function getCurrentTag() |
||
| 120 | |||
| 121 | 11 | public function getParentTag() |
|
| 128 | |||
| 129 | |||
| 130 | /*--------------------------------------------------------------------------- |
||
| 131 | Parser methods |
||
| 132 | ---------------------------------------------------------------------------*/ |
||
| 133 | |||
| 134 | /** |
||
| 135 | * @return bool |
||
| 136 | */ |
||
| 137 | public function parse() |
||
| 160 | |||
| 161 | /** |
||
| 162 | * @return void |
||
| 163 | */ |
||
| 164 | public function free() |
||
| 168 | |||
| 169 | /** |
||
| 170 | * @private |
||
| 171 | * @return string |
||
| 172 | */ |
||
| 173 | public function getXmlError() |
||
| 177 | |||
| 178 | /*--------------------------------------------------------------------------- |
||
| 179 | Custom Handler Methods |
||
| 180 | ---------------------------------------------------------------------------*/ |
||
| 181 | |||
| 182 | /** |
||
| 183 | * Adds a callback function to be called when a tag is encountered.<br> |
||
| 184 | * @param XmlTagHandler $tagHandler |
||
| 185 | * @return void |
||
| 186 | */ |
||
| 187 | 60 | public function addTagHandler(XmlTagHandler $tagHandler) |
|
| 198 | |||
| 199 | |||
| 200 | /*--------------------------------------------------------------------------- |
||
| 201 | Private Handler Methods |
||
| 202 | ---------------------------------------------------------------------------*/ |
||
| 203 | |||
| 204 | /** |
||
| 205 | * Callback function that executes whenever a the start of a tag |
||
| 206 | * occurs when being parsed. |
||
| 207 | * @param int $parser The handle to the parser. |
||
| 208 | * @param string $tagName The name of the tag currently being parsed. |
||
| 209 | * @param array $attributesArray The list of attributes associated with the tag. |
||
| 210 | * @private |
||
| 211 | * @return void |
||
| 212 | */ |
||
| 213 | public function handleBeginElement($parser, $tagName, $attributesArray) |
||
| 223 | |||
| 224 | /** |
||
| 225 | * Callback function that executes whenever the end of a tag |
||
| 226 | * occurs when being parsed. |
||
| 227 | * @param int $parser The handle to the parser. |
||
| 228 | * @param string $tagName The name of the tag currently being parsed. |
||
| 229 | * @private |
||
| 230 | * @return void |
||
| 231 | */ |
||
| 232 | public function handleEndElement($parser, $tagName) |
||
| 242 | |||
| 243 | /** |
||
| 244 | * Callback function that executes whenever character data is encountered |
||
| 245 | * while being parsed. |
||
| 246 | * @param int $parser The handle to the parser. |
||
| 247 | * @param string $data Character data inside the tag |
||
| 248 | * @return void |
||
| 249 | */ |
||
| 250 | public function handleCharacterData($parser, $data) |
||
| 259 | |||
| 260 | /** |
||
| 261 | * @param int $parser The handle to the parser. |
||
| 262 | * @param $target |
||
| 263 | * @param $data |
||
| 264 | * @return void |
||
| 265 | */ |
||
| 266 | public function handleProcessingInstruction($parser, &$target, &$data) |
||
| 272 | |||
| 273 | /** |
||
| 274 | * @param $parser |
||
| 275 | * @param $data |
||
| 276 | * @return void |
||
| 277 | */ |
||
| 278 | public function handleDefault($parser, $data) |
||
| 282 | |||
| 283 | /** |
||
| 284 | * @param $parser |
||
| 285 | * @param $entityName |
||
| 286 | * @param $base |
||
| 287 | * @param $systemId |
||
| 288 | * @param $publicId |
||
| 289 | * @param $notationName |
||
| 290 | * @return void |
||
| 291 | */ |
||
| 292 | public function handleUnparsedEntityDecl($parser, $entityName, $base, $systemId, $publicId, $notationName) |
||
| 296 | |||
| 297 | /** |
||
| 298 | * @param $parser |
||
| 299 | * @param $notationName |
||
| 300 | * @param $base |
||
| 301 | * @param $systemId |
||
| 302 | * @param $publicId |
||
| 303 | * @return void |
||
| 304 | */ |
||
| 305 | public function handleNotationDecl($parser, $notationName, $base, $systemId, $publicId) |
||
| 309 | |||
| 310 | /** |
||
| 311 | * @param $parser |
||
| 312 | * @param $openEntityNames |
||
| 313 | * @param $base |
||
| 314 | * @param $systemId |
||
| 315 | * @param $publicId |
||
| 316 | * @return void |
||
| 317 | */ |
||
| 318 | public function handleExternalEntityRef($parser, $openEntityNames, $base, $systemId, $publicId) |
||
| 322 | |||
| 323 | /** |
||
| 324 | * The default tag handler method for a tag with no handler |
||
| 325 | * |
||
| 326 | * @param $parser |
||
| 327 | * @param $tagName |
||
| 328 | * @param $attributesArray |
||
| 329 | * @return void |
||
| 330 | */ |
||
| 331 | public function handleBeginElementDefault($parser, $tagName, $attributesArray) |
||
| 334 | |||
| 335 | /** |
||
| 336 | * The default tag handler method for a tag with no handler |
||
| 337 | * |
||
| 338 | * @param $parser |
||
| 339 | * @param $tagName |
||
| 340 | * @return void |
||
| 341 | */ |
||
| 342 | public function handleEndElementDefault($parser, $tagName) |
||
| 345 | |||
| 346 | /** |
||
| 347 | * The default tag handler method for a tag with no handler |
||
| 348 | * |
||
| 349 | * @abstract |
||
| 350 | * |
||
| 351 | * @param $parser |
||
| 352 | * @param $data |
||
| 353 | * @return void |
||
| 354 | */ |
||
| 355 | public function handleCharacterDataDefault($parser, $data) |
||
| 358 | |||
| 359 | /** |
||
| 360 | * Sets error messages |
||
| 361 | * |
||
| 362 | * @param string $error string an error message |
||
| 363 | */ |
||
| 364 | public function setErrors($error) |
||
| 368 | |||
| 369 | /** |
||
| 370 | * Gets all the error messages |
||
| 371 | * |
||
| 372 | * @param bool $ashtml return as html? |
||
| 373 | * @return mixed |
||
| 374 | */ |
||
| 375 | public function getErrors($ashtml = true) |
||
| 389 | } |
||
| 390 |
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: