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 XMLStream 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 XMLStream, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 4 | class XMLStream |
||
| 5 | { |
||
| 6 | use \PHPDaemon\Traits\EventHandlers; |
||
| 7 | use \PHPDaemon\Traits\ClassWatchdog; |
||
| 8 | use \PHPDaemon\Traits\StaticObjectWatchdog; |
||
| 9 | |||
| 10 | protected $parser; |
||
| 11 | protected $xml_depth = 0; |
||
| 12 | protected $current_ns = []; |
||
| 13 | protected $idhandlers = []; |
||
| 14 | protected $xpathhandlers = []; |
||
| 15 | protected $default_ns; |
||
| 16 | |||
| 17 | /** |
||
| 18 | * Constructor |
||
| 19 | * @return void |
||
|
|
|||
| 20 | */ |
||
| 21 | public function __construct() |
||
| 30 | |||
| 31 | /** |
||
| 32 | * Set default namespace |
||
| 33 | * @param string $ns |
||
| 34 | * @return void |
||
| 35 | */ |
||
| 36 | public function setDefaultNS($ns) |
||
| 40 | |||
| 41 | /** |
||
| 42 | * Finishes the stream |
||
| 43 | * @return void |
||
| 44 | */ |
||
| 45 | public function finish() |
||
| 53 | |||
| 54 | /** |
||
| 55 | * Destructor |
||
| 56 | * @return void |
||
| 57 | */ |
||
| 58 | public function __destroy() |
||
| 65 | |||
| 66 | /** |
||
| 67 | * Feed stream |
||
| 68 | * @param string $buf |
||
| 69 | * @return void |
||
| 70 | */ |
||
| 71 | public function feed($buf) |
||
| 75 | |||
| 76 | /** |
||
| 77 | * Finalize stream |
||
| 78 | * @return void |
||
| 79 | */ |
||
| 80 | public function finalize() |
||
| 84 | |||
| 85 | /** |
||
| 86 | * XML start callback |
||
| 87 | * |
||
| 88 | * @see xml_set_element_handler |
||
| 89 | * @param resource $parser |
||
| 90 | * @param string $name |
||
| 91 | * @param array $attr |
||
| 92 | * @return void |
||
| 93 | */ |
||
| 94 | public function startXML($parser, $name, $attr) |
||
| 124 | |||
| 125 | /** |
||
| 126 | * XML end callback |
||
| 127 | * |
||
| 128 | * @see xml_set_element_handler |
||
| 129 | * |
||
| 130 | * @param resource $parser |
||
| 131 | * @param string $name |
||
| 132 | * @return void |
||
| 133 | */ |
||
| 134 | public function endXML($parser, $name) |
||
| 180 | |||
| 181 | /** |
||
| 182 | * XML character callback |
||
| 183 | * @see xml_set_character_data_handler |
||
| 184 | * |
||
| 185 | * @param resource $parser |
||
| 186 | * @param string $data |
||
| 187 | */ |
||
| 188 | public function charXML($parser, $data) |
||
| 194 | |||
| 195 | /** |
||
| 196 | * Get next ID |
||
| 197 | * |
||
| 198 | * @return integer |
||
| 199 | */ |
||
| 200 | public function getId() |
||
| 205 | |||
| 206 | /** |
||
| 207 | * Add ID Handler |
||
| 208 | * |
||
| 209 | * @param integer $id |
||
| 210 | * @param callable $cb |
||
| 211 | */ |
||
| 212 | public function addIdHandler($id, $cb) |
||
| 219 | |||
| 220 | /** |
||
| 221 | * Add XPath Handler |
||
| 222 | * |
||
| 223 | * @param string $xpath |
||
| 224 | * @param \Closure $cb |
||
| 225 | * @param null $obj |
||
| 226 | */ |
||
| 227 | public function addXPathHandler($xpath, $cb, $obj = null) |
||
| 245 | } |
||
| 246 |
Adding a
@returnannotation to a constructor is not recommended, since a constructor does not have a meaningful return value.Please refer to the PHP core documentation on constructors.