SecurityType   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 66
Duplicated Lines 24.24 %

Coupling/Cohesion

Components 1
Dependencies 7

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 16
loc 66
ccs 29
cts 29
cp 1
rs 10
c 0
b 0
f 0
wmc 5
lcom 1
cbo 7

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A getName() 0 4 1
B buildForm() 16 43 3

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\Admin;
26
27
use Symfony\Component\Form\AbstractType;
28
use Symfony\Component\Form\FormBuilderInterface;
29
use Symfony\Component\Form\FormError;
30
use Symfony\Component\Form\FormEvents;
31
use Symfony\Component\Validator\Constraints as Assert;
32
33
class SecurityType extends AbstractType
0 ignored issues
show
introduced by
Missing class doc comment
Loading history...
34
{
35
    private $app;
36
    private $config;
37
38 663
    public function __construct($app)
0 ignored issues
show
introduced by
Missing function doc comment
Loading history...
39
    {
40 663
        $this->app = $app;
41 663
        $this->config = $app['config'];
42
    }
43
44
    /**
45
     * {@inheritdoc}
46
     */
47 7
    public function buildForm(FormBuilderInterface $builder, array $options)
48
    {
49 7
        $app = $this->app;
50
        $builder
51 7
            ->add('admin_route_dir', 'text', array(
52 7
                'label' => 'ディレクトリ名',
53
                'constraints' => array(
54 7
                    new Assert\NotBlank(),
55 7
                    new Assert\Length(array('max' => $this->config['stext_len'])),
56 7
                    new Assert\Regex(array(
57 7
                       'pattern' => "/^[0-9a-zA-Z]+$/",
0 ignored issues
show
Coding Style introduced by
This line of the multi-line function call does not seem to be indented correctly. Expected 24 spaces, but found 23.
Loading history...
58
                   )),
0 ignored issues
show
Coding Style introduced by
This line of the multi-line function call does not seem to be indented correctly. Expected 20 spaces, but found 19.
Loading history...
59
                ),
60
            ))
61 7
            ->add('admin_allow_host', 'textarea', array(
62 7
                'required' => false,
63 7
                'label' => 'IP制限',
64
                'constraints' => array(
65 7
                    new Assert\Length(array('max' => $this->config['stext_len'])),
66
                ),
67
            ))
68 7
            ->add('force_ssl', 'checkbox', array(
69 7
                'label' => 'SSLを強制',
70
                'required' => false,
71
            ))
72 7 View Code Duplication
            ->addEventListener(FormEvents::POST_SUBMIT, function ($event) use($app) {
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...
Coding Style introduced by
Expected 1 space after USE keyword; found 0
Loading history...
73 6
                $form = $event->getForm();
74 6
                $data = $form->getData();
75
76 6
                $ips = preg_split("/\R/", $data['admin_allow_host'], null, PREG_SPLIT_NO_EMPTY);
77
78 6
                foreach($ips as $ip) {
79 4
                    $errors = $app['validator']->validateValue($ip, array(
80 4
                            new Assert\Ip(),
81
                        )
82
                    );
83 4
                    if ($errors->count() != 0) {
84 6
                        $form['admin_allow_host']->addError(new FormError($ip . 'はIPv4アドレスではありません。'));
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
85
                    }
86
                }
87 7
            })
88
        ;
89
    }
90
91
    /**
92
     * {@inheritdoc}
93
     */
94 663
    public function getName()
95
    {
96 663
        return 'admin_security';
97
    }
98
}
99