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 |
||
20 | class Projector extends AbstractProjector |
||
21 | { |
||
22 | /** |
||
23 | * @var WriteRepositoryInterface |
||
24 | */ |
||
25 | private $writeRepository; |
||
26 | |||
27 | /** |
||
28 | * @var ReadRepositoryInterface |
||
29 | */ |
||
30 | private $readRepository; |
||
31 | |||
32 | /** |
||
33 | * Projector constructor. |
||
34 | * @param WriteRepositoryInterface $writeRepository |
||
35 | * @param ReadRepositoryInterface $readRepository |
||
36 | */ |
||
37 | public function __construct( |
||
44 | |||
45 | /** |
||
46 | * @param Created $created |
||
47 | */ |
||
48 | View Code Duplication | public function applyCreated(Created $created) |
|
61 | |||
62 | /** |
||
63 | * @param CopyCreated $copyCreated |
||
64 | */ |
||
65 | View Code Duplication | public function applyCopyCreated(CopyCreated $copyCreated) |
|
79 | |||
80 | /** |
||
81 | * @param MadeVisible $madeVisible |
||
82 | */ |
||
83 | public function applyMadeVisible(MadeVisible $madeVisible) |
||
87 | |||
88 | /** |
||
89 | * @param MadeInvisible $madeInvisible |
||
90 | */ |
||
91 | public function applyMadeInvisible(MadeInvisible $madeInvisible) |
||
95 | |||
96 | /** |
||
97 | * @param MadePublic $madePublic |
||
98 | */ |
||
99 | public function applyMadePublic(MadePublic $madePublic) |
||
103 | |||
104 | /** |
||
105 | * @param MadePrivate $madePrivate |
||
106 | */ |
||
107 | public function applyMadePrivate(MadePrivate $madePrivate) |
||
111 | |||
112 | /** |
||
113 | * @inheritdoc |
||
114 | */ |
||
115 | public function applyLabelAdded(LabelEventInterface $labelAdded, Metadata $metadata) |
||
123 | |||
124 | /** |
||
125 | * @inheritdoc |
||
126 | */ |
||
127 | public function applyLabelRemoved(LabelEventInterface $labelRemoved, Metadata $metadata) |
||
135 | |||
136 | /** |
||
137 | * @inheritdoc |
||
138 | */ |
||
139 | public function applyLabelsImported(LabelsImportedEventInterface $labelsImported, Metadata $metadata) |
||
143 | |||
144 | /** |
||
145 | * @param LabelEventInterface $labelEvent |
||
146 | * @return UUID|null |
||
147 | */ |
||
148 | private function getUuid(LabelEventInterface $labelEvent) |
||
161 | } |
||
162 |
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.