Conditions | 32 |
Paths | > 20000 |
Total Lines | 129 |
Lines | 6 |
Ratio | 4.65 % |
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 |
||
43 | public function getPage() |
||
44 | { |
||
45 | // Maintenance page |
||
46 | if ($this->config->getCore('maintenancemode')) { |
||
47 | try { |
||
48 | $controller = new \HaaseIT\HCSF\Controller\Maintenance($this->serviceManager); |
||
49 | $this->P = $controller->getPage(); |
||
50 | } catch (\Exception $e) { |
||
51 | $this->P = $e->getMessage(); |
||
52 | } |
||
53 | } else { |
||
54 | $routes = require __DIR__.DIRECTORY_SEPARATOR.'config'.DIRECTORY_SEPARATOR.'routes.php'; |
||
55 | $customRoutesFile = PATH_BASEDIR . 'customization/routes.yml'; |
||
56 | if (is_file($customRoutesFile)) { |
||
57 | try{ |
||
58 | $customRoutes = Yaml::parse(file_get_contents($customRoutesFile)); |
||
59 | View Code Duplication | if (!empty($customRoutes['literal'])) { |
|
|
|||
60 | $routes['literal'] = array_merge($routes['literal'], $customRoutes['literal']); |
||
61 | } |
||
62 | View Code Duplication | if (!empty($customRoutes['regex'])) { |
|
63 | $routes['regex'] = array_merge($routes['regex'], $customRoutes['regex']); |
||
64 | } |
||
65 | } catch (\Exception $e) { |
||
66 | // todo: log error |
||
67 | } |
||
68 | } |
||
69 | $aURL = parse_url($this->helper->getCleanRequestTarget()); |
||
70 | $this->sPath = $aURL['path']; |
||
71 | |||
72 | $aPath = explode('/', $this->sPath); |
||
73 | if (!empty($routes['literal'][$this->sPath])) { |
||
74 | $class = '\\HaaseIT\\HCSF\\Controller\\'.$routes['literal'][$this->sPath]; |
||
75 | } else { |
||
76 | if (!empty($routes['regex'])) { |
||
77 | foreach ($routes['regex'] as $regex) { |
||
78 | $result = preg_match('(^' . $regex['regex'] . '$)', $this->sPath, $matches); |
||
79 | if ($result) { |
||
80 | $class = '\\HaaseIT\\HCSF\\Controller\\' . $regex['controller']; |
||
81 | break; |
||
82 | } |
||
83 | } |
||
84 | } |
||
85 | if (empty($result) && $aPath[1] === $this->config->getCore('directory_images')) { |
||
86 | $class = Controller\Glide::class; |
||
87 | } |
||
88 | } |
||
89 | |||
90 | if (!empty($class)) { |
||
91 | // Core Page |
||
92 | try { |
||
93 | /** @var Controller\Base $controller */ |
||
94 | $controller = new $class($this->serviceManager, $aPath, (!empty($matches) ? $matches : false)); |
||
95 | $this->P = $controller->getPage(); |
||
96 | } catch (\Exception $e) { |
||
97 | $this->P = new Page(); |
||
98 | // die($e->getMessage()); |
||
99 | $this->P->setStatus(500); |
||
100 | // todo: write error message |
||
101 | //echo $e->getMessage(); |
||
102 | } |
||
103 | } else { |
||
104 | if ($this->config->getCore('enable_module_shop')) { |
||
105 | $aRoutingoverride = $this->getRoutingoverride($aPath); |
||
106 | } |
||
107 | |||
108 | $this->P = new UserPage($this->serviceManager, $this->sPath); |
||
109 | |||
110 | // go and look if the page can be loaded yet |
||
111 | if ($this->P->cb_id === NULL) { |
||
112 | /* |
||
113 | If the last part of the path doesn't include a dot (.) and is not empty, apend a slash. |
||
114 | If there is already a slash at the end, the last part of the path array will be empty. |
||
115 | */ |
||
116 | if (mb_strpos($aPath[count($aPath) - 1], '.') === false && $aPath[count($aPath) - 1] !== '') { |
||
117 | $this->sPath .= '/'; |
||
118 | } |
||
119 | |||
120 | if ($this->sPath[strlen($this->sPath) - 1] === '/') { |
||
121 | $this->sPath .= 'index.html'; |
||
122 | } |
||
123 | |||
124 | $this->P = new UserPage($this->serviceManager, $this->sPath); |
||
125 | } |
||
126 | |||
127 | if ($this->P->cb_id === NULL) { // if the page is still not found, unset the page object |
||
128 | $this->P->setStatus(404); |
||
129 | } else { // if it is found, go on |
||
130 | // Support for shorturls |
||
131 | if ($this->P->cb_pagetype === 'shorturl') { |
||
132 | $this->P->setStatus(302); |
||
133 | $this->P->setHeader('Location', $this->P->cb_pageconfig); |
||
134 | } |
||
135 | |||
136 | if (isset($this->P, $aRoutingoverride) && count($aRoutingoverride)) { |
||
137 | $this->P->cb_pagetype = $aRoutingoverride['cb_pagetype']; |
||
138 | $this->P->cb_pageconfig->itemno = $aRoutingoverride['itemno']; |
||
139 | } |
||
140 | } |
||
141 | } |
||
142 | |||
143 | if ($this->P->getStatus() === 404) { |
||
144 | $this->P = new CorePage($this->serviceManager); |
||
145 | $this->P->cb_pagetype = 'error'; |
||
146 | $this->P->setStatus(404); |
||
147 | $this->P->oPayload->cl_html = $this->serviceManager->get('textcats')->T('misc_page_not_found'); |
||
148 | } elseif ($this->P->getStatus() === 500) { |
||
149 | $this->P = new CorePage($this->serviceManager); |
||
150 | $this->P->cb_pagetype = 'error'; |
||
151 | $this->P->setStatus(500); |
||
152 | $this->P->oPayload->cl_html = $this->serviceManager->get('textcats')->T('misc_server_error'); |
||
153 | } elseif (is_object($this->P) && $this->P->oPayload === null) {// elseif the page has been found but contains no payload... |
||
154 | // no payload is fine if page is one of these |
||
155 | $pagetypesnocontent = [ |
||
156 | 'itemoverviewjson', |
||
157 | 'itemoverview', |
||
158 | 'itemoverviewgrpd', |
||
159 | 'itemdetail', |
||
160 | 'shorturl', |
||
161 | ]; |
||
162 | if (!in_array($this->P->cb_pagetype, $pagetypesnocontent, true)) { |
||
163 | $this->P->oPayload->cl_html = $this->serviceManager->get('textcats')->T('misc_content_not_found'); |
||
164 | $this->P->setStatus(404); |
||
165 | } |
||
166 | } elseif ($this->P->oPayload->cl_lang !== null && $this->P->oPayload->cl_lang !== $this->config->getLang()) { // if the page is available but not in the current language, display info |
||
167 | $this->P->oPayload->cl_html = $this->serviceManager->get('textcats')->T('misc_page_not_available_lang').'<br><br>'.$this->P->oPayload->cl_html; |
||
168 | } |
||
169 | } |
||
170 | return $this->P; |
||
171 | } |
||
172 | |||
207 |
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.