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 |
||
37 | class AddCartType extends AbstractType |
||
|
|||
38 | { |
||
39 | |||
40 | public $config; |
||
41 | public $security; |
||
42 | public $customerFavoriteProductRepository; |
||
43 | public $Product = null; |
||
44 | |||
45 | 663 | public function __construct( |
|
54 | |||
55 | /** |
||
56 | * {@inheritdoc} |
||
57 | */ |
||
58 | 20 | public function buildForm(FormBuilderInterface $builder, array $options) |
|
59 | { |
||
60 | /* @var $Product \Eccube\Entity\Product */ |
||
61 | 20 | $Product = $options['product']; |
|
62 | 20 | $this->Product = $Product; |
|
63 | 20 | $ProductClasses = $Product->getProductClasses(); |
|
64 | |||
65 | $builder |
||
66 | 20 | ->add('mode', 'hidden', array( |
|
67 | 20 | 'data' => 'add_cart', |
|
68 | )) |
||
69 | 20 | ->add('product_id', 'hidden', array( |
|
70 | 20 | 'data' => $Product->getId(), |
|
71 | 'constraints' => array( |
||
72 | 20 | new Assert\NotBlank(), |
|
73 | 20 | new Assert\Regex(array('pattern' => '/^\d+$/')), |
|
74 | ), |
||
75 | )) |
||
76 | 20 | ->add('product_class_id', 'hidden', array( |
|
77 | 20 | 'data' => count($ProductClasses) === 1 ? $ProductClasses[0]->getId() : '', |
|
78 | 'constraints' => array( |
||
79 | 20 | new Assert\Regex(array('pattern' => '/^\d+$/')), |
|
80 | ), |
||
81 | )); |
||
82 | |||
83 | 20 | if ($Product->getStockFind()) { |
|
84 | $builder |
||
85 | 18 | ->add('quantity', 'integer', array( |
|
86 | 18 | 'data' => 1, |
|
87 | 'attr' => array( |
||
88 | 18 | 'min' => 1, |
|
89 | 18 | 'maxlength' => $this->config['int_len'], |
|
90 | ), |
||
91 | 'constraints' => array( |
||
92 | 18 | new Assert\NotBlank(), |
|
93 | 18 | new Assert\GreaterThanOrEqual(array( |
|
94 | 18 | 'value' => 1, |
|
95 | )), |
||
96 | 18 | new Assert\Regex(array('pattern' => '/^\d+$/')), |
|
97 | ), |
||
98 | )) |
||
99 | ; |
||
100 | 18 | if ($Product && $Product->getProductClasses()) { |
|
101 | 18 | if (!is_null($Product->getClassName1())) { |
|
102 | 18 | $builder->add('classcategory_id1', 'choice', array( |
|
103 | 18 | 'label' => $Product->getClassName1(), |
|
104 | 18 | 'choices' => array('__unselected' => '選択してください') + $Product->getClassCategories1(), |
|
105 | )); |
||
106 | } |
||
107 | 18 | if (!is_null($Product->getClassName2())) { |
|
108 | 13 | $builder->add('classcategory_id2', 'choice', array( |
|
109 | 13 | 'label' => $Product->getClassName2(), |
|
110 | 'choices' => array('__unselected' => '選択してください'), |
||
111 | )); |
||
112 | } |
||
113 | } |
||
114 | |||
115 | 18 | $builder->addEventListener(FormEvents::PRE_SUBMIT, function (FormEvent $event) use ($Product) { |
|
116 | 6 | $data = $event->getData(); |
|
117 | 6 | $form = $event->getForm(); |
|
118 | 6 | if (!is_null($Product->getClassName2())) { |
|
119 | 5 | if ($data['classcategory_id1']) { |
|
120 | 5 | $form->add('classcategory_id2', 'choice', array( |
|
121 | 5 | 'label' => $Product->getClassName2(), |
|
122 | 5 | 'choices' => array('__unselected' => '選択してください') + $Product->getClassCategories2($data['classcategory_id1']), |
|
123 | )); |
||
124 | } |
||
125 | } |
||
126 | 18 | }); |
|
127 | } |
||
128 | } |
||
129 | |||
130 | /** |
||
131 | * {@inheritdoc} |
||
132 | */ |
||
133 | 20 | public function setDefaultOptions(OptionsResolverInterface $resolver) |
|
143 | |||
144 | /* |
||
145 | * {@inheritdoc} |
||
146 | */ |
||
147 | 19 | public function finishView(FormView $view, FormInterface $form, array $options) |
|
159 | |||
160 | /** |
||
161 | * {@inheritdoc} |
||
162 | */ |
||
163 | 663 | public function getName() |
|
167 | |||
168 | /** |
||
169 | * validate |
||
170 | * |
||
171 | * @param type $data |
||
172 | * @param ExecutionContext $context |
||
173 | */ |
||
174 | 8 | public function validate($data, ExecutionContext $context) |
|
202 | } |
||
203 |