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 |
||
28 | abstract class AbstractCommand |
||
29 | { |
||
30 | use SlackTrait, ForgerTrait, GerritTrait; |
||
31 | |||
32 | const PROJECT_PHASE_DEVELOPMENT = 'development'; |
||
33 | const PROJECT_PHASE_STABILISATION = 'stabilisation'; |
||
34 | const PROJECT_PHASE_SOFT_FREEZE = 'soft_freeze'; |
||
35 | const PROJECT_PHASE_CODE_FREEZE = 'code_freeze'; |
||
36 | const PROJECT_PHASE_FEATURE_FREEZE = 'feature_freeze'; |
||
37 | |||
38 | /** |
||
39 | * @var string |
||
40 | */ |
||
41 | protected $commandName; |
||
42 | |||
43 | /** |
||
44 | * @var array |
||
45 | */ |
||
46 | protected $helpCommands = []; |
||
47 | |||
48 | /** |
||
49 | * @var array |
||
50 | */ |
||
51 | protected $params = []; |
||
52 | |||
53 | /** |
||
54 | * @var Payload |
||
55 | */ |
||
56 | protected $payload; |
||
57 | |||
58 | /** |
||
59 | * @var RealTimeClient |
||
60 | */ |
||
61 | protected $client; |
||
62 | |||
63 | /** |
||
64 | * @var array|null |
||
65 | */ |
||
66 | protected $configuration; |
||
67 | |||
68 | /** |
||
69 | * @var Connection |
||
70 | */ |
||
71 | protected $databaseConnection; |
||
72 | |||
73 | /** |
||
74 | * @var array |
||
75 | */ |
||
76 | protected $colors = [ |
||
77 | self::PROJECT_PHASE_STABILISATION => Message\Attachment::COLOR_WARNING, |
||
78 | self::PROJECT_PHASE_SOFT_FREEZE => Message\Attachment::COLOR_DANGER, |
||
79 | self::PROJECT_PHASE_CODE_FREEZE => Message\Attachment::COLOR_DANGER, |
||
80 | self::PROJECT_PHASE_FEATURE_FREEZE => Message\Attachment::COLOR_DANGER, |
||
81 | ]; |
||
82 | |||
83 | /** |
||
84 | * @var array |
||
85 | */ |
||
86 | protected $preTexts = [ |
||
87 | self::PROJECT_PHASE_STABILISATION => ':warning: *stabilisation phase*', |
||
88 | self::PROJECT_PHASE_SOFT_FREEZE => ':no_entry: *soft merge freeze*', |
||
89 | self::PROJECT_PHASE_CODE_FREEZE => ':no_entry: *merge freeze*', |
||
90 | self::PROJECT_PHASE_FEATURE_FREEZE => ':no_entry: *FEATURE FREEZE*', |
||
91 | ]; |
||
92 | |||
93 | /** |
||
94 | * AbstractCommand constructor. |
||
95 | * |
||
96 | * @param Payload $payload |
||
97 | * @param RealTimeClient $client |
||
98 | * @param array|null $configuration |
||
99 | */ |
||
100 | 105 | public function __construct(Payload $payload, RealTimeClient $client, array $configuration = null) |
|
106 | |||
107 | /** |
||
108 | * |
||
109 | */ |
||
110 | 47 | public function process() |
|
128 | |||
129 | /** |
||
130 | * @param Message|string $messageToSent |
||
131 | * @param string $user |
||
132 | * @param string $channel the channel id |
||
133 | */ |
||
134 | 2 | public function sendResponse($messageToSent, $user = null, $channel = null) |
|
147 | |||
148 | /** |
||
149 | * generate help. |
||
150 | * |
||
151 | * @return string |
||
152 | */ |
||
153 | 1 | public function getHelp() : string |
|
162 | |||
163 | /** |
||
164 | * build a review message. |
||
165 | * |
||
166 | * @param \stdClass $item the review item |
||
167 | * |
||
168 | * @return Message |
||
169 | */ |
||
170 | 12 | protected function buildReviewMessage($item) : Message |
|
171 | { |
||
172 | 12 | $created = substr($item->created, 0, 19); |
|
173 | 12 | $branch = $item->branch; |
|
174 | |||
175 | 12 | $color = $this->colors[$this->configuration['projectPhase']] ?? Message\Attachment::COLOR_NOTICE; |
|
176 | 12 | $preText = $this->preTexts[$this->configuration['projectPhase']] ?? ''; |
|
177 | |||
178 | 12 | $message = new Message(); |
|
179 | 12 | $attachment = new Message\Attachment(); |
|
180 | 12 | $attachment->setColor($color); |
|
181 | 12 | $attachment->setPretext($preText); |
|
182 | 12 | $attachment->setTitle($item->subject); |
|
183 | 12 | $attachment->setTitleLink('https://review.typo3.org/' . $item->_number); |
|
184 | |||
185 | 12 | $text = 'Branch: ' . $this->bold($branch) . ' | :calendar: ' . $this->bold($created) |
|
186 | 12 | . ' | ID: ' . $this->bold($item->_number) . chr(10); |
|
187 | 12 | $text .= '<https://review.typo3.org/' . $item->_number . '|:arrow_right: Goto Review>'; |
|
188 | |||
189 | 12 | $attachment->setText($text); |
|
190 | 12 | $attachment->setFallback($text); |
|
191 | 12 | $message->setText(' '); |
|
192 | 12 | $message->addAttachment($attachment); |
|
193 | |||
194 | 12 | return $message; |
|
195 | } |
||
196 | |||
197 | /** |
||
198 | * @param Message|string $messageToSent |
||
199 | * @param string $channel |
||
200 | */ |
||
201 | 2 | protected function postMessage($messageToSent, string $channel) |
|
220 | |||
221 | /** |
||
222 | * @return Connection |
||
223 | * |
||
224 | * @throws \Doctrine\DBAL\DBALException |
||
225 | */ |
||
226 | 11 | protected function getDatabaseConnection() : Connection |
|
234 | } |
||
235 |
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.