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 |
||
| 34 | class FormEventSubscriber implements EventSubscriberInterface |
||
|
|
|||
| 35 | { |
||
| 36 | public static function getEvents() |
||
| 37 | { |
||
| 38 | $events = array(); |
||
| 39 | // YamlでParseしてがんばる |
||
| 40 | $basePath = __DIR__ . '/../../../app/Plugin'; |
||
| 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')); |
||
| 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'); |
||
| 57 | |||
| 58 | if (isset($config['form'])) { |
||
| 59 | foreach ($config['form'] as $event => $class) { |
||
| 60 | $events[$event][] = '\\Plugin\\' . $config['code'] . '\\' . $class; |
||
| 61 | } |
||
| 62 | } |
||
| 63 | } |
||
| 64 | |||
| 65 | return $events; |
||
| 66 | } |
||
| 67 | |||
| 68 | 214 | public static function getSubscribedEvents() |
|
| 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) |
| 90 | |||
| 91 | 254 | View Code Duplication | public function onPostSetData(FormEvent $event) |
| 92 | { |
||
| 102 | |||
| 103 | 193 | View Code Duplication | public function onPreSubmit(FormEvent $event) |
| 114 | |||
| 115 | 238 | View Code Duplication | public function onSubmit(FormEvent $event) |
| 126 | |||
| 127 | 193 | View Code Duplication | public function onPostSubmit(FormEvent $event) |
| 138 | } |
||
| 139 |