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 ManialinkFactory implements ManialinkFactoryInterface |
||
| 21 | { |
||
| 22 | /** @var GuiHandler */ |
||
| 23 | protected $guiHandler; |
||
| 24 | |||
| 25 | /** @var Factory */ |
||
| 26 | protected $groupFactory; |
||
| 27 | |||
| 28 | /** @var ActionFactory */ |
||
| 29 | protected $actionFactory; |
||
| 30 | |||
| 31 | /** @var string */ |
||
| 32 | protected $name; |
||
| 33 | |||
| 34 | /** @var string */ |
||
| 35 | protected $className; |
||
| 36 | |||
| 37 | /** @var Group[] */ |
||
| 38 | protected $groups = []; |
||
| 39 | |||
| 40 | /** @var float|int */ |
||
| 41 | protected $sizeX; |
||
| 42 | |||
| 43 | /** @var float|int */ |
||
| 44 | protected $sizeY; |
||
| 45 | |||
| 46 | /** @var float|int */ |
||
| 47 | protected $posX; |
||
| 48 | |||
| 49 | /** @var float|int */ |
||
| 50 | protected $posY; |
||
| 51 | |||
| 52 | /** |
||
| 53 | * ManialinkFactory constructor. |
||
| 54 | * |
||
| 55 | * @param $name |
||
| 56 | * @param $sizeX |
||
| 57 | * @param $sizeY |
||
| 58 | * @param null $posX |
||
| 59 | * @param null $posY |
||
| 60 | * @param ManialinkFactoryContext $context |
||
| 61 | */ |
||
| 62 | 4 | public function __construct( |
|
| 88 | |||
| 89 | /** |
||
| 90 | * @return string |
||
| 91 | */ |
||
| 92 | public function getId() |
||
| 96 | |||
| 97 | /** |
||
| 98 | * Creates a new manialink. |
||
| 99 | * |
||
| 100 | * @param string|array|Group $group |
||
| 101 | * |
||
| 102 | * @return Group |
||
| 103 | */ |
||
| 104 | 4 | public function create($group) |
|
| 105 | { |
||
| 106 | 4 | View Code Duplication | if (is_string($group)) { |
|
|
|||
| 107 | 1 | $group = $this->groupFactory->createForPlayer($group); |
|
| 108 | } else { |
||
| 109 | 3 | if (is_array($group)) { |
|
| 110 | 2 | $group = $this->groupFactory->createForPlayers($group); |
|
| 111 | } |
||
| 112 | } |
||
| 113 | |||
| 114 | 4 | if (!is_null($this->guiHandler->getManialink($group, $this))) { |
|
| 115 | 1 | $this->update($group); |
|
| 116 | |||
| 117 | 1 | return $group; |
|
| 118 | } |
||
| 119 | |||
| 120 | |||
| 121 | 3 | $ml = $this->createManialink($group); |
|
| 122 | 3 | $this->guiHandler->addToDisplay($ml, $this); |
|
| 123 | |||
| 124 | 3 | $this->createContent($ml); |
|
| 125 | 3 | $this->updateContent($ml); |
|
| 126 | |||
| 127 | 3 | return $group; |
|
| 128 | } |
||
| 129 | |||
| 130 | /** |
||
| 131 | * Request an update for manialink. |
||
| 132 | * |
||
| 133 | * @param string|array|Group $group |
||
| 134 | */ |
||
| 135 | 1 | public function update($group) |
|
| 136 | { |
||
| 137 | 1 | View Code Duplication | if (is_string($group)) { |
| 138 | $group = $this->groupFactory->createForPlayer($group); |
||
| 139 | } else { |
||
| 140 | 1 | if (is_array($group)) { |
|
| 141 | $group = $this->groupFactory->createForPlayers($group); |
||
| 142 | } |
||
| 143 | } |
||
| 144 | |||
| 145 | 1 | $ml = $this->guiHandler->getManialink($group, $this); |
|
| 146 | 1 | if ($ml) { |
|
| 147 | 1 | $this->actionFactory->destroyNotPermanentActions($ml); |
|
| 148 | 1 | if ($ml instanceof Window) { |
|
| 149 | $ml->busyCounter += 1; |
||
| 150 | |||
| 151 | if ($ml->isBusy && $ml->busyCounter > 1) { |
||
| 152 | $ml->setBusy(false); |
||
| 153 | } |
||
| 154 | } |
||
| 155 | 1 | $this->updateContent($ml); |
|
| 156 | 1 | $this->guiHandler->addToDisplay($ml, $this); |
|
| 157 | } |
||
| 158 | 1 | } |
|
| 159 | |||
| 160 | /** |
||
| 161 | * Create content in the manialink. |
||
| 162 | * |
||
| 163 | * @param ManialinkInterface $manialink |
||
| 164 | * |
||
| 165 | */ |
||
| 166 | 3 | protected function createContent(ManialinkInterface $manialink) |
|
| 170 | |||
| 171 | /** |
||
| 172 | * Update content in the manialink. |
||
| 173 | * |
||
| 174 | * @param ManialinkInterface $manialink |
||
| 175 | * |
||
| 176 | */ |
||
| 177 | 4 | protected function updateContent(ManialinkInterface $manialink) |
|
| 181 | |||
| 182 | /** |
||
| 183 | * Hides and frees manialink resources |
||
| 184 | * |
||
| 185 | * @param Group $group |
||
| 186 | * |
||
| 187 | */ |
||
| 188 | 1 | public function destroy(Group $group) |
|
| 195 | |||
| 196 | /** |
||
| 197 | * Create manialink object for user group. |
||
| 198 | * |
||
| 199 | * @param Group $group |
||
| 200 | * |
||
| 201 | * @return Manialink |
||
| 202 | */ |
||
| 203 | 3 | protected function createManialink(Group $group) |
|
| 209 | |||
| 210 | /** |
||
| 211 | * @return float|int |
||
| 212 | */ |
||
| 213 | public function getSizeX() |
||
| 217 | |||
| 218 | /** |
||
| 219 | * @return float|int |
||
| 220 | */ |
||
| 221 | public function getSizeY() |
||
| 225 | |||
| 226 | /** |
||
| 227 | * @return float|int |
||
| 228 | */ |
||
| 229 | public function getPosX() |
||
| 233 | |||
| 234 | /** |
||
| 235 | * @return float|int |
||
| 236 | */ |
||
| 237 | public function getPosY() |
||
| 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.