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) |
|
| 64 | { |
||
| 65 | 2 | if (!empty($records)) { |
|
| 66 | 1 | $firstRecord = $records[0]; |
|
| 67 | |||
| 68 | 1 | $this->sendMessage('loaded.top1', null, [ |
|
| 69 | 1 | '%nickname%' => $firstRecord->getPlayerLogin(), // TODO get player nickname from database. |
|
| 70 | 1 | '%score%' => $this->timeFormater->milisecondsToTrackmania($firstRecord->getScore(), true) |
|
| 71 | ]); |
||
| 72 | |||
| 73 | |||
| 74 | 1 | $onlinePlayers = $this->playerStorage->getOnline(); |
|
| 75 | 1 | for ($i = 1; $i < count($records); $i++) { |
|
|
|
|||
| 76 | 1 | if (isset($onlinePlayers[$records[$i]->getPlayerLogin()])) { |
|
| 77 | 1 | $this->sendMessage('loaded.any', $records[$i]->getPlayerLogin(), [ |
|
| 78 | 1 | '%nickname%' => $onlinePlayers[$records[$i]->getPlayerLogin()]->getNickName(), |
|
| 79 | 1 | '%score%' => $this->timeFormater->milisecondsToTrackmania($records[$i]->getScore(), true), |
|
| 80 | 1 | '%position%' => $i+1, |
|
| 81 | ]); |
||
| 82 | } |
||
| 83 | } |
||
| 84 | } |
||
| 85 | 2 | } |
|
| 86 | |||
| 87 | /** |
||
| 88 | * Called when a player finishes map for the very first time (basically first record). |
||
| 89 | * |
||
| 90 | * @param Record $record |
||
| 91 | * @param Record[] $records |
||
| 92 | * @param $position |
||
| 93 | */ |
||
| 94 | 1 | public function onLocalRecordsFirstRecord(Record $record, $records, $position) |
|
| 98 | |||
| 99 | /** |
||
| 100 | * Called when a player finishes map and does same time as before. |
||
| 101 | * |
||
| 102 | * @param Record $record |
||
| 103 | * @param Record $oldRecord |
||
| 104 | * @param Record[] $records |
||
| 105 | * |
||
| 106 | * @return mixed |
||
| 107 | */ |
||
| 108 | 1 | public function onLocalRecordsSameScore(Record $record, Record $oldRecord, $records) |
|
| 112 | |||
| 113 | /** |
||
| 114 | * Called when a player finishes map with better time and has better position. |
||
| 115 | * |
||
| 116 | * @param Record $record |
||
| 117 | * @param Record $oldRecord |
||
| 118 | * @param Record[] $records |
||
| 119 | * @param int $position |
||
| 120 | * @param int $oldPosition |
||
| 121 | */ |
||
| 122 | 6 | public function onLocalRecordsBetterPosition(Record $record, Record $oldRecord, $records, $position, $oldPosition) |
|
| 123 | { |
||
| 124 | 6 | if ($position == 1 && $oldPosition == null) { |
|
| 125 | 1 | return $this->messageFirstPlaceNew($record); |
|
| 126 | } |
||
| 127 | |||
| 128 | // Check to who to send. |
||
| 129 | 5 | $to = null; |
|
| 130 | 5 | if ($position > $this->positionForPublicMessage) { |
|
| 131 | 1 | $to = $record->getPlayerLogin(); |
|
| 132 | } |
||
| 133 | |||
| 134 | // Check which message to send. |
||
| 135 | 5 | $msg = 'better'; |
|
| 136 | 5 | if ($oldPosition == null) { |
|
| 137 | 1 | $msg = 'new'; |
|
| 138 | } |
||
| 139 | |||
| 140 | // Check for top status |
||
| 141 | 5 | View Code Duplication | if ($position == 1) { |
| 142 | 1 | $msg .= '.top1'; |
|
| 143 | 4 | } else if ($position <= 5) { |
|
| 144 | 1 | $msg .= '.top5'; |
|
| 145 | } else { |
||
| 146 | 3 | $msg .= '.any'; |
|
| 147 | } |
||
| 148 | |||
| 149 | 5 | $securedBy = $this->getSecuredBy($record, $oldRecord); |
|
| 150 | 5 | $this->sendMessage( |
|
| 151 | 5 | $msg, |
|
| 152 | 5 | $to, |
|
| 153 | [ |
||
| 154 | 5 | '%nickname%' => $this->playerStorage->getPlayerInfo($record->getPlayerLogin())->getNickName(), |
|
| 155 | 5 | '%score%' => $this->timeFormater->milisecondsToTrackmania($record->getScore(), true), |
|
| 156 | 5 | '%position%' => $position, |
|
| 157 | 5 | '%old_position%' => $oldPosition, |
|
| 158 | 5 | '%by%' => $securedBy |
|
| 159 | ] |
||
| 160 | ); |
||
| 161 | 5 | } |
|
| 162 | |||
| 163 | /** |
||
| 164 | * Called when a player finishes map with better time but keeps same position. |
||
| 165 | * |
||
| 166 | * @param Record $record |
||
| 167 | * @param Record $oldRecord |
||
| 168 | * @param Record[] $records |
||
| 169 | * @param $position |
||
| 170 | * |
||
| 171 | * @return mixed |
||
| 172 | */ |
||
| 173 | 6 | public function onLocalRecordsSamePosition(Record $record, Record $oldRecord, $records, $position) |
|
| 174 | { |
||
| 175 | // Check to who to send. |
||
| 176 | 6 | $to = null; |
|
| 177 | 6 | if ($position > $this->positionForPublicMessage) { |
|
| 178 | 3 | $to = $record->getPlayerLogin(); |
|
| 179 | } |
||
| 180 | |||
| 181 | // Check which message to send. |
||
| 182 | 6 | $msg = 'secures'; |
|
| 183 | 6 | View Code Duplication | if ($position == 1) { |
| 184 | 1 | $msg .= '.top1'; |
|
| 185 | 5 | } else if ($position <= 5) { |
|
| 186 | 1 | $msg .= '.top5'; |
|
| 187 | } else { |
||
| 188 | 4 | $msg .= '.any'; |
|
| 189 | } |
||
| 190 | |||
| 191 | 6 | $securedBy = $this->getSecuredBy($record, $oldRecord); |
|
| 192 | 6 | $this->sendMessage( |
|
| 193 | 6 | $msg, |
|
| 194 | 6 | $to, |
|
| 195 | [ |
||
| 196 | 6 | '%nickname%' => $this->playerStorage->getPlayerInfo($record->getPlayerLogin())->getNickName(), |
|
| 197 | 6 | '%score%' => $this->timeFormater->milisecondsToTrackmania($record->getScore(), true), |
|
| 198 | 6 | '%position%' => $position, |
|
| 199 | 6 | '%by%' => $securedBy |
|
| 200 | ] |
||
| 201 | ); |
||
| 202 | 6 | } |
|
| 203 | |||
| 204 | 11 | protected function getSecuredBy(Record $record, Record $oldRecord) |
|
| 205 | { |
||
| 206 | 11 | $securedBy =$this->timeFormater->milisecondsToTrackmania($oldRecord->getScore() - $record->getScore(), true); |
|
| 207 | |||
| 208 | 11 | if (substr($securedBy, 0, 4) === "00:0") { |
|
| 209 | 1 | $securedBy = substr($securedBy, 4); |
|
| 210 | } else { |
||
| 211 | 10 | if (substr($securedBy, 0, 3) === "00:") { |
|
| 212 | 1 | $securedBy = substr($securedBy, 3); |
|
| 213 | } |
||
| 214 | } |
||
| 215 | |||
| 216 | 11 | return '-' . $securedBy; |
|
| 217 | } |
||
| 218 | |||
| 219 | 2 | protected function messageFirstPlaceNew(Record $record) |
|
| 220 | { |
||
| 221 | 2 | $this->sendMessage( |
|
| 222 | 2 | 'new.top1', |
|
| 223 | 2 | null, |
|
| 224 | [ |
||
| 225 | 2 | '%nickname%' => $this->playerStorage->getPlayerInfo($record->getPlayerLogin())->getNickName(), |
|
| 226 | 2 | '%score%' => $this->timeFormater->milisecondsToTrackmania($record->getScore(), true), |
|
| 227 | 2 | '%position%' => 1 |
|
| 228 | ] |
||
| 229 | ); |
||
| 230 | 2 | } |
|
| 231 | |||
| 232 | 14 | protected function sendMessage($message, $recipe, $params) |
|
| 240 | } |
||
| 241 |
If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration: