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 |
||
| 32 | class FormEventSubscriber implements EventSubscriberInterface |
||
|
|
|||
| 33 | { |
||
| 34 | 1 | public static function getEvents() |
|
| 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( |
||
| 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() |
|
| 81 | |||
| 82 | 100 | View Code Duplication | public function onPreSetData(FormEvent $event) |
| 93 | |||
| 94 | 100 | View Code Duplication | public function onPostSetData(FormEvent $event) |
| 105 | |||
| 106 | 120 | View Code Duplication | public function onPreSubmit(FormEvent $event) |
| 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) |
| 129 | |||
| 130 | 120 | View Code Duplication | public function onPostSubmit(FormEvent $event) |
| 141 | } |
||
| 142 |