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 |
||
| 21 | View Code Duplication | class Memory extends Game implements InputFilterAwareInterface |
|
|
|
|||
| 22 | { |
||
| 23 | const CLASSTYPE = 'memory'; |
||
| 24 | |||
| 25 | /** |
||
| 26 | * the cards associated with the game |
||
| 27 | * @ORM\OneToMany(targetEntity="MemoryCard", mappedBy="game") |
||
| 28 | */ |
||
| 29 | protected $cards; |
||
| 30 | |||
| 31 | /** |
||
| 32 | * @ORM\Column(name="back_image", type="string", length=255, nullable=true) |
||
| 33 | */ |
||
| 34 | protected $backImage; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * @ORM\Column(name="victory_conditions", type="integer", nullable=false) |
||
| 38 | */ |
||
| 39 | protected $victoryConditions = 0; |
||
| 40 | |||
| 41 | public function __construct() |
||
| 47 | |||
| 48 | /** |
||
| 49 | * Gets the the cards associated with the game. |
||
| 50 | * |
||
| 51 | * @return mixed |
||
| 52 | */ |
||
| 53 | public function getCards() |
||
| 57 | |||
| 58 | /** |
||
| 59 | * Sets the the cards associated with the game. |
||
| 60 | * |
||
| 61 | * @param mixed $cards the cards |
||
| 62 | * |
||
| 63 | * @return self |
||
| 64 | */ |
||
| 65 | protected function setCards($cards) |
||
| 71 | |||
| 72 | /** |
||
| 73 | * Add a card to the trading card. |
||
| 74 | * |
||
| 75 | * |
||
| 76 | * @return void |
||
| 77 | */ |
||
| 78 | public function addCard($card) |
||
| 82 | |||
| 83 | /** |
||
| 84 | * |
||
| 85 | * @return the $backImage |
||
| 86 | */ |
||
| 87 | public function getBackImage() |
||
| 91 | |||
| 92 | /** |
||
| 93 | * |
||
| 94 | * @param field_type $backImage |
||
| 95 | */ |
||
| 96 | public function setBackImage($backImage) |
||
| 102 | |||
| 103 | public function getVictoryConditions() |
||
| 107 | |||
| 108 | /** |
||
| 109 | */ |
||
| 110 | public function setVictoryConditions($victoryConditions) |
||
| 116 | |||
| 117 | /** |
||
| 118 | * Convert the object to an array. |
||
| 119 | * |
||
| 120 | * @return array |
||
| 121 | */ |
||
| 122 | public function getArrayCopy() |
||
| 129 | |||
| 130 | public function setInputFilter(InputFilterInterface $inputFilter) |
||
| 134 | |||
| 135 | public function getInputFilter() |
||
| 147 | } |
||
| 148 |
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.