Completed
Push — master ( 739baa...547298 )
by Michael
02:43 queued 01:13
created

MymenusBuilder::buildSelected()   F

Complexity

Conditions 11
Paths 300

Size

Total Lines 47
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 11
eloc 23
nc 300
nop 0
dl 0
loc 47
rs 3.8181
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
2
/*
3
 You may not change or alter any portion of this comment or credits
4
 of supporting developers from this source code or any supporting source code
5
 which is considered copyrighted (c) material of the original comment or credit authors.
6
7
 This program is distributed in the hope that it will be useful,
8
 but WITHOUT ANY WARRANTY; without even the implied warranty of
9
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10
 */
11
12
/**
13
 * @copyright       XOOPS Project (https://xoops.org)
14
 * @license         http://www.gnu.org/licenses/gpl-2.0.html GNU Public License
15
 * @package         Mymenus
16
 * @since           1.0
17
 * @author          trabis <[email protected]>
18
 */
19
20
use Xmf\Request;
21
22
/**
23
 * Class MymenusBuilder
24
 */
25
class MymenusBuilder
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

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