Conditions | 19 |
Paths | 1014 |
Total Lines | 104 |
Lines | 16 |
Ratio | 15.38 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
37 | public function index(Application $app, Request $request) |
||
38 | { |
||
39 | |||
40 | $builder = $app['form.factory']->createBuilder('admin_security'); |
||
41 | $form = $builder->getForm(); |
||
42 | |||
43 | if ('POST' === $request->getMethod()) { |
||
44 | |||
45 | $form->handleRequest($request); |
||
46 | |||
47 | if ($form->isValid()) { |
||
48 | $data = $form->getData(); |
||
49 | |||
50 | // 現在のセキュリティ情報を更新 |
||
51 | $adminRoot = $app['config']['admin_route']; |
||
52 | |||
53 | $configFile = $app['config']['root_dir'].'/app/config/eccube/config'; |
||
54 | View Code Duplication | if (file_exists($configFile.'.php')) { |
|
|
|||
55 | $config = require $configFile.'.php'; |
||
56 | } elseif (file_exists($configFile.'.yml')) { |
||
57 | $config = Yaml::parse(file_get_contents($configFile.'.yml')); |
||
58 | } |
||
59 | |||
60 | // trim処理 |
||
61 | $allowHost = Str::convertLineFeed($data['admin_allow_host']); |
||
62 | if (empty($allowHost)) { |
||
63 | $config['admin_allow_host'] = null; |
||
64 | } else { |
||
65 | $config['admin_allow_host'] = explode("\n", $allowHost); |
||
66 | } |
||
67 | |||
68 | if ($data['force_ssl']) { |
||
69 | // SSL制限にチェックをいれた場合、https経由で接続されたか確認 |
||
70 | if ($request->isSecure()) { |
||
71 | // httpsでアクセスされたらSSL制限をチェック |
||
72 | $config['force_ssl'] = Constant::ENABLED; |
||
73 | } else { |
||
74 | // httpから変更されたらfalseのまま |
||
75 | $config['force_ssl'] = Constant::DISABLED; |
||
76 | $data['force_ssl'] = (bool) Constant::DISABLED; |
||
77 | } |
||
78 | } else { |
||
79 | $config['force_ssl'] = Constant::DISABLED; |
||
80 | } |
||
81 | $form = $builder->getForm(); |
||
82 | $form->setData($data); |
||
83 | |||
84 | View Code Duplication | if (file_exists($configFile.'.php')) { |
|
85 | file_put_contents($configFile.'.php', sprintf('<?php return %s', var_export($config, true)).';'); |
||
86 | } |
||
87 | if (file_exists($configFile.'.yml')) { |
||
88 | file_put_contents($configFile.'.yml', Yaml::dump($config)); |
||
89 | } |
||
90 | |||
91 | if ($adminRoot != $data['admin_route_dir']) { |
||
92 | // admin_routeが変更されればpath.(yml|php)を更新 |
||
93 | $pathFile = $app['config']['root_dir'].'/app/config/eccube/path'; |
||
94 | |||
95 | View Code Duplication | if (file_exists($pathFile.'.php')) { |
|
96 | $config = require $pathFile.'.php'; |
||
97 | } elseif (file_exists($pathFile.'.yml')) { |
||
98 | $config = Yaml::parse(file_get_contents($pathFile.'.yml')); |
||
99 | } |
||
100 | |||
101 | $config['admin_route'] = $data['admin_route_dir']; |
||
102 | |||
103 | View Code Duplication | if (file_exists($pathFile.'.php')) { |
|
104 | file_put_contents($pathFile.'.php', sprintf('<?php return %s', var_export($config, true)).';'); |
||
105 | } |
||
106 | if (file_exists($pathFile.'.yml')) { |
||
107 | file_put_contents($pathFile.'.yml', Yaml::dump($config)); |
||
108 | } |
||
109 | |||
110 | $app->addSuccess('admin.system.security.route.dir.complete', 'admin'); |
||
111 | |||
112 | // ログアウト |
||
113 | $this->getSecurity($app)->setToken(null); |
||
114 | |||
115 | // 管理者画面へ再ログイン |
||
116 | return $app->redirect($request->getBaseUrl().'/'.$config['admin_route']); |
||
117 | } |
||
118 | |||
119 | $app->addSuccess('admin.system.security.save.complete', 'admin'); |
||
120 | |||
121 | } |
||
122 | } else { |
||
123 | // セキュリティ情報の取得 |
||
124 | $form->get('admin_route_dir')->setData($app['config']['admin_route']); |
||
125 | $allowHost = $app['config']['admin_allow_host']; |
||
126 | if (is_array($allowHost) && count($allowHost) > 0) { |
||
127 | $form->get('admin_allow_host')->setData(Str::convertLineFeed(implode("\n", $allowHost))); |
||
128 | } |
||
129 | $form->get('force_ssl')->setData((bool)$app['config']['force_ssl']); |
||
130 | } |
||
131 | |||
132 | // 管理画面URLのチェック |
||
133 | if (isset($app['config']['admin_route']) && $app['config']['admin_route'] == 'admin') { |
||
134 | $app->addWarning('admin.system.security.admin.url.warning', 'admin'); |
||
135 | } |
||
136 | |||
137 | return $app->render('Setting/System/security.twig', array( |
||
138 | 'form' => $form->createView(), |
||
139 | )); |
||
140 | } |
||
141 | } |
||
142 |
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.