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 | View Code Duplication | class ArticleTranslation |
|
|
|
|||
| 19 | { |
||
| 20 | use Translation; |
||
| 21 | |||
| 22 | /** |
||
| 23 | * @var string |
||
| 24 | * |
||
| 25 | * @Assert\NotBlank() |
||
| 26 | * @ORM\Column(name="name", type="string", length=255) |
||
| 27 | * @Serializer\Groups({"search"}) |
||
| 28 | * @VIC\BusinessProperty({"textable", "businessParameter", "seoable"}) |
||
| 29 | */ |
||
| 30 | protected $name; |
||
| 31 | |||
| 32 | /** |
||
| 33 | * @var string |
||
| 34 | * |
||
| 35 | * @Gedmo\Slug(handlers={ |
||
| 36 | * @Gedmo\SlugHandler(class="Victoire\Bundle\BusinessEntityBundle\Handler\TwigSlugHandler" |
||
| 37 | * )},fields={"name"}, updatable=false, unique=false) |
||
| 38 | * @ORM\Column(name="slug", type="string", length=255) |
||
| 39 | * @VIC\BusinessProperty("businessParameter") |
||
| 40 | */ |
||
| 41 | protected $slug; |
||
| 42 | |||
| 43 | /** |
||
| 44 | * @var string |
||
| 45 | * |
||
| 46 | * @ORM\Column(name="description", type="text", nullable=true) |
||
| 47 | * @VIC\BusinessProperty({"textable", "seoable"}) |
||
| 48 | */ |
||
| 49 | private $description; |
||
| 50 | |||
| 51 | /** |
||
| 52 | * Get name. |
||
| 53 | * |
||
| 54 | * @return string |
||
| 55 | */ |
||
| 56 | public function getName() |
||
| 60 | |||
| 61 | /** |
||
| 62 | * Set name. |
||
| 63 | * |
||
| 64 | * @param string $name |
||
| 65 | * |
||
| 66 | * @return View |
||
| 67 | */ |
||
| 68 | public function setName($name) |
||
| 74 | |||
| 75 | /** |
||
| 76 | * Set slug. |
||
| 77 | * |
||
| 78 | * @param string $slug |
||
| 79 | * |
||
| 80 | * @return View |
||
| 81 | */ |
||
| 82 | public function setSlug($slug) |
||
| 88 | |||
| 89 | /** |
||
| 90 | * Get slug. |
||
| 91 | * |
||
| 92 | * @return string |
||
| 93 | */ |
||
| 94 | public function getSlug() |
||
| 98 | |||
| 99 | /** |
||
| 100 | * Get description. |
||
| 101 | * |
||
| 102 | * @return string |
||
| 103 | */ |
||
| 104 | public function getDescription() |
||
| 108 | |||
| 109 | /** |
||
| 110 | * Set category. |
||
| 111 | * |
||
| 112 | * @param string $category |
||
| 113 | * |
||
| 114 | * @return Article |
||
| 115 | */ |
||
| 116 | public function setDescription($description) |
||
| 122 | |||
| 123 | } |
||
| 124 |
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.