Failed Conditions
Pull Request — master (#1543)
by Tsuyoshi
489:47 queued 481:38
created

FormEventSubscriber   A

Complexity

Total Complexity 21

Size/Duplication

Total Lines 110
Duplicated Lines 50 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 80.65%
Metric Value
wmc 21
lcom 1
cbo 3
dl 55
loc 110
ccs 25
cts 31
cp 0.8065
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A getSubscribedEvents() 0 10 1
A onPreSetData() 11 11 3
A onPostSetData() 11 11 3
A onSubmit() 11 11 3
A onPostSubmit() 11 11 3
B getEvents() 0 36 5
A onPreSubmit() 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\EventDispatcher\EventSubscriberInterface;
28
use Symfony\Component\Finder\Finder;
29
use Symfony\Component\Form\FormEvent;
30
use Symfony\Component\Form\FormEvents;
31
32
class FormEventSubscriber implements EventSubscriberInterface
0 ignored issues
show
introduced by
Missing class doc comment
Loading history...
33
{
34 1
    public static function getEvents()
0 ignored issues
show
introduced by
Missing function doc comment
Loading history...
35
    {
36 1
        $events = array();
37
        // YamlでParseしてがんばる
38 1
        $basePath = __DIR__.'/../../../app/Plugin';
39 1
        $finder = Finder::create()
40 1
            ->in($basePath)
41 1
            ->directories()
42
            ->depth(0);
43
44 1
        $app = \Eccube\Application::getInstance();
45
46
        foreach ($finder as $dir) {
47
            $code = $dir->getBaseName();
48
            $path = $dir->getRealPath();
49
50
            try {
51
                $app['eccube.service.plugin']->checkPluginArchiveContent($path);
52
            } catch (\Eccube\Exception\PluginException $e) {
53
                $app['monolog']->warning("skip {$code} form events loading. config.yml not foud or invalid.", array(
0 ignored issues
show
introduced by
Add a comma after each item in a multi-line array
Loading history...
54
                    'path' =>  $path,
55
                    'original-message' => $e->getMessage()
56
                ));
57
                continue;
58
            }
59
            $config = $app['eccube.service.plugin']->readYml($path.'/config.yml');
60
61
            if (isset($config['form'])) {
62
                foreach ($config['form'] as $event => $class) {
63
                    $events[$event][] = '\\Plugin\\'.$config['code'].'\\'.$class;
64
                }
65
            }
66
        }
67
68
        return $events;
69
    }
70
71 116
    public static function getSubscribedEvents()
0 ignored issues
show
introduced by
Missing function doc comment
Loading history...
72
    {
73
        return array(
74 116
            FormEvents::PRE_SET_DATA => 'onPreSetData',
75 116
            FormEvents::POST_SET_DATA => 'onPostSetData',
76 116
            FormEvents::PRE_SUBMIT => 'onPreSubmit',
77 116
            FormEvents::SUBMIT => 'onSubmit',
78 116
            FormEvents::POST_SUBMIT => 'onPostSubmit',
79
        );
80
    }
81
82 100 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...
83
    {
84
        $events = self::getEvents();
85
86 100
        if (isset($events['onPreSetData'])) {
87
            foreach ($events['onPreSetData'] as $formEventClass) {
88
                $formEvent = new $formEventClass();
89
                $formEvent->onPreSetData($event);
90
            }
91
        }
92
    }
93
94 100 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...
95
    {
96
        $events = self::getEvents();
97
98 100
        if (isset($events['onPostSetData'])) {
99
            foreach ($events['onPostSetData'] as $formEventClass) {
100
                $formEvent = new $formEventClass();
101
                $formEvent->onPostSetData($event);
102
            }
103
        }
104
    }
105
106 120 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...
107
    {
108 1
        $events = self::getEvents();
109
110 120
        if (isset($events['onPreSubmit'])) {
111
            foreach ($events['onPreSubmit'] as $formEventClass) {
112
                $formEvent = new $formEventClass();
113
                $formEvent->onPreSubmit($event);
114
            }
115
        }
116 1
    }
117
118 103 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...
119
    {
120
        $events = self::getEvents();
121
122 103
        if (isset($events['onSubmit'])) {
123
            foreach ($events['onSubmit'] as $formEventClass) {
124
                $formEvent = new $formEventClass();
125
                $formEvent->onSubmit($event);
126
            }
127
        }
128
    }
129
130 120 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...
131
    {
132
        $events = self::getEvents();
133
134 120
        if (isset($events['onPostSubmit'])) {
135
            foreach ($events['onPostSubmit'] as $formEventClass) {
136
                $formEvent = new $formEventClass();
137
                $formEvent->onPostSubmit($event);
138
            }
139
        }
140
    }
141
}
142