Conditions | 8 |
Paths | 20 |
Total Lines | 80 |
Code Lines | 57 |
Lines | 0 |
Ratio | 0 % |
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 |
||
21 | public function handle() |
||
22 | { |
||
23 | // GET-request, probably browser --> show HTML |
||
24 | $arch = trim($_GET['arch']); |
||
25 | $channel = trim($_GET['channel']); |
||
26 | if ($channel != 'beta') { |
||
27 | $channel = 'stable'; |
||
28 | } |
||
29 | $fullList = trim($_GET['fulllist']); |
||
30 | $packagesAvailable = array(); |
||
31 | |||
32 | $mustache = new \Mustache_Engine(array( |
||
33 | 'loader' => new \Mustache_Loader_FilesystemLoader($this->config->basePath . '/data/templates'), |
||
34 | 'partials_loader' => new \Mustache_Loader_FilesystemLoader($this->config->basePath . '/data/templates/partials'), |
||
35 | 'charset' => 'utf-8', |
||
36 | 'logger' => new \Mustache_Logger_StreamLogger('php://stderr'), |
||
37 | )); |
||
38 | |||
39 | $tpl_vars = array( |
||
40 | 'siteName' => $this->config->site['name'], |
||
41 | 'arch' => $arch, |
||
42 | 'channel' => ($channel == 'beta'), |
||
43 | 'requestUri' => $_SERVER['REQUEST_URI'], |
||
44 | 'baseUrl' => $this->config->baseUrl, |
||
45 | 'fullList' => $fullList, |
||
46 | ); |
||
47 | |||
48 | if ($arch) { |
||
49 | // Architecture is set --> show packages for that arch |
||
50 | $pkgs = new PackageFinder($this->config->paths['packages']); |
||
51 | $pkgf = new PackageFilter($pkgs->getAllPackages()); |
||
52 | $pkgf->setArchitectureFilter($arch); |
||
53 | $pkgf->setChannelFilter($channel); |
||
54 | $pkgf->setFirmwareVersionFilter(false); |
||
55 | $pkgf->setOldVersionFilter(true); |
||
56 | $filteredPkgList = $pkgf->getFilteredPackageList(); |
||
57 | |||
58 | $uf = new UrlFixer($this->config->baseUrl); |
||
59 | $uf->fixPackageList($filteredPkgList); |
||
60 | |||
61 | $packages = array(); |
||
62 | foreach ($filteredPkgList as $pkg) { |
||
63 | $packages[] = $pkg->getMetadata(); |
||
64 | } |
||
65 | |||
66 | $tpl_vars['packagelist'] = array_values($packages); |
||
67 | $tpl = $mustache->loadTemplate('html_packagelist'); |
||
68 | } elseif ($fullList) { |
||
69 | // No architecture, but full list of packages requested --> show simple list |
||
70 | $pkgs = new PackageFinder($this->config->paths['packages']); |
||
71 | $packagesList = $pkgs->getAllPackageFiles(); |
||
72 | |||
73 | // Prepare data for template |
||
74 | $packages = array(); |
||
75 | foreach ($packagesList as $spkFile) { |
||
76 | $packages[] = array( |
||
77 | 'url' => $baseUrl . $spkFile, |
||
78 | 'filename' => basename($spkFile), |
||
79 | ); |
||
80 | } |
||
81 | $tpl_vars['packagelist'] = $packages; |
||
82 | $tpl = $mustache->loadTemplate('html_packagelist_all'); |
||
83 | } else { |
||
84 | // Nothing requested --> show models overview |
||
85 | try { |
||
86 | $deviceList = new DeviceList($this->config->paths['models']); |
||
87 | $models = $deviceList->getDevices(); |
||
88 | if (count($models) == 0) { |
||
89 | $tpl = $mustache->loadTemplate('html_modellist_none'); |
||
90 | } else { |
||
91 | $tpl_vars['modellist'] = $models; |
||
92 | $tpl = $mustache->loadTemplate('html_modellist'); |
||
93 | } |
||
94 | } catch (\Exception $e) { |
||
95 | $tpl_vars['errorMessage'] = $e->getMessage(); |
||
96 | $tpl = $mustache->loadTemplate('html_modellist_error'); |
||
97 | } |
||
98 | } |
||
99 | echo $tpl->render($tpl_vars); |
||
100 | } |
||
101 | } |
||
102 |
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.