AddCartType   A
last analyzed

Complexity

Total Complexity 20

Size/Duplication

Total Lines 166
Duplicated Lines 10.84 %

Coupling/Cohesion

Components 1
Dependencies 12

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 18
loc 166
ccs 73
cts 73
cp 1
rs 10
c 0
b 0
f 0
wmc 20
lcom 1
cbo 12

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 1
A setDefaultOptions() 0 10 1
A finishView() 0 12 4
A getName() 0 4 1
C buildForm() 0 71 9
B validate() 18 28 4

How to fix   Duplicated Code   

Duplicated Code

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
2
/*
3
 * This file is part of EC-CUBE
4
 *
5
 * Copyright(c) 2000-2015 LOCKON CO.,LTD. All Rights Reserved.
6
 *
7
 * http://www.lockon.co.jp/
8
 *
9
 * This program is free software; you can redistribute it and/or
10
 * modify it under the terms of the GNU General Public License
11
 * as published by the Free Software Foundation; either version 2
12
 * of the License, or (at your option) any later version.
13
 *
14
 * This program is distributed in the hope that it will be useful,
15
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17
 * GNU General Public License for more details.
18
 *
19
 * You should have received a copy of the GNU General Public License
20
 * along with this program; if not, write to the Free Software
21
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
22
 */
23
24
25
namespace Eccube\Form\Type;
26
27
use Symfony\Component\Form\AbstractType;
28
use Symfony\Component\Form\FormBuilderInterface;
29
use Symfony\Component\Form\FormEvent;
30
use Symfony\Component\Form\FormEvents;
31
use Symfony\Component\Form\FormInterface;
32
use Symfony\Component\Form\FormView;
33
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
34
use Symfony\Component\Validator\Constraints as Assert;
35
use Symfony\Component\Validator\Context\ExecutionContext;
36
37
class AddCartType extends AbstractType
0 ignored issues
show
introduced by
Missing class doc comment
Loading history...
38
{
39
40
    public $config;
41
    public $security;
42
    public $customerFavoriteProductRepository;
43
    public $Product = null;
0 ignored issues
show
Coding Style introduced by
Member variable "Product" is not in valid camel caps format
Loading history...
44
45 663
    public function __construct(
0 ignored issues
show
introduced by
Missing function doc comment
Loading history...
46
        $config,
47
        \Symfony\Component\Security\Core\SecurityContext $security,
48
        \Eccube\Repository\CustomerFavoriteProductRepository $customerFavoriteProductRepository
49
    ) {
50 663
        $this->config = $config;
51 663
        $this->security = $security;
52 663
        $this->customerFavoriteProductRepository = $customerFavoriteProductRepository;
53
    }
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)
134
    {
135 20
        $resolver->setRequired('product');
0 ignored issues
show
Documentation introduced by
'product' is of type string, but the function expects a array.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
136 20
        $resolver->setDefaults(array(
137 20
            'id_add_product_id' => true,
138
            'constraints' => array(
139 20
                new Assert\Callback(array($this, 'validate')),
140
            ),
141
        ));
142
    }
143
144
    /*
145
     * {@inheritdoc}
146
     */
147 19
    public function finishView(FormView $view, FormInterface $form, array $options)
0 ignored issues
show
introduced by
You must use "/**" style comments for a function comment
Loading history...
148
    {
149 19
        if ($options['id_add_product_id']) {
150 7
            foreach ($view->vars['form']->children as $child) {
151 7
                $child->vars['id'] .= $options['product']->getId();
152
            }
153
        }
154
155 19
        if ($view->vars['form']->children['mode']->vars['value'] === 'add_cart') {
156 19
            $view->vars['form']->children['mode']->vars['value'] = '';
157
        }
158
    }
159
160
    /**
161
     * {@inheritdoc}
162
     */
163 663
    public function getName()
164
    {
165 663
        return 'add_cart';
166
    }
167
168
    /**
169
     * validate
170
     *
171
     * @param type             $data
172
     * @param ExecutionContext $context
173
     */
174 8
    public function validate($data, ExecutionContext $context)
175
    {
176 8
        if ($data['mode'] !== 'add_favorite') {
177 6
            $context->validateValue($data['product_class_id'], array(
178 6
                new Assert\NotBlank(),
179 6
            ), '[product_class_id]');
180 6 View Code Duplication
            if ($this->Product->getClassName1()) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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.

Loading history...
181 5
                $context->validateValue($data['classcategory_id1'], array(
182 5
                    new Assert\NotBlank(),
183 5
                    new Assert\NotEqualTo(array(
0 ignored issues
show
introduced by
Add a comma after each item in a multi-line array
Loading history...
184 5
                        'value' => '__unselected',
185
                        'message' => 'form.type.select.notselect'
186
                    )),
187 5
                ), '[classcategory_id1]');
188
            }
189
            //商品規格2初期状態(未選択)の場合の返却値は「NULL」で「__unselected」ではない
190 6 View Code Duplication
            if ($this->Product->getClassName2()) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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.

Loading history...
191 4
                $context->validateValue($data['classcategory_id2'], array(
192 4
                    new Assert\NotBlank(),
193 4
                    new Assert\NotEqualTo(array(
0 ignored issues
show
introduced by
Add a comma after each item in a multi-line array
Loading history...
194 4
                        'value' => '__unselected',
195
                        'message' => 'form.type.select.notselect'
196
                    )),
197 4
                ), '[classcategory_id2]');
198
            }
199
0 ignored issues
show
Coding Style introduced by
Blank line found at end of control structure
Loading history...
200
        }
201
    }
202
}
203