Conditions | 28 |
Paths | 1251 |
Total Lines | 140 |
Code Lines | 98 |
Lines | 0 |
Ratio | 0 % |
Changes | 4 | ||
Bugs | 0 | Features | 2 |
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 getPage() |
||
|
|||
38 | { |
||
39 | if (HelperConfig::$core['maintenancemode']) { |
||
40 | try { |
||
41 | $controller = new \HaaseIT\HCSF\Controller\Maintenance($this->serviceManager); |
||
42 | $this->P = $controller->getPage(); |
||
43 | } catch (\Exception $e) { |
||
44 | $this->P = $e->getMessage(); |
||
45 | } |
||
46 | } else { |
||
47 | $map = [ |
||
48 | '/_admin/index.html' => 'Admin\\Index', |
||
49 | '/_admin/' => 'Admin\\Index', |
||
50 | '/_admin' => 'Admin\\Index', |
||
51 | '/_admin/cleartemplatecache.html' => 'Admin\\ClearTemplateCache', |
||
52 | '/_admin/clearimagecache.html' => 'Admin\\ClearImageCache', |
||
53 | '/_admin/phpinfo.html' => 'Admin\\Phpinfo', |
||
54 | '/_admin/pageadmin.html' => 'Admin\\Pageadmin', |
||
55 | '/_admin/textcatadmin.html' => 'Admin\\Textcatadmin', |
||
56 | '/_admin/customeradmin.html' => 'Admin\\Customer\\Customeradmin', |
||
57 | '/_admin/itemadmin.html' => 'Admin\\Shop\\Itemadmin', |
||
58 | '/_admin/shopadmin.html' => 'Admin\\Shop\\Shopadmin', |
||
59 | '/_admin/shopadmin_export.csv' => 'Admin\\Shop\\ShopadminExportCSV', |
||
60 | '/_admin/itemgroupadmin.html' => 'Admin\\Shop\\Itemgroupadmin', |
||
61 | '/_admin/dbstatus.html' => 'Admin\\DBStatus', |
||
62 | '/_misc/login.html' => 'Customer\\Login', |
||
63 | '/_misc/logout.html' => 'Customer\\Logout', |
||
64 | '/_misc/userhome.html' => 'Customer\\Userhome', |
||
65 | '/_misc/register.html' => 'Customer\\Register', |
||
66 | '/_misc/forgotpassword.html' => 'Customer\\Forgotpassword', |
||
67 | '/_misc/rp.html' => 'Customer\\Resetpassword', |
||
68 | '/_misc/verifyemail.html' => 'Customer\\Verifyemail', |
||
69 | '/_misc/resendverificationmail.html' => 'Customer\\Resendverificationmail', |
||
70 | '/_misc/myorders.html' => 'Shop\\Myorders', |
||
71 | '/_misc/itemsearch.html' => 'Shop\\Itemsearch', |
||
72 | '/_misc/checkedout.html' => 'Shop\\Checkedout', |
||
73 | '/_misc/updateshippingcost.html' => 'Shop\\Updateshippingcost', |
||
74 | '/_misc/shoppingcart.html' => 'Shop\\Shoppingcart', |
||
75 | '/_misc/update-cart.html' => 'Shop\\Updatecart', |
||
76 | '/_misc/sofortueberweisung.html' => 'Shop\\Sofortueberweisung', |
||
77 | '/_misc/paypal.html' => 'Shop\\Paypal', |
||
78 | '/_misc/paypal_notify.html' => 'Shop\\Paypalnotify', |
||
79 | ]; |
||
80 | if (HelperConfig::$core['enable_sandbox']) { |
||
81 | $map['/_misc/sandbox.html'] = 'Sandbox'; // dev sandbox for testing new functionality |
||
82 | } |
||
83 | $this->P = 404; |
||
84 | $aURL = parse_url($this->serviceManager->get('request')->getRequestTarget()); |
||
85 | $this->sPath = $aURL["path"]; |
||
86 | |||
87 | $aPath = explode('/', $this->sPath); |
||
88 | if (!empty($map[$this->sPath])) { |
||
89 | $class = '\\HaaseIT\\HCSF\\Controller\\' . $map[$this->sPath]; |
||
90 | } else { |
||
91 | if ($aPath[1] == HelperConfig::$core['directory_images']) { |
||
92 | $class = '\\HaaseIT\\HCSF\\Controller\\Glide'; |
||
93 | } |
||
94 | } |
||
95 | |||
96 | if (!empty($class)) { |
||
97 | try { |
||
98 | $controller = new $class($this->serviceManager, $aPath); |
||
99 | $this->P = $controller->getPage(); |
||
100 | } catch (\Exception $e) { |
||
101 | $this->P = 500; |
||
102 | // todo: write error message |
||
103 | //echo $e->getMessage(); |
||
104 | |||
105 | } |
||
106 | } else { |
||
107 | if (HelperConfig::$core["enable_module_shop"]) { |
||
108 | $aRoutingoverride = $this->getRoutingoverride($aPath); |
||
109 | } |
||
110 | |||
111 | $this->P = new UserPage($this->serviceManager, $this->sPath); |
||
112 | |||
113 | // go and look if the page can be loaded yet |
||
114 | if ($this->P->cb_id == NULL) { |
||
115 | /* |
||
116 | If the last part of the path doesn't include a dot (.) and is not empty, apend a slash. |
||
117 | If there is already a slash at the end, the last part of the path array will be empty. |
||
118 | */ |
||
119 | if (mb_strpos($aPath[count($aPath) - 1], '.') === false && $aPath[count($aPath) - 1] != '') { |
||
120 | $this->sPath .= '/'; |
||
121 | } |
||
122 | |||
123 | if ($this->sPath[strlen($this->sPath) - 1] === '/') { |
||
124 | $this->sPath .= 'index.html'; |
||
125 | } |
||
126 | |||
127 | $this->P = new UserPage($this->serviceManager, $this->sPath); |
||
128 | } |
||
129 | |||
130 | if ($this->P->cb_id == NULL) { // if the page is still not found, unset the page object |
||
131 | $this->P = 404; |
||
132 | } else { // if it is found, go on |
||
133 | // Support for shorturls |
||
134 | if ($this->P->cb_pagetype === 'shorturl') { |
||
135 | header('Location: ' . $this->P->cb_pageconfig, true, 302); |
||
136 | exit(); |
||
137 | } |
||
138 | |||
139 | if (isset($this->P, $aRoutingoverride) && count($aRoutingoverride)) { |
||
140 | $this->P->cb_pagetype = $aRoutingoverride["cb_pagetype"]; |
||
141 | $this->P->cb_pageconfig->itemno = $aRoutingoverride["itemno"]; |
||
142 | } |
||
143 | } |
||
144 | } |
||
145 | |||
146 | if (!is_object($this->P) && $this->P == 404) { |
||
147 | $this->P = new CorePage($this->serviceManager); |
||
148 | $this->P->cb_pagetype = 'error'; |
||
149 | $this->P->iStatus = 404; |
||
150 | |||
151 | $this->P->oPayload->cl_html = $this->serviceManager->get('textcats')->T("misc_page_not_found"); |
||
152 | header($_SERVER["SERVER_PROTOCOL"] . " 404 Not Found"); |
||
153 | } elseif (!is_object($this->P) && $this->P == 500) { |
||
154 | $this->P = new CorePage($this->serviceManager); |
||
155 | $this->P->cb_pagetype = 'error'; |
||
156 | $this->P->iStatus = 500; |
||
157 | |||
158 | $this->P->oPayload->cl_html = $this->serviceManager->get('textcats')->T("misc_server_error"); |
||
159 | header($_SERVER["SERVER_PROTOCOL"] . " 500 Internal Server Error"); |
||
160 | } elseif (is_object($this->P) && $this->P->oPayload == NULL) {// elseif the page has been found but contains no payload... |
||
161 | if ( |
||
162 | !( |
||
163 | $this->P->cb_pagetype === 'itemoverview' |
||
164 | || $this->P->cb_pagetype === 'itemoverviewgrpd' |
||
165 | || $this->P->cb_pagetype === 'itemdetail' |
||
166 | ) |
||
167 | ) { // no payload is fine if page is one of these |
||
168 | $this->P->oPayload->cl_html = $this->serviceManager->get('textcats')->T("misc_content_not_found"); |
||
169 | header($_SERVER["SERVER_PROTOCOL"] . " 404 Not Found"); |
||
170 | } |
||
171 | } elseif ($this->P->oPayload->cl_lang != NULL && $this->P->oPayload->cl_lang != HelperConfig::$lang) { // if the page is available but not in the current language, display info |
||
172 | $this->P->oPayload->cl_html = $this->serviceManager->get('textcats')->T("misc_page_not_available_lang") . '<br><br>' . $this->P->oPayload->cl_html; |
||
173 | } |
||
174 | } |
||
175 | return $this->P; |
||
176 | } |
||
177 | |||
212 |
Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable: