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 |
||
11 | View Code Duplication | class AbstractTag extends AbstractEntity implements Translatable |
|
|
|||
12 | { |
||
13 | /** |
||
14 | * AbstractTag constructor. |
||
15 | */ |
||
16 | public function __construct() |
||
22 | |||
23 | /** |
||
24 | * @var string |
||
25 | * |
||
26 | * @ORM\Column(name="name", type="string", length=255) |
||
27 | * @Assert\NotBlank() |
||
28 | * @Gedmo\Translatable |
||
29 | */ |
||
30 | protected $name; |
||
31 | |||
32 | /** |
||
33 | * @Gedmo\Locale |
||
34 | */ |
||
35 | protected $locale; |
||
36 | |||
37 | /** |
||
38 | * @var \DateTime |
||
39 | * |
||
40 | * @ORM\Column(name="deleted_at", type="datetime", nullable=true) |
||
41 | */ |
||
42 | protected $deletedAt; |
||
43 | |||
44 | /** |
||
45 | * Set name |
||
46 | * |
||
47 | * @param string $name |
||
48 | */ |
||
49 | public function setName($name) |
||
53 | |||
54 | /** |
||
55 | * Get name |
||
56 | * |
||
57 | * @return string |
||
58 | */ |
||
59 | public function getName() |
||
63 | |||
64 | /** |
||
65 | * @return mixed |
||
66 | */ |
||
67 | public function getLocale() |
||
71 | |||
72 | /** |
||
73 | * @param mixed $locale |
||
74 | */ |
||
75 | public function setLocale($locale) |
||
79 | |||
80 | /** |
||
81 | * @return \DateTime |
||
82 | */ |
||
83 | public function getDeletedAt() |
||
87 | |||
88 | /** |
||
89 | * @param \DateTime $deletedAt |
||
90 | */ |
||
91 | public function setDeletedAt($deletedAt) |
||
95 | |||
96 | /** |
||
97 | * @return string |
||
98 | */ |
||
99 | public function __toString() |
||
103 | } |
||
104 |
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.