Complex classes like PannelloAmministrazioneController often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use PannelloAmministrazioneController, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
15 | class PannelloAmministrazioneController extends Controller |
||
16 | { |
||
17 | |||
18 | protected $apppaths; |
||
19 | |||
20 | 1 | public function indexAction() |
|
21 | { |
||
22 | 1 | $finder = new Finder(); |
|
23 | 1 | $fs = new Filesystem(); |
|
24 | 1 | $this->apppaths = $this->get("pannelloamministrazione.projectpath"); |
|
25 | |||
26 | 1 | $projectDir = $this->apppaths->getRootPath(); |
|
27 | 1 | $bundlelists = $this->container->getParameter('kernel.bundles'); |
|
28 | 1 | $bundles = array(); |
|
29 | 1 | foreach ($bundlelists as $bundle) { |
|
30 | 1 | if (substr($bundle, 0, 2) === 'Fi') { |
|
31 | 1 | $bundle = str_replace('\\', '/', $bundle); |
|
32 | 1 | $bundlepath = $this->apppaths->getSrcPath() . DIRECTORY_SEPARATOR . substr($bundle, 0, strripos($bundle, '/')); |
|
33 | 1 | if ($fs->exists($bundlepath)) { |
|
34 | 1 | $bundles[] = substr($bundle, 0, strripos($bundle, '/')); |
|
35 | } |
||
36 | } |
||
37 | } |
||
38 | 1 | $docDir = $this->apppaths->getDocPath(); |
|
39 | |||
40 | 1 | $mwbs = array(); |
|
41 | |||
42 | 1 | if ($fs->exists($docDir)) { |
|
43 | 1 | $finder->in($docDir)->files()->name('*.mwb'); |
|
44 | 1 | foreach ($finder as $file) { |
|
45 | 1 | $mwbs[] = $file->getBasename(); |
|
46 | } |
||
47 | } |
||
48 | |||
49 | 1 | if ($fs->exists($projectDir . '/.svn')) { |
|
50 | $svn = true; |
||
51 | } else { |
||
52 | 1 | $svn = false; |
|
53 | } |
||
54 | |||
55 | 1 | if ($fs->exists($projectDir . '/.git')) { |
|
56 | $git = true; |
||
57 | } else { |
||
58 | 1 | $git = false; |
|
59 | } |
||
60 | |||
61 | 1 | if (!OsFunctions::isWindows()) { |
|
62 | 1 | $delcmd = 'rm -rf'; |
|
63 | 1 | $setfilelock = "touch " .$this->getParameter("maintenanceLockFilePath"); |
|
64 | 1 | $remfilelock = "rm " .$this->getParameter("maintenanceLockFilePath"); |
|
65 | 1 | $windows = false; |
|
66 | } else { |
||
67 | $delcmd = 'del'; |
||
68 | $setfilelock = 'echo $null >> ' .$this->getParameter("maintenanceLockFilePath"); |
||
69 | $remfilelock = "del " .$this->getParameter("maintenanceLockFilePath"); |
||
70 | $windows = true; |
||
71 | } |
||
72 | |||
73 | 1 | $dellockfile = $delcmd . ' ' . $this->apppaths->getCachePath() . DIRECTORY_SEPARATOR . 'running.run'; |
|
74 | 1 | $delcomposerfile = $delcmd . ' ' . $projectDir . DIRECTORY_SEPARATOR . 'composer.lock'; |
|
75 | 1 | $dellogsfiles = $delcmd . ' ' . $this->apppaths->getLogsPath() . DIRECTORY_SEPARATOR . '*'; |
|
76 | 1 | $delcacheprodfiles = $delcmd . ' ' . $this->apppaths->getCachePath() . DIRECTORY_SEPARATOR . 'prod' . DIRECTORY_SEPARATOR . '*'; |
|
77 | 1 | $delcachedevfiles = $delcmd . ' ' . $this->apppaths->getCachePath() . DIRECTORY_SEPARATOR . 'dev' . DIRECTORY_SEPARATOR . '*'; |
|
78 | 1 | $setmaintenancefile = $setfilelock; |
|
79 | 1 | $remmaintenancefile = $remfilelock; |
|
80 | |||
81 | $comandishell = array( |
||
82 | 1 | 'lockfile' => $this->fixSlash($dellockfile), |
|
83 | 1 | 'composerlock' => $this->fixSlash($delcomposerfile), |
|
84 | 1 | 'logsfiles' => $this->fixSlash($dellogsfiles), |
|
85 | 1 | 'cacheprodfiles' => $this->fixSlash($delcacheprodfiles), |
|
86 | 1 | 'cachedevfiles' => $this->fixSlash($delcachedevfiles), |
|
87 | 1 | 'setmaintenancefile' => $setmaintenancefile, |
|
88 | 1 | 'remmaintenancefile' => $remmaintenancefile, |
|
89 | ); |
||
90 | |||
91 | 1 | $twigparms = array('svn' => $svn, 'git' => $git, 'bundles' => $bundles, 'mwbs' => $mwbs, |
|
92 | 1 | 'rootdir' => $this->fixSlash($projectDir), |
|
93 | 1 | 'comandishell' => $comandishell, 'iswindows' => $windows,); |
|
94 | |||
95 | 1 | return $this->render('PannelloAmministrazioneBundle:PannelloAmministrazione:index.html.twig', $twigparms); |
|
96 | } |
||
97 | |||
98 | 1 | private function fixSlash($path) |
|
99 | { |
||
100 | 1 | return str_replace('\\', '\\\\', $path); |
|
101 | } |
||
102 | |||
103 | public function aggiornaSchemaDatabaseAction() |
||
104 | { |
||
105 | if ((new LockSystem($this->container))->isLockedFile()) { |
||
106 | return (new LockSystem($this->container))->lockedFunctionMessage(); |
||
107 | } else { |
||
108 | (new LockSystem($this->container))->lockFile(true); |
||
109 | $command = $this->container->get("pannelloamministrazione.commands"); |
||
110 | $result = $command->aggiornaSchemaDatabase(); |
||
111 | |||
112 | (new LockSystem($this->container))->lockFile(false); |
||
113 | $twigparms = array('errcode' => $result['errcode'], 'command' => $result['command'], 'message' => $result['message']); |
||
114 | |||
115 | return $this->render('PannelloAmministrazioneBundle:PannelloAmministrazione:outputcommand.html.twig', $twigparms); |
||
116 | } |
||
117 | } |
||
118 | |||
119 | /* FORMS */ |
||
120 | |||
121 | public function generateFormCrudAction(Request $request) |
||
122 | { |
||
123 | if ((new LockSystem($this->container))->isLockedFile()) { |
||
124 | return (new LockSystem($this->container))->lockedFunctionMessage(); |
||
125 | } else { |
||
126 | $bundlename = $request->get('bundlename'); |
||
127 | $entityform = $request->get('entityform'); |
||
128 | |||
129 | (new LockSystem($this->container))->lockFile(true); |
||
130 | |||
131 | $command = $this->container->get("pannelloamministrazione.commands"); |
||
132 | $ret = $command->generateFormCrud($bundlename, $entityform); |
||
133 | |||
134 | (new LockSystem($this->container))->lockFile(false); |
||
135 | //$retcc = ''; |
||
|
|||
136 | if ($ret['errcode'] < 0) { |
||
137 | return new Response($ret['message']); |
||
138 | } else { |
||
139 | //$retcc = $command->clearCacheEnv($this->container->get('kernel')->getEnvironment()); |
||
140 | } |
||
141 | $twigparms = array('errcode' => $ret['errcode'], 'command' => $ret['command'], 'message' => $ret['message']); |
||
142 | |||
143 | return $this->render('PannelloAmministrazioneBundle:PannelloAmministrazione:outputcommand.html.twig', $twigparms); |
||
144 | } |
||
145 | } |
||
146 | |||
147 | /* ENTITIES */ |
||
148 | |||
149 | public function generateEntityAction(Request $request) |
||
150 | { |
||
151 | if ((new LockSystem($this->container))->isLockedFile()) { |
||
152 | return (new LockSystem($this->container))->lockedFunctionMessage(); |
||
153 | } else { |
||
154 | (new LockSystem($this->container))->lockFile(true); |
||
155 | $wbFile = $request->get('file'); |
||
156 | $bundlePath = $request->get('bundle'); |
||
157 | $command = $this->container->get("pannelloamministrazione.commands"); |
||
158 | $ret = $command->generateEntity($wbFile, $bundlePath); |
||
159 | (new LockSystem($this->container))->lockFile(false); |
||
160 | return new Response($ret['message']); |
||
161 | } |
||
162 | } |
||
163 | |||
164 | /* ENTITIES */ |
||
165 | |||
166 | public function generateEntityClassAction(Request $request) |
||
167 | { |
||
168 | if ((new LockSystem($this->container))->isLockedFile()) { |
||
169 | return (new LockSystem($this->container))->lockedFunctionMessage(); |
||
170 | } else { |
||
171 | (new LockSystem($this->container))->lockFile(true); |
||
172 | $bundlePath = $request->get('bundle'); |
||
173 | $command = $this->container->get("pannelloamministrazione.commands"); |
||
174 | $ret = $command->generateEntityClass($bundlePath); |
||
175 | (new LockSystem($this->container))->lockFile(false); |
||
176 | |||
177 | return new Response($ret['message']); |
||
178 | } |
||
179 | } |
||
180 | |||
181 | /* BUNDLE */ |
||
182 | |||
183 | public function generateBundleAction(Request $request) |
||
184 | { |
||
185 | $this->apppaths = $this->get("pannelloamministrazione.projectpath"); |
||
186 | if ((new LockSystem($this->container))->isLockedFile()) { |
||
187 | return (new LockSystem($this->container))->lockedFunctionMessage(); |
||
188 | } else { |
||
189 | (new LockSystem($this->container))->lockFile(true); |
||
190 | $command = $this->container->get("pannelloamministrazione.commands"); |
||
191 | $bundleName = $request->get('bundlename'); |
||
192 | $result = $command->generateBundle($bundleName); |
||
193 | if ($result["errcode"] >= 0) { |
||
194 | //$msg = "\nPer abilitare il nuovo bundle nel kernel pulire la cache e aggiornare la pagina"; |
||
195 | //$alert = '<script type="text/javascript">alert("' . $msg . '");location.reload();</script>'; |
||
196 | //$result['message'] = $result['message'] . $msg; |
||
197 | } |
||
198 | (new LockSystem($this->container))->lockFile(false); |
||
199 | //Uso exit perchè la render avendo creato un nuovo bundle schianta perchè non è caricato nel kernel il nuovo bundle ancora |
||
200 | //exit; |
||
201 | $twigparms = array('errcode' => $result['errcode'], 'command' => $result['command'], 'message' => $result['message']); |
||
202 | |||
203 | return $this->render('PannelloAmministrazioneBundle:PannelloAmministrazione:outputcommand.html.twig', $twigparms); |
||
204 | } |
||
205 | } |
||
206 | |||
207 | /* VCS (GIT,SVN) */ |
||
208 | |||
209 | public function getVcsAction() |
||
210 | { |
||
211 | set_time_limit(0); |
||
212 | $this->apppaths = $this->get("pannelloamministrazione.projectpath"); |
||
213 | if ((new LockSystem($this->container))->isLockedFile()) { |
||
214 | return (new LockSystem($this->container))->lockedFunctionMessage(); |
||
215 | } else { |
||
216 | (new LockSystem($this->container))->lockFile(true); |
||
217 | $command = $this->container->get("pannelloamministrazione.commands"); |
||
218 | $result = $command->getVcs(); |
||
219 | (new LockSystem($this->container))->lockFile(false); |
||
220 | if ($result['errcode'] < 0) { |
||
221 | $responseout = '<pre>Errore nel comando: <i style = "color: white;">' . $result['command'] . '</i>' |
||
222 | . '<br/><i style = "color: red;">' . nl2br($result['errmsg']) . '</i></pre>'; |
||
223 | } else { |
||
224 | $responseout = '<pre>Eseguito comando: <i style = "color: white;">' . $result['command'] . '</i><br/>' . |
||
225 | nl2br($result['errmsg']) . '</pre>'; |
||
226 | } |
||
227 | |||
228 | return new Response($responseout); |
||
229 | } |
||
230 | } |
||
231 | |||
232 | /* CLEAR CACHE */ |
||
233 | |||
234 | /** |
||
235 | * Suppress PMD warnings per exit. |
||
236 | * |
||
237 | * @SuppressWarnings(PHPMD) |
||
238 | */ |
||
239 | public function clearCacheAction(Request $request) |
||
240 | { |
||
241 | set_time_limit(0); |
||
242 | if ((new LockSystem($this->container))->isLockedFile()) { |
||
243 | return (new LockSystem($this->container))->lockedFunctionMessage(); |
||
244 | } else { |
||
245 | (new LockSystem($this->container))->lockFile(true); |
||
246 | $command = $this->container->get("pannelloamministrazione.commands"); |
||
247 | $result = $command->clearcache(); |
||
248 | |||
249 | (new LockSystem($this->container))->lockFile(false); |
||
250 | |||
251 | /* Uso exit perchè new response avendo cancellato la cache schianta non avendo più a disposizione i file */ |
||
252 | //return $commanddev . '<br/>' . $cmdoutputdev . '<br/><br/>' . $commandprod . '<br/>' . $cmdoutputprod; |
||
253 | //return new Response(nl2br($result)); |
||
254 | exit(nl2br($result)); |
||
255 | } |
||
256 | } |
||
257 | |||
258 | /* CLEAR CACHE */ |
||
259 | |||
260 | public function symfonyCommandAction(Request $request) |
||
288 | |||
289 | /** |
||
290 | * Suppress PMD warnings per exit. |
||
291 | * |
||
292 | * @SuppressWarnings(PHPMD) |
||
293 | */ |
||
294 | public function unixCommandAction(Request $request) |
||
358 | |||
359 | public function phpunittestAction(Request $request) |
||
400 | } |
||
401 |
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.