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 |
||
22 | abstract class Ambassador |
||
23 | { |
||
24 | use CreatorLoginTrait; |
||
25 | |||
26 | /** |
||
27 | * @var string |
||
28 | * @ORM\Id |
||
29 | * @ORM\Column(name="id", type="string", length=8, unique=true) |
||
30 | */ |
||
31 | protected $id; |
||
32 | |||
33 | /** |
||
34 | * @var string |
||
35 | * @ORM\Column(name="name", type="string", length=255, unique=true) |
||
36 | */ |
||
37 | protected $name; |
||
38 | |||
39 | /** |
||
40 | * @var \DateTime |
||
41 | * @ORM\Column(name="registration_date", type="datetime") |
||
42 | */ |
||
43 | protected $registrationDate; |
||
44 | |||
45 | /** |
||
46 | * @var string |
||
47 | * @ORM\Column(name="description", type="text", nullable=true) |
||
48 | */ |
||
49 | protected $description; |
||
50 | |||
51 | /** @var ArrayCollection */ |
||
52 | protected $members; |
||
53 | |||
54 | /** |
||
55 | * @var User |
||
56 | * @ORM\ManyToOne(targetEntity="AppBundle\Entity\User", inversedBy="createdBands") |
||
57 | * @ORM\JoinColumn(name="creator", referencedColumnName="login") |
||
58 | * @Accessor(getter="getCreatorLogin") |
||
59 | * @SerializerType("string") |
||
60 | */ |
||
61 | protected $creator; |
||
62 | |||
63 | 4 | View Code Duplication | public function __construct( |
79 | |||
80 | 2 | public function getId(): string |
|
84 | |||
85 | /** |
||
86 | * @return Collection|BandMember[] |
||
87 | */ |
||
88 | 11 | public function getMembers(): Collection |
|
92 | |||
93 | 2 | public function addMember(BandMember $bandMember) |
|
97 | |||
98 | 1 | public function removeMember(BandMember $bandMember) |
|
102 | |||
103 | 1 | public function setName(string $name) |
|
107 | |||
108 | 1 | public function setDescription(string $description) |
|
112 | |||
113 | 10 | public function getCreator(): User |
|
117 | |||
118 | 4 | public function getMemberClass(): string |
|
128 | } |
||
129 |
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.