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 |
||
17 | View Code Duplication | class Artist |
|
|
|||
18 | { |
||
19 | use TimestampableEntity; |
||
20 | |||
21 | /** |
||
22 | * @var integer |
||
23 | * |
||
24 | * @ORM\Column(name="id", type="integer") |
||
25 | * @ORM\Id |
||
26 | * @ORM\GeneratedValue(strategy="AUTO") |
||
27 | */ |
||
28 | private $id; |
||
29 | |||
30 | /** |
||
31 | * @var string |
||
32 | * |
||
33 | * @ORM\Column(name="name", type="string", length=200, nullable=false) |
||
34 | * @Groups({"artist-read", "media-read"}) |
||
35 | */ |
||
36 | private $name; |
||
37 | /** |
||
38 | * @ORM\ManyToMany(targetEntity="\AudioCoreEntity\Entity\Media",mappedBy="artists") |
||
39 | * @Groups({"artist-read"}) |
||
40 | **/ |
||
41 | private $medias; |
||
42 | |||
43 | /** |
||
44 | * @var string |
||
45 | * @Gedmo\Slug(fields={"name"}, unique=true) |
||
46 | * @ORM\Column(type="string", length=128, unique=true) |
||
47 | */ |
||
48 | protected $slug; |
||
49 | |||
50 | /** |
||
51 | * Artist constructor. |
||
52 | * @param null $name |
||
53 | */ |
||
54 | 2 | public function __construct($name = null) |
|
59 | |||
60 | /** |
||
61 | * Get id |
||
62 | * |
||
63 | * @return integer |
||
64 | */ |
||
65 | 1 | public function getId() |
|
69 | |||
70 | /** |
||
71 | * @return string |
||
72 | */ |
||
73 | 1 | public function getName() |
|
77 | |||
78 | /** |
||
79 | * @param string $name |
||
80 | * @return $this |
||
81 | */ |
||
82 | 2 | public function setName($name) |
|
87 | |||
88 | /** |
||
89 | * @return mixed |
||
90 | */ |
||
91 | 1 | public function getMedias() |
|
95 | |||
96 | /** |
||
97 | * @param mixed $medias |
||
98 | * @return Artist |
||
99 | */ |
||
100 | 1 | public function setMedias($medias) |
|
105 | |||
106 | /** |
||
107 | * @return string |
||
108 | */ |
||
109 | public function getSlug() |
||
113 | } |
||
114 |
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.