Conditions | 46 |
Paths | 80 |
Total Lines | 243 |
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 |
||
41 | public function widget_navigation($widget = []) { |
||
42 | $this->load->module('core'); |
||
43 | |||
44 | $segmentGallery = $this->pathGallery(); |
||
45 | if ($this->core->core_data['data_type'] === '404' || !$this->core->core_data['data_type'] || $segmentGallery === 'gallery') { |
||
46 | $data_type = $segmentGallery; |
||
47 | } else { |
||
48 | $data_type = $this->core->core_data['data_type']; |
||
49 | } |
||
50 | |||
51 | switch ($data_type) { |
||
52 | case 'category': |
||
53 | $cur_category = $this->core->cat_content; |
||
54 | $path_categories = $this->lib_category->get_category(array_keys($cur_category['path'])); |
||
55 | |||
56 | $tpl_data = ['navi_cats' => $path_categories]; |
||
57 | |||
58 | return $this->template->fetch('widgets/' . $widget['name'], $tpl_data); |
||
59 | |||
60 | case 'page': |
||
61 | $cur_category = $this->core->cat_content; |
||
62 | $path_categories = $this->lib_category->get_category(array_keys($cur_category['path'])); |
||
63 | |||
64 | // Insert Page data |
||
65 | $path_categories[] = [ |
||
66 | 'path_url' => $this->core->page_content['cat_url'] . $this->core->page_content['url'], |
||
67 | 'name' => $this->core->page_content['title'], |
||
68 | ]; |
||
69 | |||
70 | $tpl_data = ['navi_cats' => $path_categories]; |
||
71 | |||
72 | return $this->template->fetch('widgets/' . $widget['name'], $tpl_data); |
||
73 | |||
74 | case 'brand': |
||
75 | if ($this->core->core_data['id'] != null) { |
||
76 | $brand = SBrandsQuery::create()->setComment(__METHOD__)->joinWithI18n(MY_Controller::getCurrentLocale())->findOneById($this->core->core_data['id']); |
||
77 | |||
78 | $navi_cats[] = [ |
||
|
|||
79 | 'path_url' => 'shop/brand/', |
||
80 | 'name' => lang('Brands', 'navigation'), |
||
81 | ]; |
||
82 | $navi_cats[] = [ |
||
83 | 'path_url' => 'shop/brand/' . $brand->getUrl(), |
||
84 | 'name' => $brand->getName(), |
||
85 | ]; |
||
86 | |||
87 | $tpl_data = ['navi_cats' => $navi_cats]; |
||
88 | return $this->template->fetch('widgets/' . $widget['name'], $tpl_data); |
||
89 | } else { |
||
90 | |||
91 | if ($data_type == 'brand') { |
||
92 | return $this->make(lang('Brands', 'navigation'), 'shop/brand/', $widget); |
||
93 | } |
||
94 | } |
||
95 | break; |
||
96 | case 'compare'; |
||
97 | return $this->make(lang('Compare', 'navigation'), 'shop/compare/', $widget); |
||
98 | case 'order'; |
||
99 | return $this->make(lang('Order details', 'navigation'), 'shop/order/', $widget); |
||
100 | case 'wish_list': |
||
101 | return $this->make(lang('Wish list', 'navigation'), 'shop/wish_list/', $widget); |
||
102 | |||
103 | case 'profile': |
||
104 | return $this->make(lang('Profile', 'navigation'), 'shop/profile/', $widget); |
||
105 | |||
106 | case 'search': |
||
107 | return $this->make(lang('Search', 'navigation'), 'shop/search/', $widget); |
||
108 | |||
109 | case 'callbacks': |
||
110 | return $this->make(lang('Callbacks', 'navigation'), 'callbacks', $widget); |
||
111 | |||
112 | case 'shop': |
||
113 | |||
114 | if ($this->uri->uri_string() == 'shop/search') { |
||
115 | |||
116 | return $this->make(lang('Search', 'navigation'), 'shop/search', $widget); |
||
117 | } |
||
118 | return $this->make(lang('Compare', 'navigation'), 'shop/compare', $widget); |
||
119 | |||
120 | case 'wishlist': |
||
121 | return $this->make(lang('Wishlist', 'navigation'), 'wishlist', $widget); |
||
122 | |||
123 | case 'cart': |
||
124 | return $this->make(lang('Cart', 'navigation'), 'shop/cart/', $widget); |
||
125 | |||
126 | case 'feedback': |
||
127 | return $this->make(lang('Feedback', 'navigation'), 'feedback', $widget); |
||
128 | |||
129 | case 'action_type': |
||
130 | return $this->make(lang('Action type', 'navigation'), 'shop/action_type/show', $widget); |
||
131 | |||
132 | case 'shop_category': |
||
133 | if ($this->core->core_data['id'] !== null && $this->core->core_data > 0) { |
||
134 | |||
135 | $category = SCategoryQuery::create()->setComment(__METHOD__)->findOneById($this->core->core_data['id']); |
||
136 | $categories = $category->buildCategoryPath(Criteria::ASC, MY_Controller::getCurrentLocale()); |
||
137 | $paths = []; |
||
138 | |||
139 | foreach ($categories as $category) { |
||
140 | $paths[] = [ |
||
141 | 'path_url' => $category->getRouteUrl(), |
||
142 | 'name' => $category->getName(), |
||
143 | ]; |
||
144 | } |
||
145 | |||
146 | $tpl_data = ['navi_cats' => $paths]; |
||
147 | return $this->template->fetch('widgets/' . $widget['name'], $tpl_data); |
||
148 | } else { |
||
149 | throw new Exception('Category not found'); |
||
150 | } |
||
151 | |||
152 | break; |
||
153 | case 'product': |
||
154 | if ($this->core->core_data['id'] != null && $this->core->core_data['id'] > 0) { |
||
155 | //get product model |
||
156 | $product = SProductsQuery::create() |
||
157 | ->joinWithI18n(MY_Controller::getCurrentLocale()) |
||
158 | ->findOneById($this->core->core_data['id']); |
||
159 | |||
160 | if ($product) { |
||
161 | |||
162 | if ($product->getCategoryId() == null && $product->getCategoryId() == 0) { |
||
163 | throw new Exception('Category not found'); |
||
164 | } |
||
165 | |||
166 | $category = SCategoryQuery::create()->setComment(__METHOD__)->findOneById($product->getCategoryId()); |
||
167 | $categories = $category->buildCategoryPath(Criteria::ASC, MY_Controller::getCurrentLocale()); |
||
168 | |||
169 | foreach ($categories as $category) { |
||
170 | $path[] = [ |
||
171 | 'path_url' => $category->getRouteUrl(), |
||
172 | 'name' => $category->getName(), |
||
173 | ]; |
||
174 | } |
||
175 | |||
176 | $path[] = [ |
||
177 | 'path_url' => '', |
||
178 | 'name' => $product->getName(), |
||
179 | ]; |
||
180 | |||
181 | $tpl_data = ['navi_cats' => $path]; |
||
182 | return $this->template->fetch('widgets/' . $widget['name'], $tpl_data); |
||
183 | } else { |
||
184 | throw new Exception('Product not found'); |
||
185 | } |
||
186 | } |
||
187 | break; |
||
188 | case 'gallery': |
||
189 | $uri_segments = $this->uri->segment_array(); |
||
190 | $template_vars = $this->template->get_vars(); |
||
191 | |||
192 | $path = []; |
||
193 | foreach ($uri_segments as $segment) { |
||
194 | switch ($segment) { |
||
195 | case 'gallery': |
||
196 | $path[] = [ |
||
197 | 'path_url' => $segment, |
||
198 | 'name' => lang('Gallery', 'navigation'), |
||
199 | ]; |
||
200 | break; |
||
201 | case 'category': |
||
202 | $path[] = [ |
||
203 | 'path_url' => 'gallery/category/' . $template_vars['current_category']['id'], |
||
204 | 'name' => $template_vars['current_category']['name'], |
||
205 | ]; |
||
206 | break; |
||
207 | case 'album': |
||
208 | $path[] = [ |
||
209 | 'path_url' => 'gallery/category/' . $template_vars['current_category']['id'], |
||
210 | 'name' => $template_vars['current_category']['name'], |
||
211 | ]; |
||
212 | |||
213 | $path[] = [ |
||
214 | 'path_url' => "gallery/$segment/" . $template_vars['album']['id'], |
||
215 | 'name' => $template_vars['album']['name'], |
||
216 | ]; |
||
217 | break; |
||
218 | case 'albums': |
||
219 | $path[] = [ |
||
220 | 'path_url' => "gallery/$segment/", |
||
221 | 'name' => lang('All albums', 'navigation'), |
||
222 | ]; |
||
223 | break; |
||
224 | } |
||
225 | } |
||
226 | |||
227 | $tpl_data = ['navi_cats' => $path]; |
||
228 | return $this->template->fetch('widgets/' . $widget['name'], $tpl_data); |
||
229 | |||
230 | case 'auth': |
||
231 | $uri_segments = $this->uri->segment_array(); |
||
232 | |||
233 | $path = []; |
||
234 | foreach ($uri_segments as $segment) { |
||
235 | switch ($segment) { |
||
236 | case 'auth': |
||
237 | $path[] = [ |
||
238 | 'path_url' => $segment, |
||
239 | 'name' => lang('Login', 'navigation'), |
||
240 | ]; |
||
241 | break; |
||
242 | case 'register': |
||
243 | $path = []; |
||
244 | $path[] = [ |
||
245 | 'path_url' => "auth/$segment", |
||
246 | 'name' => lang('Registration', 'navigation'), |
||
247 | ]; |
||
248 | break; |
||
249 | case 'activate': |
||
250 | $path = []; |
||
251 | $path[] = [ |
||
252 | 'path_url' => "auth/$segment", |
||
253 | 'name' => lang('Activation', 'navigation'), |
||
254 | ]; |
||
255 | break; |
||
256 | case 'forgot_password': |
||
257 | $path = []; |
||
258 | $path[] = [ |
||
259 | 'path_url' => "auth/$segment", |
||
260 | 'name' => lang('Remind password', 'navigation'), |
||
261 | ]; |
||
262 | break; |
||
263 | case 'reset_password': |
||
264 | $path = []; |
||
265 | $path[] = [ |
||
266 | 'path_url' => "auth/$segment", |
||
267 | 'name' => lang('Reset password', 'navigation'), |
||
268 | ]; |
||
269 | break; |
||
270 | case 'change_password': |
||
271 | $path = []; |
||
272 | $path[] = [ |
||
273 | 'path_url' => "auth/$segment", |
||
274 | 'name' => lang('Change password', 'navigation'), |
||
275 | ]; |
||
276 | break; |
||
277 | } |
||
278 | } |
||
279 | |||
280 | $tpl_data = ['navi_cats' => $path]; |
||
281 | return $this->template->fetch('widgets/' . $widget['name'], $tpl_data); |
||
282 | } |
||
283 | } |
||
284 | |||
303 | /* End of file widgets.php */ |
Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.
Let’s take a look at an example:
As you can see in this example, the array
$myArray
is initialized the first time when the foreach loop is entered. You can also see that the value of thebar
key is only written conditionally; thus, its value might result from a previous iteration.This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.