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 | class CreditCardSubForm extends AbstractPayoneSubForm |
||
21 | { |
||
22 | const PAYMENT_METHOD = 'credit_card'; |
||
23 | |||
24 | const FIELD_CARD_TYPE = 'cardtype'; |
||
25 | const FIELD_CARD_NUMBER = 'cardpan'; |
||
26 | const FIELD_NAME_ON_CARD = 'cardholder'; |
||
27 | const FIELD_CARD_EXPIRES_MONTH = 'cardexpiredate_month'; |
||
28 | const FIELD_CARD_EXPIRES_YEAR = 'cardexpiredate_year'; |
||
29 | const FIELD_CARD_SECURITY_CODE = 'cardcvc2'; |
||
30 | const FIELD_PSEUDO_CARD_NUMBER = 'pseudocardpan'; |
||
31 | |||
32 | const OPTION_CARD_EXPIRES_CHOICES_MONTH = 'month choices'; |
||
33 | const OPTION_CARD_EXPIRES_CHOICES_YEAR = 'year choices'; |
||
34 | const OPTION_CARD_TYPES = 'card types'; |
||
35 | |||
36 | const OPTION_PAYONE_SETTINGS = 'payone settings'; |
||
37 | |||
38 | /** |
||
39 | * @return string |
||
40 | */ |
||
41 | public function getName() |
||
45 | |||
46 | /** |
||
47 | * @return string |
||
48 | */ |
||
49 | public function getPropertyPath() |
||
53 | |||
54 | /** |
||
55 | * @return string |
||
56 | */ |
||
57 | public function getTemplatePath() |
||
61 | |||
62 | /** |
||
63 | * @param \Symfony\Component\OptionsResolver\OptionsResolver $resolver |
||
64 | * |
||
65 | * @return void |
||
66 | */ |
||
67 | public function configureOptions(OptionsResolver $resolver) |
||
73 | |||
74 | /** |
||
75 | * @param \Symfony\Component\OptionsResolver\OptionsResolver $resolver |
||
76 | * |
||
77 | * @return void |
||
78 | */ |
||
79 | public function setDefaultOptions(OptionsResolver $resolver) |
||
83 | |||
84 | /** |
||
85 | * @param \Symfony\Component\Form\FormBuilderInterface $builder |
||
86 | * @param array $options |
||
87 | * |
||
88 | * @return void |
||
89 | */ |
||
90 | public function buildForm(FormBuilderInterface $builder, array $options) |
||
96 | |||
97 | /** |
||
98 | * @param \Symfony\Component\Form\FormBuilderInterface $builder |
||
99 | * @param array $options |
||
100 | * |
||
101 | * @return \SprykerEco\Yves\Payone\Form\CreditCardSubForm |
||
102 | */ |
||
103 | public function addCardType(FormBuilderInterface $builder, array $options) |
||
123 | |||
124 | /** |
||
125 | * @param \Symfony\Component\Form\FormBuilderInterface $builder |
||
126 | * |
||
127 | * @return \SprykerEco\Yves\Payone\Form\CreditCardSubForm |
||
128 | */ |
||
129 | View Code Duplication | protected function addCardNumber(FormBuilderInterface $builder) |
|
142 | |||
143 | /** |
||
144 | * @param \Symfony\Component\Form\FormBuilderInterface $builder |
||
145 | * |
||
146 | * @return \SprykerEco\Yves\Payone\Form\CreditCardSubForm |
||
147 | */ |
||
148 | protected function addNameOnCard(FormBuilderInterface $builder) |
||
164 | |||
165 | /** |
||
166 | * @param \Symfony\Component\Form\FormBuilderInterface $builder |
||
167 | * @param array $options |
||
168 | * |
||
169 | * @return \SprykerEco\Yves\Payone\Form\CreditCardSubForm |
||
170 | */ |
||
171 | protected function addCardExpiresMonth(FormBuilderInterface $builder, array $options) |
||
188 | |||
189 | /** |
||
190 | * @param \Symfony\Component\Form\FormBuilderInterface $builder |
||
191 | * @param array $options |
||
192 | * |
||
193 | * @return \SprykerEco\Yves\Payone\Form\CreditCardSubForm |
||
194 | */ |
||
195 | protected function addCardExpiresYear(FormBuilderInterface $builder, array $options) |
||
215 | |||
216 | /** |
||
217 | * @param \Symfony\Component\Form\FormBuilderInterface $builder |
||
218 | * |
||
219 | * @return \SprykerEco\Yves\Payone\Form\CreditCardSubForm |
||
220 | */ |
||
221 | View Code Duplication | protected function addCardSecurityCode(FormBuilderInterface $builder) |
|
234 | |||
235 | /** |
||
236 | * @param \Symfony\Component\Form\FormBuilderInterface $builder |
||
237 | * |
||
238 | * @return $this |
||
239 | */ |
||
240 | protected function addHiddenInputs(FormBuilderInterface $builder) |
||
254 | } |
||
255 |
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.