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 |
||
22 | class ReviewCommand extends AbstractCommand |
||
23 | { |
||
24 | /** |
||
25 | * AbstractCommand constructor. |
||
26 | * |
||
27 | * @param Payload $payload |
||
28 | * @param RealTimeClient $client |
||
29 | * @param array|null $configuration |
||
30 | */ |
||
31 | 27 | public function __construct(Payload $payload, RealTimeClient $client, array $configuration = null) |
|
45 | |||
46 | /** |
||
47 | * process count. |
||
48 | * |
||
49 | * @return string |
||
50 | */ |
||
51 | 1 | protected function processCount() : string |
|
74 | |||
75 | /** |
||
76 | * process random. |
||
77 | * |
||
78 | * @return Message |
||
79 | */ |
||
80 | 1 | protected function processRandom() : Message |
|
81 | { |
||
82 | /** @var array $result */ |
||
83 | 1 | $result = $this->queryGerrit('is:open project:Packages/TYPO3.CMS'); |
|
84 | 1 | $item = $result[array_rand($result)]; |
|
85 | |||
86 | 1 | return $this->buildReviewMessage($item); |
|
87 | } |
||
88 | |||
89 | /** |
||
90 | * process user. |
||
91 | * |
||
92 | * @return string |
||
93 | */ |
||
94 | 3 | protected function processUser() : string |
|
95 | { |
||
96 | 3 | $username = $this->params[1] ?? null; |
|
97 | 3 | $project = $this->params[2] ?? 'Packages/TYPO3.CMS'; |
|
98 | 3 | if ($username === null) { |
|
99 | 1 | return 'hey, I need a username!'; |
|
100 | } |
||
101 | 2 | $results = $this->queryGerrit('is:open owner:"' . $username . '" project:' . $project); |
|
102 | 2 | View Code Duplication | if (!empty($results)) { |
|
|||
103 | 1 | $listOfItems = ['*Here are the results for ' . $username . '*:']; |
|
104 | 1 | foreach ($results as $item) { |
|
105 | 1 | $listOfItems[] = $this->buildReviewLine($item); |
|
106 | } |
||
107 | 1 | return implode(chr(10), $listOfItems); |
|
108 | } |
||
109 | 1 | return $username . ' has no open reviews or username is unknown'; |
|
110 | } |
||
111 | |||
112 | /** |
||
113 | * process count. |
||
114 | * |
||
115 | * @return string|Message |
||
116 | */ |
||
117 | 14 | protected function processShow() |
|
138 | |||
139 | /** |
||
140 | * @return string |
||
141 | */ |
||
142 | 1 | protected function buildReviewLineOutput() : string |
|
143 | { |
||
144 | 1 | $paramsCount = count($this->params); |
|
145 | 1 | $changeIds = []; |
|
146 | 1 | for ($i = 1; $i < $paramsCount; ++$i) { |
|
147 | 1 | $changeIds[] = 'change:' . $this->params[$i]; |
|
148 | } |
||
149 | 1 | $result = $this->queryGerrit(implode(' OR ', $changeIds)); |
|
150 | 1 | $listOfItems = []; |
|
151 | 1 | if (is_array($result)) { |
|
152 | 1 | foreach ($result as $item) { |
|
153 | 1 | $listOfItems[] = $this->buildReviewLine($item); |
|
154 | } |
||
155 | } |
||
156 | 1 | return implode(chr(10), $listOfItems); |
|
157 | } |
||
158 | |||
159 | /** |
||
160 | * @param int $refId |
||
161 | * |
||
162 | * @return string|Message |
||
163 | */ |
||
164 | 11 | protected function buildReviewMessageOutput(int $refId) |
|
165 | { |
||
166 | 11 | $result = $this->queryGerrit('change:' . $refId); |
|
167 | 11 | if (!empty($result)) { |
|
168 | 10 | foreach ($result as $item) { |
|
169 | 10 | if ($item->_number === $refId) { |
|
170 | 10 | return $this->buildReviewMessage($item); |
|
171 | } |
||
172 | } |
||
173 | } |
||
174 | 1 | return "{$refId} not found, sorry!"; |
|
175 | } |
||
176 | |||
177 | /** |
||
178 | * process query. |
||
179 | * |
||
180 | * @return string |
||
181 | */ |
||
182 | 3 | protected function processQuery() : string |
|
183 | { |
||
184 | 3 | array_shift($this->params); |
|
185 | 3 | $query = trim(implode(' ', $this->params)); |
|
186 | 3 | if ($query === '') { |
|
187 | 1 | return 'hey, I need a query!'; |
|
188 | } |
||
189 | |||
190 | 2 | $results = $this->queryGerrit('limit:50 ' . $query); |
|
191 | 2 | View Code Duplication | if (!empty($results)) { |
192 | 1 | $listOfItems = ["*Here are the results for {$query}*:"]; |
|
193 | 1 | foreach ($results as $item) { |
|
194 | 1 | $listOfItems[] = $this->buildReviewLine($item); |
|
195 | } |
||
196 | 1 | return implode(chr(10), $listOfItems); |
|
197 | } |
||
198 | |||
199 | 1 | return "{$query} not found, sorry!"; |
|
200 | } |
||
201 | |||
202 | /** |
||
203 | * @return string |
||
204 | */ |
||
205 | 3 | protected function processMerged() : string |
|
220 | |||
221 | /** |
||
222 | * check format of given date. |
||
223 | * |
||
224 | * @param $date |
||
225 | * |
||
226 | * @return bool |
||
227 | */ |
||
228 | 3 | protected function isDateFormatCorrect($date) : bool |
|
232 | } |
||
233 |
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.