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 ChatNotification implements RecordsDataListener |
||
21 | { |
||
22 | /** @var ChatNotificationHelper */ |
||
23 | protected $chatNotification; |
||
24 | |||
25 | /** @var Time */ |
||
26 | protected $timeFormater; |
||
27 | |||
28 | /** @var PlayerStorage */ |
||
29 | protected $playerStorage; |
||
30 | |||
31 | /** @var string */ |
||
32 | protected $translationPrefix; |
||
33 | |||
34 | /** @var ConfigInterface */ |
||
35 | protected $positionForPublicMessage; |
||
36 | |||
37 | /** |
||
38 | * ChatNotification constructor. |
||
39 | * |
||
40 | * @param ChatNotificationHelper $chatNotification |
||
41 | * @param Time $timeFormater |
||
42 | * @param PlayerStorage $playerStorage |
||
43 | * @param string $translationPrefix |
||
44 | * @param ConfigInterface $positionForPublicMessage |
||
45 | */ |
||
46 | 15 | public function __construct( |
|
59 | |||
60 | /** |
||
61 | * @inheritdoc |
||
62 | */ |
||
63 | 2 | public function onLocalRecordsLoaded($records, BaseRecords $baseRecords) |
|
86 | |||
87 | /** |
||
88 | * @inheritdoc |
||
89 | */ |
||
90 | 1 | public function onLocalRecordsFirstRecord(Record $record, $records, $position, BaseRecords $baseRecords) |
|
94 | |||
95 | /** |
||
96 | * @inheritdoc |
||
97 | */ |
||
98 | 1 | public function onLocalRecordsSameScore(Record $record, Record $oldRecord, $records, BaseRecords $baseRecords) |
|
102 | |||
103 | /** |
||
104 | * @inheritdoc |
||
105 | */ |
||
106 | 6 | public function onLocalRecordsBetterPosition(Record $record, Record $oldRecord, $records, $position, $oldPosition, BaseRecords $baseRecords) |
|
107 | { |
||
108 | 6 | if ($position == 1 && $oldPosition == null) { |
|
109 | 1 | $this->messageFirstPlaceNew($record); |
|
110 | |||
111 | 1 | return; |
|
112 | } |
||
113 | |||
114 | // Check to who to send. |
||
115 | 5 | $to = null; |
|
116 | 5 | if ($position > $this->positionForPublicMessage->get()) { |
|
117 | 1 | $to = $record->getPlayer()->getLogin(); |
|
118 | } |
||
119 | |||
120 | // Check which message to send. |
||
121 | 5 | $msg = 'better'; |
|
122 | 5 | if ($oldPosition == null) { |
|
123 | 1 | $msg = 'new'; |
|
124 | } |
||
125 | |||
126 | // Check for top status |
||
127 | 5 | View Code Duplication | if ($position == 1) { |
|
|||
128 | 1 | $msg .= '.top1'; |
|
129 | } else { |
||
130 | 4 | if ($position <= 5) { |
|
131 | 1 | $msg .= '.top5'; |
|
132 | } else { |
||
133 | 3 | $msg .= '.any'; |
|
134 | } |
||
135 | } |
||
136 | |||
137 | 5 | $securedBy = $this->getSecuredBy($record, $oldRecord); |
|
138 | 5 | $this->sendMessage( |
|
139 | 5 | $msg, |
|
140 | $to, |
||
141 | [ |
||
142 | 5 | '%nickname%' => TMString::trimStyles($record->getPlayer()->getNickName()), |
|
143 | 5 | '%score%' => $this->timeFormater->timeToText($record->getScore(), true), |
|
144 | 5 | '%position%' => $position, |
|
145 | 5 | '%old_position%' => $oldPosition, |
|
146 | 5 | '%by%' => $securedBy, |
|
147 | ] |
||
148 | ); |
||
149 | 5 | } |
|
150 | |||
151 | /** |
||
152 | * @inheritdoc |
||
153 | */ |
||
154 | 6 | public function onLocalRecordsSamePosition(Record $record, Record $oldRecord, $records, $position, BaseRecords $baseRecords) |
|
155 | { |
||
156 | // Check to who to send. |
||
157 | 6 | $to = null; |
|
158 | 6 | if ($position > $this->positionForPublicMessage->get()) { |
|
159 | 3 | $to = $record->getPlayer()->getLogin(); |
|
160 | } |
||
161 | |||
162 | // Check which message to send. |
||
163 | 6 | $msg = 'secures'; |
|
164 | 6 | View Code Duplication | if ($position == 1) { |
165 | 1 | $msg .= '.top1'; |
|
166 | } else { |
||
167 | 5 | if ($position <= 5) { |
|
168 | 1 | $msg .= '.top5'; |
|
169 | } else { |
||
170 | 4 | $msg .= '.any'; |
|
171 | } |
||
172 | } |
||
173 | |||
174 | 6 | $securedBy = $this->getSecuredBy($record, $oldRecord); |
|
175 | 6 | $this->sendMessage( |
|
176 | 6 | $msg, |
|
177 | $to, |
||
178 | [ |
||
179 | 6 | '%nickname%' => TMString::trimStyles($record->getPlayer()->getNickName()), |
|
180 | 6 | '%score%' => $this->timeFormater->timeToText($record->getScore(), true), |
|
181 | 6 | '%position%' => $position, |
|
182 | 6 | '%by%' => $securedBy, |
|
183 | ] |
||
184 | ); |
||
185 | 6 | } |
|
186 | |||
187 | /** |
||
188 | * @param Record $record |
||
189 | * @param Record $oldRecord |
||
190 | * @return string |
||
191 | */ |
||
192 | 11 | protected function getSecuredBy(Record $record, Record $oldRecord) |
|
210 | |||
211 | /** |
||
212 | * @param Record $record |
||
213 | * @throws \Propel\Runtime\Exception\PropelException |
||
214 | */ |
||
215 | 2 | protected function messageFirstPlaceNew(Record $record) |
|
227 | |||
228 | /** |
||
229 | * @param string $message |
||
230 | * @param null|string $recipient |
||
231 | * @param $params |
||
232 | */ |
||
233 | 14 | protected function sendMessage($message, $recipient, $params) |
|
241 | } |
||
242 |
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.