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:
1 | <?php |
||
25 | View Code Duplication | trait HasRealmsTrait |
|
|
|||
26 | { |
||
27 | protected $realmsPropertyName = 'realms'; |
||
28 | |||
29 | /** |
||
30 | * @var ObjectCollectionInterface |
||
31 | */ |
||
32 | private $realms; |
||
33 | |||
34 | public function addRealm(Contract\RealmInterface $realm) |
||
38 | |||
39 | /** |
||
40 | * @return ObjectCollectionInterface |
||
41 | */ |
||
42 | private function getRealms() |
||
46 | |||
47 | public function hasRealm($name) |
||
51 | |||
52 | /** |
||
53 | * @param $name |
||
54 | * |
||
55 | * @return Contract\RealmInterface |
||
56 | */ |
||
57 | public function getRealm($name) |
||
61 | |||
62 | /** |
||
63 | * @param $name |
||
64 | */ |
||
65 | public function removeRealm($name) |
||
69 | |||
70 | /** |
||
71 | * @return \Generator |
||
72 | */ |
||
73 | public function listRealms() |
||
79 | |||
80 | /** |
||
81 | * @param ObjectCollectionInterface $realms |
||
82 | */ |
||
83 | private function setRealms(ObjectCollectionInterface $realms) |
||
88 | } |
||
89 |
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.