Complex classes like AbstractNavigationNode 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 AbstractNavigationNode, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 23 | abstract class AbstractNavigationNode implements NavigationInterface, AlphabeticalTreeNodeInterface { |
||
| 24 | |||
| 25 | /** |
||
| 26 | * Active ? |
||
| 27 | * |
||
| 28 | * @var bool |
||
| 29 | */ |
||
| 30 | private $active; |
||
| 31 | |||
| 32 | /** |
||
| 33 | * Enable ? |
||
| 34 | * |
||
| 35 | * @var bool |
||
| 36 | */ |
||
| 37 | private $enable; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * Icon. |
||
| 41 | * |
||
| 42 | * @var string |
||
| 43 | */ |
||
| 44 | private $icon; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * Id. |
||
| 48 | * |
||
| 49 | * @var string |
||
| 50 | */ |
||
| 51 | private $id; |
||
| 52 | |||
| 53 | /** |
||
| 54 | * Index. |
||
| 55 | * |
||
| 56 | * @var array |
||
| 57 | */ |
||
| 58 | private $index; |
||
| 59 | |||
| 60 | /** |
||
| 61 | * Label. |
||
| 62 | * |
||
| 63 | * @var string |
||
| 64 | */ |
||
| 65 | private $label; |
||
| 66 | |||
| 67 | /** |
||
| 68 | * Matcher. |
||
| 69 | * |
||
| 70 | * @var string |
||
| 71 | */ |
||
| 72 | private $matcher; |
||
| 73 | |||
| 74 | /** |
||
| 75 | * Navigation nodes. |
||
| 76 | * |
||
| 77 | * @var AbstractNavigationNode[] |
||
| 78 | */ |
||
| 79 | private $nodes; |
||
| 80 | |||
| 81 | /** |
||
| 82 | * Parent. |
||
| 83 | * |
||
| 84 | * @var AbstractNavigationNode |
||
| 85 | */ |
||
| 86 | private $parent; |
||
| 87 | |||
| 88 | /** |
||
| 89 | * Target. |
||
| 90 | * |
||
| 91 | * @var string |
||
| 92 | */ |
||
| 93 | private $target; |
||
| 94 | |||
| 95 | /** |
||
| 96 | * URI. |
||
| 97 | * |
||
| 98 | * @var string |
||
| 99 | */ |
||
| 100 | private $uri; |
||
| 101 | |||
| 102 | /** |
||
| 103 | * Visible ? |
||
| 104 | * |
||
| 105 | * @var bool |
||
| 106 | */ |
||
| 107 | private $visible; |
||
| 108 | |||
| 109 | /** |
||
| 110 | * Constructor. |
||
| 111 | * |
||
| 112 | * @param string $label The label. |
||
| 113 | * @param string|null $icon The icon. |
||
| 114 | * @param string|null $uri The URI. |
||
| 115 | * @param string $matcher The matcher. |
||
| 116 | */ |
||
| 117 | protected function __construct($label, $icon = null, $uri = null, $matcher = self::NAVIGATION_MATCHER_URL) { |
||
| 131 | |||
| 132 | /** |
||
| 133 | * Add a navigation node. |
||
| 134 | * |
||
| 135 | * @param AbstractNavigationNode $node The navigation node. |
||
| 136 | * @return AbstractNavigationNode Returns this navigation node. |
||
| 137 | */ |
||
| 138 | public function addNode(AbstractNavigationNode $node) { |
||
| 143 | |||
| 144 | /** |
||
| 145 | * Clear the navigation nodes. |
||
| 146 | * |
||
| 147 | * @return AbstractNavigationNode Returns this navigation node. |
||
| 148 | */ |
||
| 149 | public function clearNodes() { |
||
| 155 | |||
| 156 | /** |
||
| 157 | * Get the active. |
||
| 158 | * |
||
| 159 | * @return bool Returns the active. |
||
| 160 | */ |
||
| 161 | public function getActive() { |
||
| 164 | |||
| 165 | /** |
||
| 166 | * {@inheritdoc} |
||
| 167 | */ |
||
| 168 | public function getAlphabeticalTreeNodeLabel() { |
||
| 171 | |||
| 172 | /** |
||
| 173 | * {@inheritdoc} |
||
| 174 | */ |
||
| 175 | public function getAlphabeticalTreeNodeParent() { |
||
| 178 | |||
| 179 | /** |
||
| 180 | * Get the enable. |
||
| 181 | * |
||
| 182 | * @return bool Returns the enable. |
||
| 183 | */ |
||
| 184 | public function getEnable() { |
||
| 187 | |||
| 188 | /** |
||
| 189 | * Get the first navigation node. |
||
| 190 | * |
||
| 191 | * @return AbstractNavigationNode|null Returns the first navigation node in case of success, null otherwise. |
||
| 192 | */ |
||
| 193 | public function getFirstNode() { |
||
| 196 | |||
| 197 | /** |
||
| 198 | * Get the icon. |
||
| 199 | * |
||
| 200 | * @return string Returns the icon. |
||
| 201 | */ |
||
| 202 | public function getIcon() { |
||
| 205 | |||
| 206 | /** |
||
| 207 | * Get the id. |
||
| 208 | * |
||
| 209 | * @return string Returns the id. |
||
| 210 | */ |
||
| 211 | public function getId() { |
||
| 214 | |||
| 215 | /** |
||
| 216 | * Get the label. |
||
| 217 | * |
||
| 218 | * @return string Returns the label. |
||
| 219 | */ |
||
| 220 | public function getLabel(): string { |
||
| 223 | |||
| 224 | /** |
||
| 225 | * Get the last navigation node. |
||
| 226 | * |
||
| 227 | * @return AbstractNavigationNode|null Returns the last navigation node in case of success, null otherwise. |
||
| 228 | */ |
||
| 229 | public function getLastNode() { |
||
| 232 | |||
| 233 | /** |
||
| 234 | * Get the matcher. |
||
| 235 | * |
||
| 236 | * @return string Returns the matcher. |
||
| 237 | */ |
||
| 238 | public function getMatcher() { |
||
| 241 | |||
| 242 | /** |
||
| 243 | * Get a navigation node at. |
||
| 244 | * |
||
| 245 | * @param int $position The position. |
||
| 246 | * @return AbstractNavigationNode|null Returns the navigation node in case of success, null otherwise. |
||
| 247 | */ |
||
| 248 | public function getNodeAt($position) { |
||
| 256 | |||
| 257 | /** |
||
| 258 | * Get a navigation node by id. |
||
| 259 | * |
||
| 260 | * @param string $id The id. |
||
| 261 | * @param bool $recursively Recursively ? |
||
| 262 | * @return AbstractNavigationNode Returns the navigation node in case of success, null otherwise. |
||
| 263 | */ |
||
| 264 | public function getNodeById($id, $recursively = false) { |
||
| 286 | |||
| 287 | /** |
||
| 288 | * Get the navigation nodes. |
||
| 289 | * |
||
| 290 | * @return AbstractNavigationNode[] Returns the navigation nodes. |
||
| 291 | */ |
||
| 292 | public function getNodes() { |
||
| 295 | |||
| 296 | /** |
||
| 297 | * Get the parent. |
||
| 298 | * |
||
| 299 | * @return AbstractNavigationNode Returns the parent. |
||
| 300 | */ |
||
| 301 | public function getParent() { |
||
| 304 | |||
| 305 | /** |
||
| 306 | * Get the target. |
||
| 307 | * |
||
| 308 | * @return string Returns the target. |
||
| 309 | */ |
||
| 310 | public function getTarget() { |
||
| 313 | |||
| 314 | /** |
||
| 315 | * Get the URI. |
||
| 316 | * |
||
| 317 | * @return string Returns the URI. |
||
| 318 | */ |
||
| 319 | public function getUri() { |
||
| 322 | |||
| 323 | /** |
||
| 324 | * Get the visible. |
||
| 325 | * |
||
| 326 | * @return bool Returns the visible. |
||
| 327 | */ |
||
| 328 | public function getVisible() { |
||
| 331 | |||
| 332 | /** |
||
| 333 | * Determines if this node is displayable. |
||
| 334 | * |
||
| 335 | * @return bool Returns true in case of success, false otherwise. |
||
| 336 | */ |
||
| 337 | public function isDisplayable() { |
||
| 352 | |||
| 353 | /** |
||
| 354 | * Remove a navigation node. |
||
| 355 | * |
||
| 356 | * @param AbstractNavigationNode $node The navigation node. |
||
| 357 | * @return AbstractNavigationNode Returns this navigation node. |
||
| 358 | */ |
||
| 359 | public function removeNode(AbstractNavigationNode $node) { |
||
| 370 | |||
| 371 | /** |
||
| 372 | * Set the active. |
||
| 373 | * |
||
| 374 | * @param bool $active Active ? |
||
| 375 | * @return AbstractNavigationNode Returns this navigation node. |
||
| 376 | */ |
||
| 377 | public function setActive($active) { |
||
| 381 | |||
| 382 | /** |
||
| 383 | * Set the enable. |
||
| 384 | * |
||
| 385 | * @param bool $enable Enable ? |
||
| 386 | * @return AbstractNavigationNode Returns this navigation node. |
||
| 387 | */ |
||
| 388 | public function setEnable($enable) { |
||
| 392 | |||
| 393 | /** |
||
| 394 | * Set the icon. |
||
| 395 | * |
||
| 396 | * @param string $icon The icon. |
||
| 397 | * @return AbstractNavigationNode Returns this navigation node. |
||
| 398 | */ |
||
| 399 | public function setIcon($icon) { |
||
| 403 | |||
| 404 | /** |
||
| 405 | * Set the id. |
||
| 406 | * |
||
| 407 | * @param string $id The id. |
||
| 408 | * @return AbstractNavigationNode Returns this navigation node. |
||
| 409 | */ |
||
| 410 | protected function setId($id) { |
||
| 414 | |||
| 415 | /** |
||
| 416 | * Set the index. |
||
| 417 | * |
||
| 418 | * @param array $index The index. |
||
| 419 | * @return AbstractNavigationNode Returns this navigation node. |
||
| 420 | */ |
||
| 421 | protected function setIndex(array $index) { |
||
| 425 | |||
| 426 | /** |
||
| 427 | * Set the label. |
||
| 428 | * |
||
| 429 | * @param string $label The label. |
||
| 430 | * @return AbstractNavigationNode Returns this navigation node. |
||
| 431 | */ |
||
| 432 | public function setLabel(string $label): AbstractNavigationNode { |
||
| 436 | |||
| 437 | /** |
||
| 438 | * Set the mather. |
||
| 439 | * |
||
| 440 | * @param string $matcher The matcher. |
||
| 441 | * @return AbstractNavigationNode Returns this navigation node. |
||
| 442 | */ |
||
| 443 | public function setMatcher($matcher) { |
||
| 447 | |||
| 448 | /** |
||
| 449 | * Set the navigation nodes. |
||
| 450 | * |
||
| 451 | * @param AbstractNavigationNode[] $nodes The navigation nodes. |
||
| 452 | * @return AbstractNavigationNode Returns this navigation node. |
||
| 453 | */ |
||
| 454 | protected function setNodes(array $nodes) { |
||
| 458 | |||
| 459 | /** |
||
| 460 | * Set the parent. |
||
| 461 | * |
||
| 462 | * @param AbstractNavigationNode $parent The parent. |
||
| 463 | * @return AbstractNavigationNode Returns this navigation node. |
||
| 464 | */ |
||
| 465 | protected function setParent(AbstractNavigationNode $parent = null) { |
||
| 469 | |||
| 470 | /** |
||
| 471 | * Set the target. |
||
| 472 | * |
||
| 473 | * @param string $target The target. |
||
| 474 | * @return AbstractNavigationNode Returns this navigation node. |
||
| 475 | */ |
||
| 476 | public function setTarget($target) { |
||
| 480 | |||
| 481 | /** |
||
| 482 | * Set the URI. |
||
| 483 | * |
||
| 484 | * @param string $uri The URI. |
||
| 485 | * @return AbstractNavigationNode Returns this navigation node. |
||
| 486 | */ |
||
| 487 | public function setUri($uri) { |
||
| 491 | |||
| 492 | /** |
||
| 493 | * Set the visible. |
||
| 494 | * |
||
| 495 | * @param bool $visible Visible ? |
||
| 496 | * @return AbstractNavigationNode Returns this navigation node. |
||
| 497 | */ |
||
| 498 | protected function setVisible($visible) { |
||
| 502 | |||
| 503 | /** |
||
| 504 | * Size. |
||
| 505 | * |
||
| 506 | * @return int Returns the size. |
||
| 507 | */ |
||
| 508 | public function size() { |
||
| 511 | } |
||
| 512 |