Passed
Push — master ( 5c9a79...879726 )
by Michael
02:51 queued 01:58
created

Builder::buildSelected()   C

Complexity

Conditions 11
Paths 300

Size

Total Lines 45
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 11
eloc 22
nc 300
nop 0
dl 0
loc 45
rs 5.2333
c 0
b 0
f 0

How to fix   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 namespace XoopsModules\Mymenus;
2
3
/*
4
 You may not change or alter any portion of this comment or credits
5
 of supporting developers from this source code or any supporting source code
6
 which is considered copyrighted (c) material of the original comment or credit authors.
7
8
 This program is distributed in the hope that it will be useful,
9
 but WITHOUT ANY WARRANTY; without even the implied warranty of
10
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
11
 */
12
13
/**
14
 * @copyright       XOOPS Project (https://xoops.org)
15
 * @license         http://www.gnu.org/licenses/gpl-2.0.html GNU Public License
16
 * @package         Mymenus
17
 * @since           1.0
18
 * @author          trabis <[email protected]>
19
 */
20
21
use Xmf\Request;
0 ignored issues
show
Bug introduced by
The type Xmf\Request was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
22
23
/**
24
 * Class Builder
25
 */
26
class Builder
27
{
28
    public $parents = [];
29
    public $output  = [];
30
31
    /**
32
     * @param $array
33
     */
34
    public function __construct($array)
35
    {
36
        $this->addMenu($array);
37
    }
38
39
    /**
40
     * @param $array
41
     */
42
    public function addMenu($array)
43
    {
44
        foreach ($array as $item) {
45
            $this->add($item);
46
        }
47
    }
48
49
    /**
50
     * @param $item
51
     */
52
    public function add($item)
53
    {
54
        $this->parents[$item['pid']][] = $item;
55
    }
56
57
    /**
58
     * @param int $pid
59
     */
60
    public function buildMenus($pid = 0)
61
    {
62
        static $idx = -1;
63
        static $level = -1;
64
        ++$level;
65
        $first = true;
66
67
        foreach ($this->parents[$pid] as $item) {
68
            ++$idx;
69
70
            $this->output[$idx]['oul']    = false;
71
            $this->output[$idx]['oli']    = false;
72
            $this->output[$idx]['close']  = '';
73
            $this->output[$idx]['cul']    = false;
74
            $this->output[$idx]['cli']    = false;
75
            $this->output[$idx]['hassub'] = false;
76
            $this->output[$idx]['level']  = $level;
77
78
            if ($first) {
79
                $this->output[$idx]['oul'] = true;
80
                $first                     = false;
81
            }
82
83
            $this->output[$idx]['oli'] = true;
84
            $this->output[$idx]        = array_merge($item, $this->output[$idx]);
85
86
            if (isset($this->parents[$item['id']])) {
87
                $this->output[$idx]['hassub'] = true;
88
                $this->buildMenus($item['id']);
89
            }
90
            $this->output[$idx]['cli']   = true;
91
            $this->output[$idx]['close'] .= "</li>\n";
92
        }
93
        $this->output[$idx]['cul']   = true;
94
        $this->output[$idx]['close'] .= "</ul>\n";
95
        --$level;
96
    }
97
98
    /**
99
     * @param int $pid
100
     */
101
    public function buildUpDown($pid = 0)
102
    {
103
        static $idx = -1;
104
        $prevWeight = null;
105
        $up         = 0;
106
        $down       = 1;
107
        $counter    = 0;
108
        $count      = count($this->parents[$pid]);
109
110
        foreach ($this->parents[$pid] as $item) {
111
            ++$idx;
112
            $counter++;
113
            if ($counter == $count) {
114
                $down = 0;
115
            } // turn off down link for last entry
116
117
            if ($up) {
118
                $this->output[$idx]['up_weight'] = $prevWeight;
119
            }
120
            if ($down) {
121
                $this->output[$idx]['down_weight'] = $this->output[$idx]['weight'] + 2;
122
            }
123
124
            $prevWeight = $this->output[$idx]['weight'];
125
            $up         = 1; // turn on up link for all entries after first one
126
127
            if (isset($this->parents[$item['id']])) {
128
                $this->buildUpDown($item['id']);
129
            }
130
        }
131
    }
132
133
    public function buildSelected()
134
    {
135
        //get the currentpage
136
        $sel = [];
137
        //        $queryString = $_SERVER['QUERY_STRING'] ? '?' . $_SERVER['QUERY_STRING'] : '';
138
        $queryString = Request::getString('QUERY_STRING', '', 'SERVER') ? '?' . Request::getString('QUERY_STRING', '', 'SERVER') : '';
139
        //        $self         = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'] . $queryString;
140
        $self = 'http://' . Request::getString('HTTP_HOST', '', 'SERVER') . Request::getString('PHP_SELF', '', 'SERVER') . $queryString;
141
142
        //set a default page in case we don't get matches
143
        $default = XOOPS_URL . '/index.php';
0 ignored issues
show
Bug introduced by
The constant XoopsModules\Mymenus\XOOPS_URL was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
144
145
        //get all matching links
146
        foreach ($this->output as $idx => $menu) {
147
            $selected = 0;
148
            if ($menu['link']) {
149
                $selected = (false !== stripos($self, $menu['link'])) ? 1 : $selected;
150
            }
151
            $selected = ($menu['link'] == $self) ? 1 : $selected;
152
            $selected = ($menu['link'] == $default) ? 1 : $selected;
153
            if ($selected) {
154
                $sel[$idx] = $menu;
155
            }
156
        }
157
158
        //From those links get only the longer one
159
        $longlink = '';
160
        $longidx  = 0;
161
        foreach ($sel as $idx => $menu) {
162
            if (strlen($menu['link']) > strlen($longlink)) {
163
                $longidx  = $idx;
164
                $longlink = $menu['link'];
165
            }
166
        }
167
168
        /*
169
         * When visiting site.com when XOOPS_URL is set to www.site.com
170
         * longidx is not detected, this IF will prevent blank page
171
         */
172
        if (isset($this->output[$longidx])) {
173
            $this->output[$longidx]['selected']    = true;
174
            $this->output[$longidx]['topselected'] = true;
175
176
            //Now turn all this menu parents to selected
177
            $this->addSelectedParents($this->output[$longidx]['pid']);
178
        }
179
    }
180
181
    /**
182
     * @param $pid
183
     */
184
    public function addSelectedParents($pid)
185
    {
186
        foreach ($this->output as $idx => $menu) {
187
            if ($menu['id'] == $pid) {
188
                $this->output[$idx]['selected'] = true;
189
                $this->addSelectedParents($menu['pid']);
190
            }
191
        }
192
    }
193
194
    /**
195
     * @return array
196
     */
197
    public function render()
198
    {
199
        $this->buildMenus();
200
        $this->buildUpDown();
201
        $this->buildSelected();
202
203
        return $this->output;
204
    }
205
}
206