Completed
Push — master ( 6c6183...390ac4 )
by Mikael
02:32
created

CNavbar::create()   D

Complexity

Conditions 18
Paths 8

Size

Total Lines 97
Code Lines 58

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 342

Importance

Changes 5
Bugs 1 Features 1
Metric Value
c 5
b 1
f 1
dl 0
loc 97
ccs 0
cts 58
cp 0
rs 4.7996
cc 18
eloc 58
nc 8
nop 0
crap 342

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
2
3
namespace Anax\Navigation;
4
5
/**
6
 * Helper to create a navbar for sites by reading its configuration from file
7
 * and then applying some code while rendering the resultning navbar.
8
 *
9
 */
10
class CNavbar
11
{
12
    use \Anax\TConfigure,
13
        \Anax\DI\TInjectionAware;
14
15
16
17
    /**
18
     * Create a navigation bar / menu, with submenu.
19
     *
20
     * @return string with html for the menu.
21
     *
22
     * @link http://dbwebb.se/coachen/skapa-en-dynamisk-navbar-meny-med-undermeny-via-php
23
     */
24
    public function create()
25
    {
26
        // Keep default options in an array and merge with incoming options that can override the defaults.
27
        $default = array(
28
            'id'          => null,
29
            'class'       => null,
30
            'wrapper'     => 'nav',
31
            'create_url'  => function ($url) {
32
                return $url;
33
            },
34
        );
35
        $menu = array_replace_recursive($default, $this->config);
36
37
        // Create the ul li menu from the array, use an anonomous recursive
38
        // function that returns an array of values.
39
        $createMenu = function (
40
            $items,
41
            $callback,
42
            $ulId = null,
43
            $ulClass = null
44
        ) use (&$createMenu, $menu) {
45
46
            $html = null;
47
            $hasItemIsSelected = false;
48
49
            foreach ($items as $item) {
50
                // has submenu, call recursivly and keep track on if the submenu has a selected item in it.
51
                $submenu        = null;
52
                $selectedParent = null;
53
54
                if (isset($item['submenu'])) {
55
                    list($submenu, $selectedParent) = $createMenu($item['submenu']['items'], $callback);
56
                    $selectedParent = $selectedParent
57
                        ? "selected-parent "
58
                        : null;
59
                }
60
61
                // Check if the current menuitem is selected
62
                $selected = $callback($item['url'])
63
                    ? "selected "
64
                    : null;
65
66
                // Check if the menuitem is a parent of current page, /controller for /controller/action
67
                $isParent = null;
68
                if (isset($item['mark-if-parent-of']) && $item['mark-if-parent-of'] == true) {
69
                    $isParent = $menu['is_parent']($item['mark-if-parent-of'])
70
                        ? "is-parent "
71
                        : null;
72
                }
73
74
                // Is there a class set for this item, then use it
75
                $class = isset($item['class']) && ! is_null($item['class'])
76
                    ? $item['class']
77
                    : null;
78
79
                // Prepare the class-attribute, if used
80
                $class = ($selected || $selectedParent || $isParent || $class)
0 ignored issues
show
Bug Best Practice introduced by
The expression $selected of type string|null is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
Bug Best Practice introduced by
The expression $selectedParent of type string|null is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
Bug Best Practice introduced by
The expression $isParent of type string|null is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
81
                    ? " class='{$selected}{$selectedParent}{$isParent}{$class}' "
82
                    : null;
83
84
                // Add the menu item
85
                $url = $menu['create_url']($item['url']);
86
                $html .= "\n<li{$class}><a href='{$url}' title='{$item['title']}'>{$item['text']}</a>{$submenu}</li>\n";
87
88
                // To remember there is selected children when going up the menu hierarchy
89
                if ($selected) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $selected of type string|null is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
90
                    $hasItemIsSelected = true;
91
                }
92
            }
93
94
            // Return the menu
95
            return array("\n<ul$ulId$ulClass>$html</ul>\n", $hasItemIsSelected);
96
        };
97
98
        // Call the anonomous function to create the menu, and submenues if any.
99
        $id = isset($menu['id'])
100
            ? " id=\"{$menu['id']}\""
101
            : null;
102
        $class = isset($menu['class'])
103
            ? " class=\"{$menu['class']}\""
104
            : null;
105
106
        list($html) = $createMenu(
107
            $menu['items'],
108
            $menu['callback'],
109
            $id,
110
            $class
111
        );
112
113
        // Set the id & class element, only if it exists in the menu-array
114
        $wrapper = $menu['wrapper'];
115
        if ($wrapper) {
116
            $html = "<{$wrapper}>{$html}</{$wrapper}>";
117
        }
118
119
        return "\n{$html}\n";
120
    }
121
}
122