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 |
||
21 | class Updater extends RepositoryUpdater |
||
22 | { |
||
23 | /** |
||
24 | * Insert a record into the message queue. |
||
25 | * |
||
26 | * @param string $name |
||
27 | * @param Model $model |
||
28 | * @param int|User $changeBy |
||
29 | * |
||
30 | * @return void |
||
31 | */ |
||
32 | View Code Duplication | public function queue($name, Model $model, $changeBy) |
|
57 | |||
58 | /** |
||
59 | * Insert a record into the message queue about a delete event. |
||
60 | * |
||
61 | * @param string $name |
||
62 | * @param Model $model |
||
63 | * @param int|User $changeBy |
||
64 | * |
||
65 | * @return void |
||
66 | */ |
||
67 | View Code Duplication | public function queueDelete($name, Model $model, $changeBy) |
|
77 | |||
78 | /** |
||
79 | * Insert records about tag changes into the message queue. |
||
80 | * |
||
81 | * @param Issue $issue |
||
82 | * @param array $added |
||
83 | * @param array $removed |
||
84 | * @param UserInterface $changeBy |
||
85 | * |
||
86 | * @return mixed |
||
87 | */ |
||
88 | public function queueIssueTagChanges(Issue $issue, array $added, array $removed, UserInterface $changeBy) |
||
98 | |||
99 | /** |
||
100 | * Returns an array containing the data needed for the message queue save. |
||
101 | * |
||
102 | * @param string $name |
||
103 | * @param Model $model |
||
104 | * @param int|UserInterface $changeBy |
||
105 | * @param array $payload |
||
106 | * |
||
107 | * @return array |
||
108 | */ |
||
109 | View Code Duplication | protected function getFillAttributes($name, Model $model, $changeBy, array $payload) |
|
122 | } |
||
123 |
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.