Completed
Pull Request — master (#1443)
by chihiro
23:09 queued 17:08
created

FormEventSubscriber   A

Complexity

Total Complexity 21

Size/Duplication

Total Lines 105
Duplicated Lines 52.38 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 51.61%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 21
c 1
b 0
f 0
lcom 1
cbo 4
dl 55
loc 105
ccs 16
cts 31
cp 0.5161
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A onPreSetData() 11 11 3
B getEvents() 0 31 5
A getSubscribedEvents() 0 10 1
A onPostSetData() 11 11 3
A onPreSubmit() 11 11 3
A onSubmit() 11 11 3
A onPostSubmit() 11 11 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\Event;
26
27
use Symfony\Component\Form\FormEvent;
28
use Symfony\Component\Form\FormEvents;
29
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
30
use Symfony\Component\Finder\Finder;
31
use Symfony\Component\Yaml\Yaml;
32
use Monolog\Logger;
33
34
class FormEventSubscriber implements EventSubscriberInterface
0 ignored issues
show
introduced by
Missing class doc comment
Loading history...
35
{
36
    public static function getEvents()
0 ignored issues
show
introduced by
Missing function doc comment
Loading history...
37
    {
38
        $events = array();
39
        // YamlでParseしてがんばる
40
        $basePath = __DIR__ . '/../../../app/Plugin';
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
41
        $finder = Finder::create()
42
            ->in($basePath)
43
            ->directories()
44
            ->depth(0);
45
46
        $app = \Eccube\Application::getInstance();
47
48
        foreach ($finder as $dir) {
49
            $config = Yaml::parse(file_get_contents($dir->getRealPath() . '/config.yml'));
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
Unused Code introduced by
$config is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
50
            try {
51
                $app['eccube.service.plugin']->checkPluginArchiveContent($dir->getRealPath());
52
            } catch(\Eccube\Exception\PluginException $e) {
53
                $app['monolog']->warning($e->getMessage());
54
                continue;
55
            }
56
            $config = $app['eccube.service.plugin']->readYml($dir->getRealPath() . '/config.yml');
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
57
58
            if (isset($config['form'])) {
59
                foreach ($config['form'] as $event => $class) {
60
                    $events[$event][] = '\\Plugin\\' . $config['code'] . '\\' . $class;
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
61
                }
62
            }
63
        }
64
65
        return $events;
66
    }
67
68 214
    public static function getSubscribedEvents()
0 ignored issues
show
introduced by
Missing function doc comment
Loading history...
69
    {
70
        return array(
71 214
            FormEvents::PRE_SET_DATA => 'onPreSetData',
72 214
            FormEvents::POST_SET_DATA => 'onPostSetData',
73 214
            FormEvents::PRE_SUBMIT => 'onPreSubmit',
74 214
            FormEvents::SUBMIT => 'onSubmit',
75 214
            FormEvents::POST_SUBMIT => 'onPostSubmit',
76
        );
77
    }
78
79 254 View Code Duplication
    public function onPreSetData(FormEvent $event)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
introduced by
Missing function doc comment
Loading history...
80
    {
81
        $events = self::getEvents();
82
83 254
        if (isset($events['onPreSetData'])) {
84
            foreach($events['onPreSetData'] as $formEventClass) {
85
                $formEvent = new $formEventClass();
86
                $formEvent->onPreSetData($event);
87
            }
88
        }
89
    }
90
91 254 View Code Duplication
    public function onPostSetData(FormEvent $event)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
introduced by
Missing function doc comment
Loading history...
92
    {
93
        $events = self::getEvents();
94
95 254
        if (isset($events['onPostSetData'])) {
96
            foreach($events['onPostSetData'] as $formEventClass) {
97
                $formEvent = new $formEventClass();
98
                $formEvent->onPostSetData($event);
99
            }
100
        }
101
    }
102
103 193 View Code Duplication
    public function onPreSubmit(FormEvent $event)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
introduced by
Missing function doc comment
Loading history...
104
    {
105
        $events = self::getEvents();
106
107 193
        if (isset($events['onPreSubmit'])) {
108
            foreach($events['onPreSubmit'] as $formEventClass) {
109
                $formEvent = new $formEventClass();
110
                $formEvent->onPreSubmit($event);
111
            }
112
        }
113
    }
114
115 238 View Code Duplication
    public function onSubmit(FormEvent $event)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
introduced by
Missing function doc comment
Loading history...
116
    {
117
        $events = self::getEvents();
118
119 238
        if (isset($events['onSubmit'])) {
120
            foreach($events['onSubmit'] as $formEventClass) {
121
                $formEvent = new $formEventClass();
122
                $formEvent->onSubmit($event);
123
            }
124
        }
125
    }
126
127 193 View Code Duplication
    public function onPostSubmit(FormEvent $event)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
introduced by
Missing function doc comment
Loading history...
128
    {
129
        $events = self::getEvents();
130
131 193
        if (isset($events['onPostSubmit'])) {
132
            foreach($events['onPostSubmit'] as $formEventClass) {
133
                $formEvent = new $formEventClass();
134
                $formEvent->onPostSubmit($event);
135
            }
136
        }
137
    }
138
}
139