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) |
|
| 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) |
|
| 203 | |||
| 204 | 11 | protected function getSecuredBy(Record $record, Record $oldRecord) |
|
| 218 | |||
| 219 | 2 | protected function messageFirstPlaceNew(Record $record) |
|
| 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: