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 |
||
19 | View Code Duplication | class CheckboxPagePart extends AbstractFormPagePart |
|
|
|||
20 | { |
||
21 | /** |
||
22 | * If set to true, you are obligated to fill in this page part |
||
23 | * |
||
24 | * @ORM\Column(type="boolean", nullable=true) |
||
25 | */ |
||
26 | protected $required = false; |
||
27 | |||
28 | /** |
||
29 | * Error message shows when the page part is required and nothing is filled in |
||
30 | * |
||
31 | * @ORM\Column(type="string", name="error_message_required", nullable=true) |
||
32 | */ |
||
33 | protected $errorMessageRequired; |
||
34 | |||
35 | /** |
||
36 | * Sets the required valud of this page part |
||
37 | * |
||
38 | * @param bool $required |
||
39 | * |
||
40 | * @return CheckboxPagePart |
||
41 | */ |
||
42 | 2 | public function setRequired($required) |
|
48 | |||
49 | /** |
||
50 | * Check if the page part is required |
||
51 | * |
||
52 | * @return bool |
||
53 | */ |
||
54 | 2 | public function getRequired() |
|
58 | |||
59 | /** |
||
60 | * Sets the message shown when the page part is required and no value was entered |
||
61 | * |
||
62 | * @param string $errorMessageRequired |
||
63 | * |
||
64 | * @return CheckboxPagePart |
||
65 | */ |
||
66 | 3 | public function setErrorMessageRequired($errorMessageRequired) |
|
72 | |||
73 | /** |
||
74 | * Get the error message that will be shown when the page part is required and no value was entered |
||
75 | * |
||
76 | * @return string |
||
77 | */ |
||
78 | 2 | public function getErrorMessageRequired() |
|
82 | |||
83 | /** |
||
84 | * Returns the frontend view |
||
85 | * |
||
86 | * @return string |
||
87 | */ |
||
88 | 1 | public function getDefaultView() |
|
92 | |||
93 | /** |
||
94 | * Modify the form with the fields of the current page part |
||
95 | * |
||
96 | * @param FormBuilderInterface $formBuilder The form builder |
||
97 | * @param ArrayObject $fields The fields |
||
98 | * @param int $sequence The sequence of the form field |
||
99 | */ |
||
100 | 1 | public function adaptForm(FormBuilderInterface $formBuilder, ArrayObject $fields, $sequence) |
|
130 | |||
131 | /** |
||
132 | * Returns the default backend form type for this page part |
||
133 | * |
||
134 | * @return string |
||
135 | */ |
||
136 | 1 | public function getDefaultAdminType() |
|
140 | } |
||
141 |
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.