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 |
||
| 9 | class TradingCard extends Game implements ServiceManagerAwareInterface |
||
| 10 | { |
||
| 11 | protected $tradingcardMapper; |
||
| 12 | protected $tradingcardmodelMapper; |
||
| 13 | protected $tradingcardcardMapper; |
||
| 14 | |||
| 15 | View Code Duplication | public function getModelPath($model) |
|
|
|
|||
| 16 | { |
||
| 17 | $path = $this->getOptions()->getMediaPath() . DIRECTORY_SEPARATOR; |
||
| 18 | $path .= 'game' . $model->getGame()->getId() . DIRECTORY_SEPARATOR; |
||
| 19 | if (!is_dir($path)) { |
||
| 20 | mkdir($path, 0777, true); |
||
| 21 | } |
||
| 22 | $path .= 'model'. $model->getId() . DIRECTORY_SEPARATOR; |
||
| 23 | if (!is_dir($path)) { |
||
| 24 | mkdir($path, 0777, true); |
||
| 25 | } |
||
| 26 | |||
| 27 | return $path; |
||
| 28 | } |
||
| 29 | |||
| 30 | View Code Duplication | public function getModelMediaUrl($model) |
|
| 31 | { |
||
| 32 | $media_url = $this->getOptions()->getMediaUrl() . '/'; |
||
| 33 | $media_url .= 'game' . $model->getGame()->getId() . '/' . 'model'. $model->getId() . '/'; |
||
| 34 | |||
| 35 | return $media_url; |
||
| 36 | } |
||
| 37 | |||
| 38 | /** |
||
| 39 | * @param array $data |
||
| 40 | * @return \PlaygroundGame\Entity\Game |
||
| 41 | */ |
||
| 42 | public function updateModel(array $data, $model) |
||
| 43 | { |
||
| 44 | $form = $this->getServiceManager()->get('playgroundgame_tradingcardmodel_form'); |
||
| 45 | $tradingcard = $this->getGameMapper()->findById($data['trading_card_id']); |
||
| 46 | $model->setGame($tradingcard); |
||
| 47 | $path = $this->getModelPath($model); |
||
| 48 | $media_url = $this->getModelMediaUrl($model); |
||
| 49 | |||
| 50 | $form->bind($model); |
||
| 51 | $form->setData($data); |
||
| 52 | |||
| 53 | if (!$form->isValid()) { |
||
| 54 | return false; |
||
| 55 | } |
||
| 56 | |||
| 57 | View Code Duplication | if (!empty($data['upload_image']['tmp_name'])) { |
|
| 58 | ErrorHandler::start(); |
||
| 59 | $data['upload_image']['name'] = $this->fileNewname( |
||
| 60 | $path, |
||
| 61 | $model->getId() . "-" . $data['upload_image']['name'] |
||
| 62 | ); |
||
| 63 | move_uploaded_file($data['upload_image']['tmp_name'], $path . $data['upload_image']['name']); |
||
| 64 | $model->setImage($media_url . $data['upload_image']['name']); |
||
| 65 | ErrorHandler::stop(true); |
||
| 66 | } |
||
| 67 | |||
| 68 | // if (isset($data['delete_image']) && empty($data['upload_image']['tmp_name'])) { |
||
| 69 | // ErrorHandler::start(); |
||
| 70 | // $image = $model->getImage(); |
||
| 71 | // $image = str_replace($media_url, '', $image); |
||
| 72 | // if (file_exists($path .$image)) { |
||
| 73 | // unlink($path .$image); |
||
| 74 | // } |
||
| 75 | // $model->setImage(null); |
||
| 76 | // ErrorHandler::stop(true); |
||
| 77 | // } |
||
| 78 | |||
| 79 | $this->getTradingCardModelMapper()->update($model); |
||
| 80 | $this->getEventManager()->trigger( |
||
| 81 | __FUNCTION__.'.post', |
||
| 82 | $this, |
||
| 83 | array('model' => $model, 'data' => $data) |
||
| 84 | ); |
||
| 85 | |||
| 86 | return $model; |
||
| 87 | } |
||
| 88 | |||
| 89 | public function getBooster($game, $user, $entry) |
||
| 145 | |||
| 146 | public function getAlbum($game, $user) |
||
| 166 | |||
| 167 | public function getGameEntity() |
||
| 171 | |||
| 172 | public function getTradingCardMapper() |
||
| 180 | |||
| 181 | public function getTradingCardCardMapper() |
||
| 189 | |||
| 190 | public function getTradingCardModelMapper() |
||
| 198 | } |
||
| 199 |
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.