breadcrumb_helper.php ➔ set_breadcrumb()   F
last analyzed

Complexity

Conditions 73
Paths > 20000

Size

Total Lines 343
Code Lines 163

Duplication

Lines 48
Ratio 13.99 %

Importance

Changes 0
Metric Value
cc 73
eloc 163
nc 121651200
nop 3
dl 48
loc 343
rs 2
c 0
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

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:

1
<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
2
3
/**
4
 * Breadcrumb helper
5
 *
6
 * Features:
7
 * - Easy integration. Just put <?php echo set_breadcrumb(); ?> in view files.
8
 * - Can use any delimiter.
9
 * - Easy to replace any unproper link name.
10
 * - Easy to hide any link by it's name or number segment.
11
 * - Return the breadcrumb in a list (<ul><li></li></ul>) or something else.
12
 * - Auto link beauty.
13
 * - Can unlink last segment of breadcrumb.
14
 * - Multilanguage support.
15
 * - Many more...
16
 *
17
 * Installation:
18
 * 1. Put breadcrumb_helper.php to application/helpers.
19
 * 2. Put breadcrumb.php to application/config.
20
 * 3. Load the helper either in your controller or in autoload config.
21
 *     In your controller   : $this->load->helper('breadcrumb') OR
22
 *     In autoload  : $autoload['helper'] = array('breadcrumb')
23
 * 4. Add these line to your view file: <?php echo set_breadcrumb(); ?>. I suggest that you put it on master template
24
 *     so that it can save time as you don't need to add text in every view page.
25
 * 5. Change the configuration as you need.
26
 *
27
 * @package     Breadcrumb
28
 * @subpackage  Helpers
29
 * @category    Helpers
30
 * @author          Ardinoto Wahono
31
 * @version         12.05.1 (CI 2.x & CI 1.x. Published on April,01 2012, minor release 1)
32
 * @copyright       Copyright (c) 2009-2012 Ardinoto Wahono, WAH-IT Web Division
33
 *
34
 * Permission is hereby granted, free of charge, to any person obtaining a copy
35
 * of this software and associated documentation files (the "Software"), to deal
36
 * in the Software without restriction, including without limitation the rights
37
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
38
 * copies of the Software, and to permit persons to whom the Software is
39
 * furnished to do so, subject to the following conditions:
40
 *
41
 * The above copyright notice and this permission notice shall be included in
42
 * all copies or substantial portions of the Software.
43
 *
44
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
45
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
46
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
47
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
48
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
49
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
50
 * THE SOFTWARE.
51
 */
52
53
/**
54
 * Patch for PHP 5 - 5.2
55
 * @author  Kromack
56
 * @link    http://codeigniter.com/forums/viewreply/694827/
57
 */
58
if (!function_exists('array_replace'))
59
{
60
    function array_replace(array &$array, array &$array1)
61
    {
62
        foreach($array as $k=>$v)
63
        {
64
            if(array_key_exists($k, $array1))
65
            {
66
                $array[$k] = $array1[$k];
67
            }
68
        }
69
        return $array;
70
    }
71
}
72
73
if ( ! function_exists('set_breadcrumb'))
74
{
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;
0 ignored issues
show
Bug introduced by
The variable $prep_exclude does not seem to be defined for all execution paths leading up to this point.

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:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

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

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
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);
0 ignored issues
show
Unused Code Comprehensibility introduced by
65% of this comment could be valid code. Did you maybe forget this after debugging?

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.

Loading history...
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);
0 ignored issues
show
Unused Code introduced by
$number_added_value_array is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
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'))
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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.

Loading history...
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))
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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.

Loading history...
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'))
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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.

Loading history...
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))
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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.

Loading history...
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;
0 ignored issues
show
Bug introduced by
The variable $str_link does not seem to be defined for all execution paths leading up to this point.

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:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

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

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
375
                $str_name[] = ucwords($v);
0 ignored issues
show
Bug introduced by
The variable $str_name does not seem to be defined for all execution paths leading up to this point.

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:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

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

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
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();
0 ignored issues
show
Unused Code introduced by
The call to the function clear_breadcrumb() seems unnecessary as the function has no side-effects.
Loading history...
416
        return $str;
417
    }
418
}
419
420
if ( ! function_exists('clear_breadcrumb'))
421
{
422
    function clear_breadcrumb()
423
    {
424
        unset($wrapper_inline);
425
        unset($wrapper);
426
    }
427
}
428
429
/* End of file breadcrumb_helper.php */
430
/* Location: ./application/helpers/breadcrumb_helper.php */
431