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 |
||
| 18 | class ChatNotification implements RecordsDataListener |
||
| 19 | { |
||
| 20 | /** @var ChatNotificationHelper */ |
||
| 21 | protected $chatNotification; |
||
| 22 | |||
| 23 | /** @var PlayerStorage */ |
||
| 24 | protected $playerStorage; |
||
| 25 | |||
| 26 | /** @var Time */ |
||
| 27 | protected $timeFormater; |
||
| 28 | |||
| 29 | /** @var string */ |
||
| 30 | protected $translationPrefix; |
||
| 31 | |||
| 32 | /** @var int */ |
||
| 33 | protected $positionForPublicMessage; |
||
| 34 | |||
| 35 | /** |
||
| 36 | * ChatNotification constructor. |
||
| 37 | * |
||
| 38 | * @param ChatNotificationHelper $chatNotification |
||
| 39 | * @param PlayerStorage $playerStorage |
||
| 40 | * @param Time $timeFormater |
||
| 41 | * @param string $translationPrefix |
||
| 42 | * @param int $positionForPublicMessage |
||
| 43 | */ |
||
| 44 | 15 | public function __construct( |
|
| 57 | |||
| 58 | /** |
||
| 59 | * Called when local records are loaded. |
||
| 60 | * |
||
| 61 | * @param Record[] $records |
||
| 62 | */ |
||
| 63 | 2 | public function onLocalRecordsLoaded($records) |
|
| 87 | |||
| 88 | /** |
||
| 89 | * Called when a player finishes map for the very first time (basically first record). |
||
| 90 | * |
||
| 91 | * @param Record $record |
||
| 92 | * @param Record[] $records |
||
| 93 | * @param int $position |
||
| 94 | */ |
||
| 95 | 1 | public function onLocalRecordsFirstRecord(Record $record, $records, $position) |
|
| 99 | |||
| 100 | /** |
||
| 101 | * Called when a player finishes map and does same time as before. |
||
| 102 | * |
||
| 103 | * @param Record $record |
||
| 104 | * @param Record $oldRecord |
||
| 105 | * @param Record[] $records |
||
| 106 | * |
||
| 107 | * @return void |
||
| 108 | */ |
||
| 109 | 1 | public function onLocalRecordsSameScore(Record $record, Record $oldRecord, $records) |
|
| 113 | |||
| 114 | /** |
||
| 115 | * Called when a player finishes map with better time and has better position. |
||
| 116 | * |
||
| 117 | * @param Record $record |
||
| 118 | * @param Record $oldRecord |
||
| 119 | * @param Record[] $records |
||
| 120 | * @param int $position |
||
| 121 | * @param int $oldPosition |
||
| 122 | * |
||
| 123 | * @return void |
||
| 124 | */ |
||
| 125 | 6 | public function onLocalRecordsBetterPosition(Record $record, Record $oldRecord, $records, $position, $oldPosition) |
|
| 168 | |||
| 169 | /** |
||
| 170 | * Called when a player finishes map with better time but keeps same position. |
||
| 171 | * |
||
| 172 | * @param Record $record |
||
| 173 | * @param Record $oldRecord |
||
| 174 | * @param Record[] $records |
||
| 175 | * @param $position |
||
| 176 | * |
||
| 177 | * @return void |
||
| 178 | */ |
||
| 179 | 6 | public function onLocalRecordsSamePosition(Record $record, Record $oldRecord, $records, $position) |
|
| 211 | |||
| 212 | 11 | protected function getSecuredBy(Record $record, Record $oldRecord) |
|
| 213 | { |
||
| 214 | 11 | if ($oldRecord->getScore()) { |
|
| 215 | 11 | $securedBy = $this->timeFormater->timeToText($oldRecord->getScore() - $record->getScore(), true); |
|
| 216 | |||
| 217 | 11 | if (substr($securedBy, 0, 4) === "00:0") { |
|
| 218 | 1 | $securedBy = substr($securedBy, 4); |
|
| 219 | 1 | } else { |
|
| 220 | 10 | if (substr($securedBy, 0, 3) === "00:") { |
|
| 221 | 1 | $securedBy = substr($securedBy, 3); |
|
| 222 | 1 | } |
|
| 223 | } |
||
| 224 | |||
| 225 | 11 | return '-' . $securedBy; |
|
| 226 | } |
||
| 227 | |||
| 228 | return $securedBy = $this->timeFormater->timeToText(0); |
||
| 229 | } |
||
| 230 | |||
| 231 | 2 | protected function messageFirstPlaceNew(Record $record) |
|
| 232 | { |
||
| 233 | 2 | $this->sendMessage( |
|
| 234 | 2 | 'new.top1', |
|
| 235 | 2 | null, |
|
| 236 | [ |
||
| 237 | 2 | '%nickname%' => $this->playerStorage->getPlayerInfo($record->getPlayerLogin())->getNickName(), |
|
| 238 | 2 | '%score%' => $this->timeFormater->timeToText($record->getScore(), true), |
|
| 239 | 2 | '%position%' => 1, |
|
| 240 | ] |
||
| 241 | 2 | ); |
|
| 242 | 2 | } |
|
| 243 | |||
| 244 | /** |
||
| 245 | * @param string $message |
||
| 246 | * @param null|integer $recipe |
||
| 247 | */ |
||
| 248 | 14 | protected function sendMessage($message, $recipe, $params) |
|
| 256 | } |
||
| 257 |
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.