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 |
||
20 | View Code Duplication | class SingleLineTextPagePart extends AbstractFormPagePart |
|
|
|||
21 | { |
||
22 | /** |
||
23 | * If set to true, you are obligated to fill in this page part |
||
24 | * |
||
25 | * @ORM\Column(type="boolean", nullable=true) |
||
26 | */ |
||
27 | protected $required = false; |
||
28 | |||
29 | /** |
||
30 | * Error message shows when the page part is required and nothing is filled in |
||
31 | * |
||
32 | * @ORM\Column(type="string", name="error_message_required", nullable=true) |
||
33 | */ |
||
34 | protected $errorMessageRequired; |
||
35 | |||
36 | /** |
||
37 | * If set the entered value will be matched with this regular expression |
||
38 | * |
||
39 | * @ORM\Column(type="string", nullable=true) |
||
40 | */ |
||
41 | protected $regex; |
||
42 | |||
43 | /** |
||
44 | * If a regular expression is set and it doesn't match with the given value, this error message will be shown |
||
45 | * |
||
46 | * @ORM\Column(type="string", name="error_message_regex", nullable=true) |
||
47 | */ |
||
48 | protected $errorMessageRegex; |
||
49 | |||
50 | /** |
||
51 | * Sets the required valud of this page part |
||
52 | * |
||
53 | * @param bool $required |
||
54 | * |
||
55 | * @return SingleLineTextPagePart |
||
56 | */ |
||
57 | public function setRequired($required) |
||
63 | |||
64 | /** |
||
65 | * Check if the page part is required |
||
66 | * |
||
67 | * @return bool |
||
68 | */ |
||
69 | public function getRequired() |
||
73 | |||
74 | /** |
||
75 | * Sets the message shown when the page part is required and no value was entered |
||
76 | * |
||
77 | * @param string $errorMessageRequired |
||
78 | * |
||
79 | * @return SingleLineTextPagePart |
||
80 | */ |
||
81 | public function setErrorMessageRequired($errorMessageRequired) |
||
87 | |||
88 | /** |
||
89 | * Get the error message that will be shown when the page part is required and no value was entered |
||
90 | * |
||
91 | * @return string |
||
92 | */ |
||
93 | public function getErrorMessageRequired() |
||
97 | |||
98 | /** |
||
99 | * Set the regular expression to match the entered value against |
||
100 | * |
||
101 | * @param string $regex |
||
102 | * |
||
103 | * @return SingleLineTextPagePart |
||
104 | */ |
||
105 | public function setRegex($regex) |
||
111 | |||
112 | /** |
||
113 | * Get the current regular expression |
||
114 | * |
||
115 | * @return string |
||
116 | */ |
||
117 | public function getRegex() |
||
121 | |||
122 | /** |
||
123 | * Set the error message which will be shown when the entered value doesn't match the regular expression |
||
124 | * |
||
125 | * @param string $errorMessageRegex |
||
126 | * |
||
127 | * @return SingleLineTextPagePart |
||
128 | */ |
||
129 | public function setErrorMessageRegex($errorMessageRegex) |
||
135 | |||
136 | /** |
||
137 | * Get the current error message which will be shown when the entered value doesn't match the regular expression |
||
138 | * |
||
139 | * @return string |
||
140 | */ |
||
141 | public function getErrorMessageRegex() |
||
145 | |||
146 | /** |
||
147 | * Returns the frontend view |
||
148 | * |
||
149 | * @return string |
||
150 | */ |
||
151 | public function getDefaultView() |
||
155 | |||
156 | /** |
||
157 | * Modify the form with the fields of the current page part |
||
158 | * |
||
159 | * @param FormBuilderInterface $formBuilder The form builder |
||
160 | * @param ArrayObject $fields The fields |
||
161 | * @param int $sequence The sequence of the form field |
||
162 | */ |
||
163 | public function adaptForm(FormBuilderInterface $formBuilder, ArrayObject $fields, $sequence) |
||
201 | |||
202 | /** |
||
203 | * Returns the default backend form type for this page part |
||
204 | * |
||
205 | * @return string |
||
206 | */ |
||
207 | public function getDefaultAdminType() |
||
211 | } |
||
212 |
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.