Conditions | 73 |
Paths | > 20000 |
Total Lines | 343 |
Code Lines | 163 |
Lines | 48 |
Ratio | 13.99 % |
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 if ( ! defined('BASEPATH')) exit('No direct script access allowed'); |
||
75 | function set_breadcrumb($delimiter_config = '', $exclude = '', $config= array()) |
||
76 | { |
||
77 | $CI =& get_instance(); |
||
78 | $CI->load->helper('url'); |
||
79 | $CI->lang->load('breadcrumb'); |
||
80 | $CI->config->load('breadcrumb'); |
||
81 | |||
82 | // Load configuration |
||
83 | $ci_version = $CI->config->item('codeigniter_version'); |
||
84 | |||
85 | /* --- Chris Miller's Patch --- */ |
||
86 | /* Go to this link http://codeigniter.com/forums/viewthread/137949/P190 |
||
87 | * for guidance. |
||
88 | * @author Chris Miller |
||
89 | */ |
||
90 | $attr_home = ( isset( $config['attr_home'] ) |
||
91 | ? $config['attr_home'] |
||
92 | : $CI->config->item('attr_home') ); |
||
93 | |||
94 | $unlink_home = ( isset( $config['unlike_home'] ) |
||
95 | ? $config['unlike_home'] |
||
96 | : $CI->config->item('unlike_home') ); |
||
97 | |||
98 | if (empty($exclude)) |
||
99 | { |
||
100 | $exclude = $CI->config->item('exclude'); |
||
101 | } |
||
102 | |||
103 | $exclude_segment = ( isset( $config['exclude_segment'] ) |
||
104 | ? array_merge($CI->config->item('exclude_segment'),$config['exclude_segment']) |
||
105 | : $CI->config->item('exclude_segment') ); |
||
106 | |||
107 | $replacer_default = ( isset( $config['replacer'] ) |
||
108 | ? array_merge($CI->config->item('replacer'),$config['replacer']) |
||
109 | : $CI->config->item('replacer') ); |
||
110 | |||
111 | $replacer_embed = ( isset( $config['replacer_embed'] ) |
||
112 | ? array_merge($CI->config->item('replacer_embed'),$config['replacer_embed']) |
||
113 | : $CI->config->item('replacer_embed') ); |
||
114 | |||
115 | $partial_replace = ( isset( $config['partial_replace'] ) |
||
116 | ? array_merge($CI->config->item('partial_replace'),$config['partial_replace']) |
||
117 | : $CI->config->item('partial_replace') ); |
||
118 | |||
119 | /* --- End Patch --- */ |
||
120 | |||
121 | $uri = rtrim($CI->uri->uri_string(),'/'); |
||
122 | $uri_array_original = explode("/", $uri); |
||
123 | |||
124 | // cahva's fix (http://codeigniter.com/forums/viewreply/855097/) |
||
125 | $uri_array_cnt = count($uri_array_original); |
||
126 | if (config_item('hide_number_on_last_segment') && isset($uri_array_original[$uri_array_cnt-1]) && is_numeric($uri_array_original[$uri_array_cnt-1])) |
||
127 | { |
||
128 | array_pop($uri_array_original); |
||
129 | } |
||
130 | // <-- End cahva's fix (http://codeigniter.com/forums/viewreply/855097/) |
||
131 | |||
132 | // If last segment is a number ? |
||
133 | $show_last_number = -1; |
||
134 | $number_array = count($uri_array_original); |
||
135 | if (! $CI->config->item('hide_number_on_last_segment')) |
||
136 | { |
||
137 | $l_array = $number_array - 1; // last array number |
||
138 | if (preg_match("/^[0-9]/", $uri_array_original[$l_array]) AND ! preg_match("/[a-zA-Z]+/", $uri_array_original[$l_array])) |
||
139 | { |
||
140 | $show_last_number = $l_array; |
||
141 | } |
||
142 | } |
||
143 | |||
144 | // Find segments uri that only contain a number |
||
145 | foreach($uri_array_original as $key => $value) |
||
146 | { |
||
147 | // find number but keep number where positioned in the last segment |
||
148 | if (preg_match("/^[0-9]/", $value) AND ! preg_match("/[a-zA-Z]+/", $value) AND $key != $show_last_number) |
||
149 | { |
||
150 | $uri_array_original[$key] = (int)$value; |
||
151 | |||
152 | // If hide_number is TRUE then set the $exclude_segment array variable; |
||
153 | if ($CI->config->item('hide_number')) |
||
154 | { |
||
155 | $exclude_segment = array_merge($exclude_segment, array($key)); |
||
156 | } |
||
157 | } |
||
158 | } |
||
159 | |||
160 | // Preparing the replacer, add exclude to replacer array |
||
161 | foreach ($exclude as $value) |
||
162 | { |
||
163 | $prep_exclude = array(); |
||
164 | $prep_exclude[$value] = ''; //if exclude then it's value is set to null |
||
165 | } |
||
166 | |||
167 | $replacer = $replacer_default + $prep_exclude; |
||
|
|||
168 | |||
169 | // $replacer_embed usually set on controller; |
||
170 | $replacer = $replacer_embed + $replacer; |
||
171 | |||
172 | // Find uri segment from $replacer and $exclude_segment |
||
173 | $replacer_null = array(); |
||
174 | foreach ($replacer as $key => $value) |
||
175 | { |
||
176 | if (empty($value)) |
||
177 | { |
||
178 | //$replacer_null[] = array_search($key, $uri_array_original, TRUE); |
||
179 | $replacer_null[] = array_search($key, $uri_array_original); |
||
180 | } |
||
181 | } |
||
182 | $skip_key = array_merge($replacer_null, $exclude_segment); |
||
183 | |||
184 | $uri_array = $uri_array_original; |
||
185 | |||
186 | // Change link name as mentioned on $replacer |
||
187 | foreach ($replacer as $key => $value) |
||
188 | { |
||
189 | if ($value && in_array($key, $uri_array_original, TRUE)) |
||
190 | { |
||
191 | $key_uri = array_search($key, $uri_array_original, TRUE); |
||
192 | |||
193 | // Add multilanguage |
||
194 | if (! is_array($value) && $CI->config->item('multilang')) |
||
195 | { |
||
196 | if ($CI->lang->line($value)) { |
||
197 | $value = ucwords($CI->lang->line($value)); |
||
198 | } |
||
199 | } |
||
200 | |||
201 | $replacement = array($key_uri => $value); |
||
202 | |||
203 | $uri_array = array_replace($uri_array, $replacement); |
||
204 | } |
||
205 | } |
||
206 | |||
207 | // Set wrapper |
||
208 | $wrapper = explode("|", $CI->config->item('wrapper')); |
||
209 | $wrapper_inline = explode("|", $CI->config->item('wrapper_inline')); |
||
210 | if ( ! $CI->config->item('use_wrapper')) |
||
211 | { |
||
212 | $wrapper = array('', ''); |
||
213 | $wrapper_inline = array('', ''); |
||
214 | } |
||
215 | |||
216 | // Begin writing breadcrumb string |
||
217 | $init_link = $CI->config->item('set_home'); |
||
218 | if ($init_link != "") |
||
219 | { |
||
220 | if ($CI->config->item('multilang')) |
||
221 | { |
||
222 | $init_link = $CI->lang->line('set_home'); |
||
223 | } |
||
224 | $str_first = $wrapper[0].$wrapper_inline[0].anchor('', $init_link, $attr_home).$wrapper_inline[1]; |
||
225 | if ($unlink_home) |
||
226 | { |
||
227 | $str_first = $wrapper[0].$wrapper_inline[0].$init_link.$wrapper_inline[1]; |
||
228 | } |
||
229 | |||
230 | } else { |
||
231 | $str_first = $wrapper[0]; |
||
232 | } |
||
233 | |||
234 | $segment = ''; |
||
235 | |||
236 | $i = 0; |
||
237 | |||
238 | foreach ($uri_array as $value) |
||
239 | { |
||
240 | if ($i > 0 OR $ci_version == '2.x') |
||
241 | { |
||
242 | $segment .= $uri_array_original[$i].'/'; |
||
243 | |||
244 | // If replace value is an array |
||
245 | if (! in_array($i, $skip_key, TRUE) && is_array($value)) // Skip link if replace value is null |
||
246 | { |
||
247 | $number_added_value_array = count($value); |
||
248 | |||
249 | foreach ($value as $pair_values) |
||
250 | { |
||
251 | $pv_array = explode("|", $pair_values); |
||
252 | $val_url = $pv_array[0]; |
||
253 | $number_pv_array = count($pv_array); |
||
254 | if ($number_pv_array == 1) |
||
255 | { |
||
256 | $val_name = $pv_array[0]; |
||
257 | } |
||
258 | else |
||
259 | { |
||
260 | $val_name = $pv_array[1]; |
||
261 | } |
||
262 | |||
263 | // Add multilanguage |
||
264 | View Code Duplication | if ($CI->config->item('multilang')) |
|
265 | { |
||
266 | if ($CI->lang->line($val_name)) { |
||
267 | $val_name = ucwords($CI->lang->line($val_name)); |
||
268 | } |
||
269 | } |
||
270 | |||
271 | // Look up for partial replace |
||
272 | View Code Duplication | if (! empty($partial_replace)) |
|
273 | { |
||
274 | foreach ($partial_replace as $pkey => $pvalue) |
||
275 | { |
||
276 | if ($CI->config->item('multilang')) |
||
277 | { |
||
278 | $lang_pvalue = $CI->lang->line($pvalue)?$CI->lang->line($pvalue):$CI->lang->line($pkey); |
||
279 | $preplace = ' '.$lang_pvalue.'_'; |
||
280 | |||
281 | } else { |
||
282 | $preplace = ' '.$pvalue.'_'; |
||
283 | } |
||
284 | if (substr_count($val_name, $pkey) > 0) |
||
285 | { |
||
286 | $val_name = str_replace($pkey, $preplace, $val_name); |
||
287 | } |
||
288 | } |
||
289 | } |
||
290 | |||
291 | // Url preparation |
||
292 | // If no url define (array key is empty) |
||
293 | if ($number_pv_array == 1 || $val_url == $uri_array_original[$i]) |
||
294 | { |
||
295 | $new_segment_url = $segment; |
||
296 | } |
||
297 | else if ($val_url[0] == '/') |
||
298 | { |
||
299 | $new_segment_url = base_url().substr($val_url, 1); |
||
300 | } |
||
301 | else |
||
302 | { |
||
303 | $new_segment_url = $segment.$val_url; |
||
304 | } |
||
305 | |||
306 | $str_link = array(); |
||
307 | $str_link[] = $new_segment_url; |
||
308 | $str_name = array(); |
||
309 | $str_name[] = ucwords($val_name); |
||
310 | } |
||
311 | } |
||
312 | else if (! in_array($i, $skip_key, TRUE)) // If value is NOT an array |
||
313 | { |
||
314 | // Add multilanguage |
||
315 | View Code Duplication | if ($CI->config->item('multilang')) |
|
316 | { |
||
317 | if ($CI->lang->line($value)) { |
||
318 | $value = ucwords($CI->lang->line($value)); |
||
319 | } |
||
320 | } |
||
321 | |||
322 | // Look up for partial replace |
||
323 | View Code Duplication | if (! empty($partial_replace)) |
|
324 | { |
||
325 | foreach ($partial_replace as $pkey => $pvalue) |
||
326 | { |
||
327 | if ($CI->config->item('multilang')) |
||
328 | { |
||
329 | $lang_pvalue = $CI->lang->line($pvalue)?$CI->lang->line($pvalue):$CI->lang->line($pkey); |
||
330 | $preplace = ' '.$lang_pvalue.'_'; |
||
331 | |||
332 | } else { |
||
333 | $preplace = ' '.$pvalue.'_'; |
||
334 | } |
||
335 | if (substr_count($value, $pkey) > 0) |
||
336 | { |
||
337 | $value = str_replace($pkey, $preplace, $value); |
||
338 | } |
||
339 | } |
||
340 | } |
||
341 | |||
342 | // Auto link make over |
||
343 | if (strpos($value, "_") OR strpos($value, "-")) |
||
344 | { |
||
345 | $char_to_replace = $CI->config->item('strip_characters'); |
||
346 | $value = ucwords(strtolower(str_replace($char_to_replace, " ", $value))); |
||
347 | if ($CI->config->item('strip_regexp')) |
||
348 | { |
||
349 | foreach($CI->config->item('strip_regexp') as $exp) |
||
350 | { |
||
351 | $value = preg_replace($exp, '', $value); |
||
352 | } |
||
353 | } |
||
354 | } |
||
355 | |||
356 | $str_link = array(); |
||
357 | $str_link[] = $segment; |
||
358 | $str_name = array(); |
||
359 | $str_name[] = ucwords($value); |
||
360 | } |
||
361 | } |
||
362 | $i++; |
||
363 | } |
||
364 | |||
365 | /* --- Chris Miller's Patch --- */ |
||
366 | // Check for custom additions |
||
367 | if ( isset( $config['include_segments'] ) ) { |
||
368 | |||
369 | // Set our variable for usage |
||
370 | $include_segments = $config['include_segments']; |
||
371 | |||
372 | // Loop our config array |
||
373 | foreach ( $include_segments AS $k => $v ) { |
||
374 | $str_link[] = $k; |
||
375 | $str_name[] = ucwords($v); |
||
376 | } |
||
377 | } |
||
378 | /* --- End Patch --- */ |
||
379 | |||
380 | $str_last = $wrapper[1]; |
||
381 | $str = $str_first; |
||
382 | |||
383 | if (isset($str_name)) { |
||
384 | $breadcrumb_number = count($str_name); |
||
385 | |||
386 | if ($breadcrumb_number > 0) { |
||
387 | |||
388 | $i = 0; |
||
389 | |||
390 | foreach ($str_name as $key => $val) { |
||
391 | // If home is hidden then don't show first delimiter |
||
392 | if ( $i == 0 && ($str == '' || $str == $wrapper[0]) ) { |
||
393 | $delimiter = ''; |
||
394 | } elseif (empty($delimiter_config)) { |
||
395 | $delimiter = $CI->config->item('delimiter'); |
||
396 | } else { |
||
397 | $delimiter = $delimiter_config; |
||
398 | } |
||
399 | |||
400 | if ($val != '') { |
||
401 | if ($key == $breadcrumb_number-1 && $CI->config->item('unlink_last_segment')) |
||
402 | { |
||
403 | $str .= $delimiter.$wrapper_inline[0].ucwords($val).$wrapper_inline[1]; |
||
404 | } else { |
||
405 | $str .= $delimiter.$wrapper_inline[0].anchor($str_link[$key], $val).$wrapper_inline[1]; |
||
406 | } |
||
407 | } |
||
408 | |||
409 | $i++; |
||
410 | } |
||
411 | } |
||
412 | } |
||
413 | |||
414 | $str .= $str_last; |
||
415 | clear_breadcrumb(); |
||
416 | return $str; |
||
417 | } |
||
418 | } |
||
431 |
If you define a variable conditionally, it can happen that it is not defined for all execution paths.
Let’s take a look at an example:
In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.
Available Fixes
Check for existence of the variable explicitly:
Define a default value for the variable:
Add a value for the missing path: