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 TellCommand extends AbstractCommand |
||
22 | { |
||
23 | const PRESENCE_ACTIVE = 'active'; |
||
24 | const PRESENCE_AWAY = 'away'; |
||
25 | |||
26 | /** |
||
27 | * AbstractCommand constructor. |
||
28 | * |
||
29 | * @param Payload $payload |
||
30 | * @param RealTimeClient $client |
||
31 | * @param array|null $configuration |
||
32 | */ |
||
33 | 8 | View Code Duplication | public function __construct(Payload $payload, RealTimeClient $client, array $configuration = null) |
44 | |||
45 | /** |
||
46 | * @return bool|string |
||
47 | * |
||
48 | * @throws \Doctrine\DBAL\DBALException |
||
49 | */ |
||
50 | 6 | public function process() |
|
61 | |||
62 | /** |
||
63 | * @param string $user |
||
64 | * @param string $presence |
||
65 | * |
||
66 | * @throws \Doctrine\DBAL\DBALException |
||
67 | */ |
||
68 | 4 | public function processPresenceChange($user, $presence) |
|
84 | |||
85 | /** |
||
86 | * @param string $user |
||
87 | * |
||
88 | * @return array |
||
89 | * @throws \Doctrine\DBAL\DBALException |
||
90 | */ |
||
91 | 3 | protected function getNotificationsForUser(string $user) : array |
|
102 | |||
103 | /** |
||
104 | * @param int $id |
||
105 | * |
||
106 | * @throws \Doctrine\DBAL\DBALException |
||
107 | */ |
||
108 | 3 | protected function markNotificationAsDelivered(int $id) |
|
119 | |||
120 | /** |
||
121 | * @param array $notification |
||
122 | * @param string $user |
||
123 | */ |
||
124 | 1 | protected function processReviewMessage(array $notification, string $user) |
|
125 | { |
||
126 | 1 | $parts = explode(':', $notification['message']); |
|
127 | 1 | $refId = (int) trim($parts[1]); |
|
128 | 1 | $result = $this->queryGerrit('change:' . $refId); |
|
129 | 1 | $msg = '*Hi <@' . $user . '>, <@' . $notification['from_user'] . '>' |
|
130 | 1 | . ' ask you to look at this patch:*'; |
|
131 | |||
132 | 1 | if (is_array($result)) { |
|
133 | 1 | foreach ($result as $item) { |
|
134 | 1 | if ((int) $item->_number === $refId) { |
|
135 | 1 | $message = $this->buildReviewMessage($item); |
|
136 | 1 | $message->setText($msg); |
|
137 | 1 | $this->sendResponse($message, $user); |
|
138 | } |
||
139 | } |
||
140 | } |
||
141 | 1 | } |
|
142 | |||
143 | /** |
||
144 | * @param array $notification |
||
145 | * @param string $user |
||
146 | */ |
||
147 | 1 | protected function processForgeMessage(array $notification, string $user) |
|
148 | { |
||
149 | 1 | $parts = explode(':', $notification['message']); |
|
150 | 1 | $issueNumber = (int) trim($parts[1]); |
|
151 | 1 | $result = $this->queryForge('issues/' . $issueNumber); |
|
152 | 1 | if ($result) { |
|
153 | 1 | $msg = '*Hi <@' . $user . '>, <@' . $notification['from_user'] . '>' |
|
154 | 1 | . ' ask you to look at this issue:*'; |
|
155 | 1 | $this->sendResponse($msg . chr(10) . $this->buildIssueMessage($result->issue), $user); |
|
156 | } |
||
157 | 1 | } |
|
158 | |||
159 | /** |
||
160 | * @param array $notification |
||
161 | * @param string $user |
||
162 | */ |
||
163 | 1 | protected function processTextMessage(array $notification, string $user) |
|
168 | |||
169 | /** |
||
170 | * @return string |
||
171 | * |
||
172 | * @throws \Doctrine\DBAL\DBALException |
||
173 | */ |
||
174 | 6 | protected function processTell() : string |
|
194 | } |
||
195 |