Complex classes like Node 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 Node, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 10 | class Node |
||
| 11 | { |
||
| 12 | /** |
||
| 13 | * @var String $id : The unique id of the node. |
||
| 14 | */ |
||
| 15 | private $id; |
||
| 16 | /** |
||
| 17 | * @var String $color : The text color of the node. |
||
| 18 | */ |
||
| 19 | private $color; |
||
| 20 | /** |
||
| 21 | * @var String $created : The timestamp of the creation date of the node. |
||
| 22 | */ |
||
| 23 | private $created; |
||
| 24 | /** |
||
| 25 | * @var String $modified : The timestamp of the modified date of the node. |
||
| 26 | */ |
||
| 27 | private $modified; |
||
| 28 | /** |
||
| 29 | * @var String $position : The relative position of the node. |
||
| 30 | */ |
||
| 31 | private $position; |
||
| 32 | /** |
||
| 33 | * @var int $vshift : The vshift of the node. |
||
| 34 | */ |
||
| 35 | private $vshift; |
||
| 36 | /** |
||
| 37 | * @var Bool $folded : Is the node folded or not. |
||
| 38 | */ |
||
| 39 | private $folded; |
||
| 40 | /** |
||
| 41 | * @var String $text : The text of the node. |
||
| 42 | */ |
||
| 43 | private $text; |
||
| 44 | /** |
||
| 45 | * @var Bool $bold : Is the text node in bold. |
||
| 46 | */ |
||
| 47 | private $bold; |
||
| 48 | /** |
||
| 49 | * @var Bool $italic : Is the text node in italic. |
||
| 50 | */ |
||
| 51 | private $italic; |
||
| 52 | /** |
||
| 53 | * @var String $fontName : The font name of the node. |
||
| 54 | */ |
||
| 55 | private $fontName; |
||
| 56 | /** |
||
| 57 | * @var int $fontSize : The font size of the node. |
||
| 58 | */ |
||
| 59 | private $fontSize; |
||
| 60 | /** |
||
| 61 | * @var Icon $icon : The icon of the node if there is one. |
||
| 62 | */ |
||
| 63 | private $icon; |
||
| 64 | /** |
||
| 65 | * @var NodeList $children : The children of the node as a collection of Nodes. |
||
| 66 | */ |
||
| 67 | private $children; |
||
| 68 | /** |
||
| 69 | * @var DOMElement $domNode : The DOMElement instance of the current Node. |
||
| 70 | */ |
||
| 71 | private $domNode; |
||
| 72 | |||
| 73 | /** |
||
| 74 | * @param DOMElement $domNode : The DOMElement instance of the current Node. |
||
| 75 | * @param NodeList $children : The children of the node as a collection of Nodes. |
||
| 76 | * @param Icon $icon : The icon of the node if there is one. |
||
| 77 | * @param String $id : The unique id of the node. |
||
| 78 | * @param String $color : The text color of the node. |
||
| 79 | * @param String $created : The timestamp of the creation date of the node. |
||
| 80 | * @param String $modified : The timestamp of the modified date of the node. |
||
| 81 | * @param String $position : The relative position of the node. |
||
| 82 | * @param int $vshift : The vshift of the node. |
||
| 83 | * @param Bool $folded : Is the node folded or not. |
||
| 84 | * @param String $text : The text of the node. |
||
| 85 | * @param Bool $bold : Is the text node in bold. |
||
| 86 | * @param Bool $italic : Is the text node in italic. |
||
| 87 | * @param String $fontName : The font name of the current Node. |
||
| 88 | * @param int $fontSize : The font size of the node. |
||
| 89 | */ |
||
| 90 | public function __construct(DOMElement $domNode = null, NodeList $children = null, Icon $icon = null, $id = null, $color = null, $created = null, $modified = null, $position = null, $vshift = null, $folded = null, $text = null, $bold = null, $italic = null, $fontName = null, $fontSize = null) { |
||
| 107 | |||
| 108 | /** |
||
| 109 | * Return the Id of the current node. |
||
| 110 | * |
||
| 111 | * @return String : The node id. |
||
| 112 | */ |
||
| 113 | public function getId() { |
||
| 116 | |||
| 117 | /** |
||
| 118 | * Set the Id of the current node. |
||
| 119 | * |
||
| 120 | * @param String $id : The node id. |
||
| 121 | */ |
||
| 122 | public function setId($id) { |
||
| 125 | |||
| 126 | /** |
||
| 127 | * Return the color of the current node. |
||
| 128 | * |
||
| 129 | * @return String : The node color value in hexadecimal. |
||
| 130 | */ |
||
| 131 | public function getColor() { |
||
| 134 | |||
| 135 | /** |
||
| 136 | * Set the color of the current node. |
||
| 137 | * |
||
| 138 | * @param String $color : The node color value in hexadecimal. |
||
| 139 | */ |
||
| 140 | public function setColor($color) { |
||
| 143 | |||
| 144 | /** |
||
| 145 | * Return the created timestamp of the current node. |
||
| 146 | * |
||
| 147 | * @return String : The node created timestamp. |
||
| 148 | */ |
||
| 149 | public function getCreated() { |
||
| 152 | |||
| 153 | /** |
||
| 154 | * Set the created timestamp of the current node. |
||
| 155 | * |
||
| 156 | * @param String $created : The node created timestamp. |
||
| 157 | */ |
||
| 158 | public function setCreated($created) { |
||
| 161 | |||
| 162 | /** |
||
| 163 | * Return the modified timestamp of the current node. |
||
| 164 | * |
||
| 165 | * @return String : The node modified timestamp. |
||
| 166 | */ |
||
| 167 | public function getModified() { |
||
| 170 | |||
| 171 | /** |
||
| 172 | * Set the modified timestamp of the current node. |
||
| 173 | * |
||
| 174 | * @param String $modified : The node modified timestamp. |
||
| 175 | */ |
||
| 176 | public function setModified($modified) { |
||
| 179 | |||
| 180 | /** |
||
| 181 | * Return the relative position the current node to the parent node. |
||
| 182 | * |
||
| 183 | * @return String : The node relative position. |
||
| 184 | */ |
||
| 185 | public function getPosition() { |
||
| 188 | |||
| 189 | /** |
||
| 190 | * Set the relative position the current node to the parent node. |
||
| 191 | * |
||
| 192 | * @param String $position : The node relative position. |
||
| 193 | */ |
||
| 194 | public function setPosition($position) { |
||
| 197 | |||
| 198 | /** |
||
| 199 | * Return the vshift value of the current node. |
||
| 200 | * |
||
| 201 | * @return int : The node vshift value. |
||
| 202 | */ |
||
| 203 | public function getVshift() { |
||
| 206 | |||
| 207 | /** |
||
| 208 | * Set the vshift value of the current node. |
||
| 209 | * |
||
| 210 | * @param int $vshift : The node vshift value. |
||
| 211 | */ |
||
| 212 | public function setVshift($vshift) { |
||
| 215 | |||
| 216 | /** |
||
| 217 | * Return if the current node is folded or not. |
||
| 218 | * |
||
| 219 | * @return Bool $folded |
||
| 220 | */ |
||
| 221 | public function isFolded() { |
||
| 224 | |||
| 225 | /** |
||
| 226 | * Set if the current node is folded or not. |
||
| 227 | * |
||
| 228 | * @param Bool $folded |
||
| 229 | */ |
||
| 230 | public function setFolded($folded) { |
||
| 233 | |||
| 234 | /** |
||
| 235 | * Return the text of the current node with html entities decoded. |
||
| 236 | * |
||
| 237 | * @return String : The node text value. |
||
| 238 | */ |
||
| 239 | public function getText() { |
||
| 242 | |||
| 243 | /** |
||
| 244 | * Return the text of the current node. |
||
| 245 | * |
||
| 246 | * @return String : The node text value. |
||
| 247 | */ |
||
| 248 | public function getRawText() { |
||
| 251 | |||
| 252 | /** |
||
| 253 | * Set the text of the current node with html entities decoded. |
||
| 254 | * |
||
| 255 | * @param String $text : The node text value in plain text not html encoded. |
||
| 256 | */ |
||
| 257 | public function setText($text) { |
||
| 260 | |||
| 261 | /** |
||
| 262 | * Return the font name of the current node. |
||
| 263 | * |
||
| 264 | * @return String : The node font name. |
||
| 265 | */ |
||
| 266 | public function getFontName() { |
||
| 269 | |||
| 270 | /** |
||
| 271 | * Set the font name of the current node. |
||
| 272 | * |
||
| 273 | * @param String $fontName : The node font name. |
||
| 274 | */ |
||
| 275 | public function setFontName($fontName) { |
||
| 278 | |||
| 279 | /** |
||
| 280 | * Return if the text node is bold. |
||
| 281 | * |
||
| 282 | * @return Bool : If the text is bold or not. |
||
| 283 | */ |
||
| 284 | public function isBold() { |
||
| 287 | |||
| 288 | /** |
||
| 289 | * Set if the text node is bold. |
||
| 290 | * |
||
| 291 | * @param Bool $bold : If the text must be bold or not. |
||
| 292 | */ |
||
| 293 | public function setBold($bold) { |
||
| 296 | |||
| 297 | /** |
||
| 298 | * Return if the text node is italic. |
||
| 299 | * |
||
| 300 | * @return Bool : If the text is italic or not. |
||
| 301 | */ |
||
| 302 | public function isItalic() { |
||
| 305 | |||
| 306 | /** |
||
| 307 | * Set if the text node is italic. |
||
| 308 | * |
||
| 309 | * @param Bool $italic : If the text must be italic or not. |
||
| 310 | */ |
||
| 311 | public function setItalic($italic) { |
||
| 314 | |||
| 315 | /** |
||
| 316 | * Return the font size of the current node. |
||
| 317 | * |
||
| 318 | * @return int : The node font size. |
||
| 319 | */ |
||
| 320 | public function getFontSize() { |
||
| 323 | |||
| 324 | /** |
||
| 325 | * Set the font size of the current node. |
||
| 326 | * |
||
| 327 | * @param int $fontSize : The node font size. |
||
| 328 | */ |
||
| 329 | public function setFontSize($fontSize) { |
||
| 332 | |||
| 333 | /** |
||
| 334 | * Return the icon of the current node if there is one. |
||
| 335 | * |
||
| 336 | * @return Icon : The icon. |
||
| 337 | */ |
||
| 338 | public function getIcon() { |
||
| 341 | |||
| 342 | /** |
||
| 343 | * Set the icon of the current node. |
||
| 344 | * |
||
| 345 | * @param Icon $icon : The node icon. |
||
| 346 | */ |
||
| 347 | public function setIcon(Icon $icon) { |
||
| 350 | |||
| 351 | /** |
||
| 352 | * Return the list of children of the current node. |
||
| 353 | * |
||
| 354 | */ |
||
| 355 | public function getChildren() { |
||
| 358 | |||
| 359 | /** |
||
| 360 | * Set the list of children of the current node. |
||
| 361 | * |
||
| 362 | * @param NodeList $children : The list of children node. |
||
| 363 | */ |
||
| 364 | public function setChildren(NodeList $children) { |
||
| 367 | |||
| 368 | /** |
||
| 369 | * Return the DOMElement instance of the cirrent Node. |
||
| 370 | * |
||
| 371 | * @return DOMElement : DOMElement instance. |
||
| 372 | */ |
||
| 373 | public function getDomNode() { |
||
| 376 | |||
| 377 | /** |
||
| 378 | * Set the current DOMElement instance. |
||
| 379 | * |
||
| 380 | * @param DOMElement $domNode : The DOMElement instance. |
||
| 381 | */ |
||
| 382 | public function setDomNode(DOMElement $domNode) { |
||
| 385 | |||
| 386 | /** |
||
| 387 | * To String method. |
||
| 388 | * |
||
| 389 | * @return String : The text content of the node. |
||
| 390 | */ |
||
| 391 | public function __toString() { |
||
| 394 | |||
| 395 | /** |
||
| 396 | * Transform the objects tree in an array tree. |
||
| 397 | * |
||
| 398 | * @return Array $array : The array tree. |
||
| 399 | */ |
||
| 400 | public function toArray() { |
||
| 425 | } |