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 Folder 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 Folder, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 33 | class Folder |
||
| 34 | { |
||
| 35 | const VISIBILITY_NO_RESTRICTION = 0; |
||
| 36 | const VISIBILITY_OWN_USER = 1; |
||
| 37 | const VISIBILITY_OWN_PROFILE = 2; |
||
| 38 | |||
| 39 | const GROUP_BY_NONE = 0; |
||
| 40 | const GROUP_BY_USER = 1; |
||
| 41 | const GROUP_BY_PROFILE = 2; |
||
| 42 | |||
| 43 | const TYPE_NORMAL = 0; |
||
| 44 | const TYPE_WORKFLOW = 1; |
||
| 45 | const TYPE_TASKS = 2; |
||
| 46 | |||
| 47 | /** |
||
| 48 | * @ORM\Id() |
||
| 49 | * @ORM\GeneratedValue(strategy="AUTO") |
||
| 50 | * @ORM\Column(type="integer") |
||
| 51 | * @var int |
||
| 52 | */ |
||
| 53 | private $id; |
||
| 54 | |||
| 55 | /** |
||
| 56 | * @ORM\Column(type="string") |
||
| 57 | * @var string |
||
| 58 | */ |
||
| 59 | private $name; |
||
| 60 | |||
| 61 | /** |
||
| 62 | * @ORM\Column(type="text", nullable=true) |
||
| 63 | * @var string |
||
| 64 | */ |
||
| 65 | private $description; |
||
| 66 | |||
| 67 | /** |
||
| 68 | * @Gedmo\TreeLeft |
||
| 69 | * @ORM\Column(name="lft", type="integer") |
||
| 70 | * @var int |
||
| 71 | */ |
||
| 72 | private $left; |
||
| 73 | |||
| 74 | /** |
||
| 75 | * @Gedmo\TreeLevel |
||
| 76 | * @ORM\Column(name="lvl", type="integer") |
||
| 77 | * @var int |
||
| 78 | */ |
||
| 79 | private $level; |
||
| 80 | |||
| 81 | /** |
||
| 82 | * @Gedmo\TreeRight |
||
| 83 | * @ORM\Column(name="rght", type="integer") |
||
| 84 | * @var int |
||
| 85 | */ |
||
| 86 | private $right; |
||
| 87 | |||
| 88 | /** |
||
| 89 | * @Gedmo\TreeParent() |
||
| 90 | * @ORM\ManyToOne(targetEntity="Folder", inversedBy="children") |
||
| 91 | * @ORM\JoinColumn(onDelete="CASCADE", nullable=true) |
||
| 92 | * @var Folder |
||
| 93 | */ |
||
| 94 | private $parent; |
||
| 95 | |||
| 96 | /** |
||
| 97 | * @ORM\OneToMany(targetEntity="Folder", mappedBy="parent") |
||
| 98 | * @var Collection |
||
| 99 | */ |
||
| 100 | private $children; |
||
| 101 | |||
| 102 | /** |
||
| 103 | * @Gedmo\TreeRoot |
||
| 104 | * @ORM\ManyToOne(targetEntity="AppBundle\Entity\Organization") |
||
| 105 | * @ORM\JoinColumn(nullable=false, onDelete="CASCADE") |
||
| 106 | * @var Organization |
||
| 107 | */ |
||
| 108 | private $organization; |
||
| 109 | |||
| 110 | /** |
||
| 111 | * @ORM\OneToMany(targetEntity="FolderPermission", mappedBy="folder") |
||
| 112 | * @var Collection |
||
| 113 | */ |
||
| 114 | private $permissions; |
||
| 115 | |||
| 116 | /** |
||
| 117 | * @ORM\Column(type="integer") |
||
| 118 | * @var int |
||
| 119 | */ |
||
| 120 | private $type; |
||
| 121 | |||
| 122 | /** |
||
| 123 | * @ORM\Column(type="boolean") |
||
| 124 | * @var bool |
||
| 125 | */ |
||
| 126 | private $versionShown; |
||
| 127 | |||
| 128 | /** |
||
| 129 | * @ORM\Column(type="boolean") |
||
| 130 | * @var bool |
||
| 131 | */ |
||
| 132 | private $public; |
||
| 133 | |||
| 134 | /** |
||
| 135 | * @ORM\Column(type="string", nullable=true) |
||
| 136 | * @var string |
||
| 137 | */ |
||
| 138 | private $publicToken; |
||
| 139 | |||
| 140 | /** |
||
| 141 | * @ORM\OneToMany(targetEntity="Entry", mappedBy="folder") |
||
| 142 | * @var Collection |
||
| 143 | */ |
||
| 144 | private $entries; |
||
| 145 | |||
| 146 | /** |
||
| 147 | * @ORM\OneToMany(targetEntity="Task", mappedBy="folder") |
||
| 148 | * @var Collection |
||
| 149 | */ |
||
| 150 | private $tasks; |
||
| 151 | |||
| 152 | /** |
||
| 153 | * @ORM\Column(type="integer") |
||
| 154 | * @var int |
||
| 155 | */ |
||
| 156 | private $visibility; |
||
| 157 | |||
| 158 | /** |
||
| 159 | * @ORM\Column(type="integer") |
||
| 160 | * @var int |
||
| 161 | */ |
||
| 162 | private $groupBy; |
||
| 163 | |||
| 164 | /** |
||
| 165 | * @ORM\Column(type="boolean") |
||
| 166 | * @var bool |
||
| 167 | */ |
||
| 168 | private $autoArchive; |
||
| 169 | |||
| 170 | /** |
||
| 171 | * Constructor |
||
| 172 | */ |
||
| 173 | public function __construct() |
||
| 187 | |||
| 188 | /** |
||
| 189 | * Converts entity to string |
||
| 190 | * |
||
| 191 | * @return string |
||
| 192 | */ |
||
| 193 | public function __toString() |
||
| 197 | |||
| 198 | /** |
||
| 199 | * Get element path |
||
| 200 | * |
||
| 201 | * @return string |
||
| 202 | */ |
||
| 203 | View Code Duplication | public function getPath() |
|
| 217 | |||
| 218 | /** |
||
| 219 | * Get element path array from specified root |
||
| 220 | * |
||
| 221 | * @param Folder|null $root |
||
| 222 | * |
||
| 223 | * @return Folder[] |
||
| 224 | */ |
||
| 225 | View Code Duplication | public function getPathArray(Folder $root = null) |
|
| 236 | |||
| 237 | /** |
||
| 238 | * Get id |
||
| 239 | * |
||
| 240 | * @return integer |
||
| 241 | */ |
||
| 242 | public function getId() |
||
| 246 | |||
| 247 | /** |
||
| 248 | * Set name |
||
| 249 | * |
||
| 250 | * @param string $name |
||
| 251 | * |
||
| 252 | * @return Folder |
||
| 253 | */ |
||
| 254 | public function setName($name) |
||
| 260 | |||
| 261 | /** |
||
| 262 | * Get name |
||
| 263 | * |
||
| 264 | * @return string |
||
| 265 | */ |
||
| 266 | public function getName() |
||
| 270 | |||
| 271 | /** |
||
| 272 | * Set left |
||
| 273 | * |
||
| 274 | * @param integer $left |
||
| 275 | * |
||
| 276 | * @return Folder |
||
| 277 | */ |
||
| 278 | public function setLeft($left) |
||
| 284 | |||
| 285 | /** |
||
| 286 | * Get left |
||
| 287 | * |
||
| 288 | * @return integer |
||
| 289 | */ |
||
| 290 | public function getLeft() |
||
| 294 | |||
| 295 | /** |
||
| 296 | * Set level |
||
| 297 | * |
||
| 298 | * @param integer $level |
||
| 299 | * |
||
| 300 | * @return Folder |
||
| 301 | */ |
||
| 302 | public function setLevel($level) |
||
| 308 | |||
| 309 | /** |
||
| 310 | * Get level |
||
| 311 | * |
||
| 312 | * @return integer |
||
| 313 | */ |
||
| 314 | public function getLevel() |
||
| 318 | |||
| 319 | /** |
||
| 320 | * Set right |
||
| 321 | * |
||
| 322 | * @param integer $right |
||
| 323 | * |
||
| 324 | * @return Folder |
||
| 325 | */ |
||
| 326 | public function setRight($right) |
||
| 332 | |||
| 333 | /** |
||
| 334 | * Get right |
||
| 335 | * |
||
| 336 | * @return integer |
||
| 337 | */ |
||
| 338 | public function getRight() |
||
| 342 | |||
| 343 | /** |
||
| 344 | * Set parent |
||
| 345 | * |
||
| 346 | * @param Folder $parent |
||
| 347 | * |
||
| 348 | * @return Folder |
||
| 349 | */ |
||
| 350 | public function setParent(Folder $parent = null) |
||
| 356 | |||
| 357 | /** |
||
| 358 | * Get parent |
||
| 359 | * |
||
| 360 | * @return Folder |
||
| 361 | */ |
||
| 362 | public function getParent() |
||
| 366 | |||
| 367 | /** |
||
| 368 | * Add child |
||
| 369 | * |
||
| 370 | * @param Folder $child |
||
| 371 | * |
||
| 372 | * @return Folder |
||
| 373 | */ |
||
| 374 | public function addChild(Folder $child) |
||
| 380 | |||
| 381 | /** |
||
| 382 | * Remove child |
||
| 383 | * |
||
| 384 | * @param Folder $child |
||
| 385 | */ |
||
| 386 | public function removeChild(Folder $child) |
||
| 390 | |||
| 391 | /** |
||
| 392 | * Get children |
||
| 393 | * |
||
| 394 | * @return Collection |
||
| 395 | */ |
||
| 396 | public function getChildren() |
||
| 400 | |||
| 401 | /** |
||
| 402 | * Set description |
||
| 403 | * |
||
| 404 | * @param string $description |
||
| 405 | * |
||
| 406 | * @return Folder |
||
| 407 | */ |
||
| 408 | public function setDescription($description) |
||
| 414 | |||
| 415 | /** |
||
| 416 | * Get description |
||
| 417 | * |
||
| 418 | * @return string |
||
| 419 | */ |
||
| 420 | public function getDescription() |
||
| 424 | |||
| 425 | /** |
||
| 426 | * Set organization |
||
| 427 | * |
||
| 428 | * @param Organization $organization |
||
| 429 | * |
||
| 430 | * @return Folder |
||
| 431 | */ |
||
| 432 | public function setOrganization(Organization $organization) |
||
| 438 | |||
| 439 | /** |
||
| 440 | * Get organization |
||
| 441 | * |
||
| 442 | * @return Organization |
||
| 443 | */ |
||
| 444 | public function getOrganization() |
||
| 448 | |||
| 449 | /** |
||
| 450 | * Add permission |
||
| 451 | * |
||
| 452 | * @param FolderPermission $permission |
||
| 453 | * |
||
| 454 | * @return Folder |
||
| 455 | */ |
||
| 456 | public function addPermission(FolderPermission $permission) |
||
| 462 | |||
| 463 | /** |
||
| 464 | * Remove permission |
||
| 465 | * |
||
| 466 | * @param FolderPermission $permission |
||
| 467 | */ |
||
| 468 | public function removePermission(FolderPermission $permission) |
||
| 472 | |||
| 473 | /** |
||
| 474 | * Get permissions |
||
| 475 | * |
||
| 476 | * @return Collection |
||
| 477 | */ |
||
| 478 | public function getPermissions() |
||
| 482 | |||
| 483 | /** |
||
| 484 | * Set versionShown |
||
| 485 | * |
||
| 486 | * @param boolean $versionShown |
||
| 487 | * |
||
| 488 | * @return Folder |
||
| 489 | */ |
||
| 490 | public function setVersionShown($versionShown) |
||
| 496 | |||
| 497 | /** |
||
| 498 | * Get versionShown |
||
| 499 | * |
||
| 500 | * @return boolean |
||
| 501 | */ |
||
| 502 | public function isVersionShown() |
||
| 506 | |||
| 507 | /** |
||
| 508 | * Set public |
||
| 509 | * |
||
| 510 | * @param boolean $public |
||
| 511 | * |
||
| 512 | * @return Folder |
||
| 513 | */ |
||
| 514 | public function setPublic($public) |
||
| 520 | |||
| 521 | /** |
||
| 522 | * Get public |
||
| 523 | * |
||
| 524 | * @return boolean |
||
| 525 | */ |
||
| 526 | public function isPublic() |
||
| 530 | |||
| 531 | /** |
||
| 532 | * Set publicToken |
||
| 533 | * |
||
| 534 | * @param string $publicToken |
||
| 535 | * |
||
| 536 | * @return Folder |
||
| 537 | */ |
||
| 538 | public function setPublicToken($publicToken) |
||
| 544 | |||
| 545 | /** |
||
| 546 | * Get publicToken |
||
| 547 | * |
||
| 548 | * @return string |
||
| 549 | */ |
||
| 550 | public function getPublicToken() |
||
| 554 | |||
| 555 | /** |
||
| 556 | * Add entry |
||
| 557 | * |
||
| 558 | * @param Entry $entry |
||
| 559 | * |
||
| 560 | * @return Folder |
||
| 561 | */ |
||
| 562 | public function addEntry(Entry $entry) |
||
| 568 | |||
| 569 | /** |
||
| 570 | * Remove entry |
||
| 571 | * |
||
| 572 | * @param Entry $entry |
||
| 573 | */ |
||
| 574 | public function removeEntry(Entry $entry) |
||
| 578 | |||
| 579 | /** |
||
| 580 | * Get entries |
||
| 581 | * |
||
| 582 | * @return Collection |
||
| 583 | */ |
||
| 584 | public function getEntries() |
||
| 588 | |||
| 589 | /** |
||
| 590 | * Set visibility |
||
| 591 | * |
||
| 592 | * @param integer $visibility |
||
| 593 | * |
||
| 594 | * @return Folder |
||
| 595 | */ |
||
| 596 | public function setVisibility($visibility) |
||
| 602 | |||
| 603 | /** |
||
| 604 | * Get visibility |
||
| 605 | * |
||
| 606 | * @return integer |
||
| 607 | */ |
||
| 608 | public function getVisibility() |
||
| 612 | |||
| 613 | /** |
||
| 614 | * Set groupBy |
||
| 615 | * |
||
| 616 | * @param integer $groupBy |
||
| 617 | * |
||
| 618 | * @return Folder |
||
| 619 | */ |
||
| 620 | public function setGroupBy($groupBy) |
||
| 626 | |||
| 627 | /** |
||
| 628 | * Get groupBy |
||
| 629 | * |
||
| 630 | * @return integer |
||
| 631 | */ |
||
| 632 | public function getGroupBy() |
||
| 636 | |||
| 637 | /** |
||
| 638 | * Set type |
||
| 639 | * |
||
| 640 | * @param integer $type |
||
| 641 | * |
||
| 642 | * @return Folder |
||
| 643 | */ |
||
| 644 | public function setType($type) |
||
| 650 | |||
| 651 | /** |
||
| 652 | * Get type |
||
| 653 | * |
||
| 654 | * @return integer |
||
| 655 | */ |
||
| 656 | public function getType() |
||
| 660 | |||
| 661 | /** |
||
| 662 | * Add task |
||
| 663 | * |
||
| 664 | * @param Task $task |
||
| 665 | * |
||
| 666 | * @return Folder |
||
| 667 | */ |
||
| 668 | public function addTask(Task $task) |
||
| 674 | |||
| 675 | /** |
||
| 676 | * Remove task |
||
| 677 | * |
||
| 678 | * @param Task $task |
||
| 679 | */ |
||
| 680 | public function removeTask(Task $task) |
||
| 684 | |||
| 685 | /** |
||
| 686 | * Get tasks |
||
| 687 | * |
||
| 688 | * @return Collection |
||
| 689 | */ |
||
| 690 | public function getTasks() |
||
| 694 | |||
| 695 | /** |
||
| 696 | * @return bool |
||
| 697 | */ |
||
| 698 | public function isAutoArchive() |
||
| 702 | |||
| 703 | /** |
||
| 704 | * @param bool $autoArchive |
||
| 705 | * |
||
| 706 | * @return Folder |
||
| 707 | */ |
||
| 708 | public function setAutoArchive($autoArchive) |
||
| 713 | |||
| 714 | } |
||
| 715 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.