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 |
||
15 | View Code Duplication | class Survey |
|
|
|||
16 | { |
||
17 | use ORMBehaviors\Timestampable\Timestampable; |
||
18 | |||
19 | /** |
||
20 | * @var int |
||
21 | * |
||
22 | * @ORM\Column(type="integer") |
||
23 | * @ORM\Id |
||
24 | * @ORM\GeneratedValue(strategy="AUTO") |
||
25 | */ |
||
26 | private $id; |
||
27 | |||
28 | /** |
||
29 | * @var SurveyType |
||
30 | * @Assert\Type("object") |
||
31 | * @Assert\Valid |
||
32 | * @ORM\ManyToOne(targetEntity="SurveyType", inversedBy="surveys") |
||
33 | */ |
||
34 | private $type; |
||
35 | |||
36 | /** |
||
37 | * @var User |
||
38 | * @Assert\Type("object") |
||
39 | * @Assert\Valid |
||
40 | * @ORM\ManyToOne(targetEntity="User", inversedBy="surveys") |
||
41 | */ |
||
42 | private $user; |
||
43 | |||
44 | /** |
||
45 | * @var ArrayCollection[SurveyQuestion] |
||
46 | * @ORM\ManyToMany(targetEntity="SurveyQuestion", mappedBy="surveys") |
||
47 | */ |
||
48 | private $questions; |
||
49 | |||
50 | /** |
||
51 | * @var ArrayCollection[SurveyAnswer] |
||
52 | * @ORM\OneToMany(targetEntity="SurveyAnswer", mappedBy="survey") |
||
53 | */ |
||
54 | private $answers; |
||
55 | |||
56 | public function __construct() |
||
61 | |||
62 | /** |
||
63 | * Get id. |
||
64 | * |
||
65 | * @return int |
||
66 | */ |
||
67 | public function getId() |
||
71 | |||
72 | /** |
||
73 | * Set type. |
||
74 | * |
||
75 | * @param SurveyType $type |
||
76 | * |
||
77 | * @return Survey |
||
78 | */ |
||
79 | public function setType(SurveyType $type) |
||
85 | |||
86 | /** |
||
87 | * Get type. |
||
88 | * |
||
89 | * @return SurveyType |
||
90 | */ |
||
91 | public function getType() |
||
95 | |||
96 | /** |
||
97 | * Set user. |
||
98 | * |
||
99 | * @param User $user |
||
100 | * |
||
101 | * @return Survey |
||
102 | */ |
||
103 | public function setUser(User $user) |
||
109 | |||
110 | /** |
||
111 | * Get user. |
||
112 | * |
||
113 | * @return User |
||
114 | */ |
||
115 | public function getUser() |
||
119 | |||
120 | /** |
||
121 | * @param SurveyQuestion $question |
||
122 | * |
||
123 | * @return Survey |
||
124 | */ |
||
125 | public function setQuestions(SurveyQuestion $question) |
||
134 | |||
135 | /** |
||
136 | * Get Questions. |
||
137 | * |
||
138 | * @return ArrayCollection |
||
139 | */ |
||
140 | public function getQuestions() |
||
144 | |||
145 | /** |
||
146 | * Get Questions. |
||
147 | * |
||
148 | * @return ArrayCollection |
||
149 | */ |
||
150 | public function getAnswers() |
||
151 | { |
||
152 | return $this->answers; |
||
153 | } |
||
154 | } |
||
155 |
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.