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 SurveyQuestion |
|
|
|||
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 string |
||
30 | * @Assert\NotBlank() |
||
31 | * @Assert\Type("string") |
||
32 | * @Assert\Length( |
||
33 | * max = 500 |
||
34 | * ) |
||
35 | * @ORM\Column(name="body", type="text") |
||
36 | */ |
||
37 | private $title; |
||
38 | |||
39 | /** |
||
40 | * @var SurveyType |
||
41 | * @Assert\Type("object") |
||
42 | * @Assert\Valid |
||
43 | * @ORM\ManyToOne(targetEntity="SurveyType", inversedBy="questions") |
||
44 | */ |
||
45 | private $surveyType; |
||
46 | |||
47 | /** |
||
48 | * @var ArrayCollection[Survey] |
||
49 | * @ORM\ManyToMany(targetEntity="Survey", inversedBy="questions") |
||
50 | */ |
||
51 | private $surveys; |
||
52 | |||
53 | /** |
||
54 | * @var ArrayCollection[SurveyAnswer] |
||
55 | * @ORM\OneToMany(targetEntity="SurveyAnswer", mappedBy="question") |
||
56 | */ |
||
57 | private $answers; |
||
58 | |||
59 | public function __construct() |
||
60 | { |
||
61 | $this->surveys = new ArrayCollection(); |
||
62 | $this->answers = new ArrayCollection(); |
||
63 | } |
||
64 | |||
65 | /** |
||
66 | * Get id. |
||
67 | * |
||
68 | * @return int |
||
69 | */ |
||
70 | public function getId() |
||
71 | { |
||
72 | return $this->id; |
||
73 | } |
||
74 | |||
75 | /** |
||
76 | * Set title. |
||
77 | * |
||
78 | * @param string $title |
||
79 | * |
||
80 | * @return SurveyQuestion |
||
81 | */ |
||
82 | public function setTitle($title) |
||
83 | { |
||
84 | $this->title = $title; |
||
85 | |||
86 | return $this; |
||
87 | } |
||
88 | |||
89 | /** |
||
90 | * Get title. |
||
91 | * |
||
92 | * @return string |
||
93 | */ |
||
94 | public function getTitle() |
||
95 | { |
||
96 | return $this->title; |
||
97 | } |
||
98 | |||
99 | /** |
||
100 | * Set type. |
||
101 | * |
||
102 | * @param SurveyType $type |
||
103 | * |
||
104 | * @return SurveyQuestion |
||
105 | */ |
||
106 | public function setSurveyType(SurveyType $type) |
||
107 | { |
||
108 | $this->surveyType = $type; |
||
109 | |||
110 | return $this; |
||
111 | } |
||
112 | |||
113 | /** |
||
114 | * Get type. |
||
115 | * |
||
116 | * @return SurveyType |
||
117 | */ |
||
118 | public function getSurveyType() |
||
119 | { |
||
120 | return $this->surveyType; |
||
121 | } |
||
122 | |||
123 | /** |
||
124 | * @param Survey $survey |
||
125 | * |
||
126 | * @return SurveyQuestion |
||
127 | */ |
||
128 | public function setSurveys(Survey $survey) |
||
137 | |||
138 | /** |
||
139 | * Get Surveys. |
||
140 | * |
||
141 | * @return ArrayCollection |
||
142 | */ |
||
143 | public function getSurveys() |
||
147 | |||
148 | /** |
||
149 | * Get Answers. |
||
150 | * |
||
151 | * @return ArrayCollection |
||
152 | */ |
||
153 | public function getAnswers() |
||
157 | } |
||
158 |
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.