Passed
Branch ops-updates (277b44)
by Björn
05:09
created

Toolbar   A

Complexity

Total Complexity 39

Size/Duplication

Total Lines 212
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 83
c 0
b 0
f 0
dl 0
loc 212
rs 9.28
wmc 39

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __invoke() 0 7 2
F renderNormalMenu() 0 137 34
A setTagname() 0 5 2
A getTagname() 0 2 1
1
<?php
2
/**
3
 * BB's Zend Framework 2 Components
4
 * 
5
 * UI Components
6
 *
7
 * @package     [MyApplication]
8
 * @subpackage  BB's Zend Framework 2 Components
9
 * @subpackage  UI Components
10
 * @author      Björn Bartels <[email protected]>
11
 * @link        https://gitlab.bjoernbartels.earth/groups/zf2
12
 * @license     http://www.apache.org/licenses/LICENSE-2.0 Apache License, Version 2.0
13
 * @copyright   copyright (c) 2016 Björn Bartels <[email protected]>
14
 */
15
16
namespace UIComponents\View\Helper\Components;
17
18
use \RecursiveIteratorIterator;
0 ignored issues
show
Bug introduced by
The type \RecursiveIteratorIterator 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...
19
use Zend\Navigation\AbstractContainer;
20
21
/**
22
 *
23
 * Helper for rendering 'Bootstrap' 
24
 * 
25
 * @see \UIComponents\View\Helper\Navigation\Menu
26
 */
27
class Toolbar extends \UIComponents\View\Helper\Navigation\Menu
28
{
29
    
30
    /**
31
     * component's tag-name
32
     *
33
     * @var string
34
     */
35
    protected $tagname = 'div';
36
    
37
    /**
38
     * component's class-names
39
     *
40
     * @var string
41
     */
42
    protected $classnames = 'toolbar toolbar-default callout small';
43
    
44
    /**
45
     * component's size attributes
46
     *
47
     * @var string|array
48
     */
49
    protected $size = '';
50
    
51
    /**
52
     * View helper entry point:
53
     * Retrieves helper and optionally sets container to operate on
54
     *
55
     * @param  string|AbstractContainer $container [optional] container to operate on
56
     * @return self
57
     */
58
    public function __invoke($container = 'componentnavigationhelper')
59
    {
60
        if (null !== $container) {
0 ignored issues
show
introduced by
The condition null !== $container is always true.
Loading history...
61
            $this->setContainer($container);
62
        }
63
64
        return (clone $this);
65
    }
66
    
67
    /**
68
     * Renders a normal menu (called from {@link renderMenu()})
69
     *
70
     * @param    AbstractContainer $container            container to render
71
     * @param    string            $ulClass            CSS class for first UL
72
     * @param    string            $indent             initial indentation
73
     * @param    int|null            $minDepth            minimum depth
74
     * @param    int|null            $maxDepth            maximum depth
75
     * @param    bool                $onlyActive         render only active branch?
76
     * @param    bool                $escapeLabels        Whether or not to escape the labels
77
     * @param    bool                $addClassToListItem Whether or not page class applied to <li> element
78
     * @param    string            $liActiveClass        CSS class for active LI
79
     * @return string
80
     */
81
    protected function renderNormalMenu(
82
        \Zend\Navigation\AbstractContainer $container,
83
        $ulClass,
84
        $indent,
85
        $minDepth,
86
        $maxDepth,
87
        $onlyActive,
88
        $escapeLabels,
89
        $addClassToListItem,
90
        $liActiveClass
91
    ) {
92
        $html = '';
93
94
        // find deepest active
95
        $found = $this->findActive($container, $minDepth, $maxDepth);
96
        /* @var $escaper \Zend\View\Helper\EscapeHtmlAttr */
97
        $escaper = $this->view->plugin('escapeHtmlAttr');
0 ignored issues
show
Bug introduced by
The method plugin() does not exist on Zend\View\Renderer\RendererInterface. It seems like you code against a sub-type of Zend\View\Renderer\RendererInterface such as Zend\View\Renderer\PhpRenderer. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

97
        /** @scrutinizer ignore-call */ 
98
        $escaper = $this->view->plugin('escapeHtmlAttr');
Loading history...
98
99
        if ($found) {
100
            $foundPage    = $found['page'];
101
            $foundDepth = $found['depth'];
102
        } else {
103
            $foundPage = null;
104
        }
105
106
        // create iterator
107
        $iterator = new RecursiveIteratorIterator(
108
            $container,
109
            RecursiveIteratorIterator::SELF_FIRST
110
        );
111
        if (is_int($maxDepth)) {
112
            $iterator->setMaxDepth($maxDepth);
113
        }
114
115
        // iterate container
116
        $prevDepth = -1;
117
        foreach ($iterator as $page) {
118
            $depth = $iterator->getDepth();
119
            $page->set('level', $depth);
120
            $isActive = $page->isActive(true);
121
            if ($depth < $minDepth || !$this->accept($page)) {
122
                // page is below minDepth or not accepted by acl/visibility
123
                continue;
124
            } elseif ($onlyActive && !$isActive) {
125
                // page is not active itself, but might be in the active branch
126
                $accept = false;
127
                if ($foundPage) {
128
                    if ($foundPage->hasPage($page)) {
129
                        // accept if page is a direct child of the active page
130
                        $accept = true;
131
                    } elseif ($foundPage->getParent()->hasPage($page)) {
132
                        // page is a sibling of the active page...
133
                        if (!$foundPage->hasPages(!$this->renderInvisible) ||
134
                            is_int($maxDepth) && $foundDepth + 1 > $maxDepth) {
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $foundDepth does not seem to be defined for all execution paths leading up to this point.
Loading history...
135
                            // accept if active page has no children, or the
136
                            // children are too deep to be rendered
137
                            $accept = true;
138
                        }
139
                    }
140
                }
141
142
                if (!$accept) {
143
                    continue;
144
                }
145
            }
146
147
            // make sure indentation is correct
148
            $depth -= $minDepth;
149
            $myIndent = $indent . str_repeat('    ', $depth);
150
151
            if ($depth > $prevDepth) {
152
                // start new ul tag
153
                $ulClass = '' . 
154
                    ($depth == 0 ? $this->getUlClass() : 
155
                            ($depth == 1 ? $this->getSubUlClassLevel1() : $this->getSubUlClass())
156
                    ) . 
157
                    ' level_' . $depth . 
158
                '';
159
                if ($ulClass && $depth ==    0) {
160
                    $ulClass = ' class="' . $escaper($ulClass) . '"';
161
                } else {
162
                    $ulClass = ' class="' . $escaper($ulClass) . '"';
163
                }
164
                $html .= $myIndent . '<' . $this->getTagname() . $ulClass . '>' . PHP_EOL;
165
            } elseif ($prevDepth > $depth) {
166
                // close li/ul tags until we're at current depth
167
                for ($i = $prevDepth; $i > $depth; $i--) {
168
                    $ind = $indent . str_repeat('        ', $i);
169
                    //$html .= $ind . '    </li>' . PHP_EOL;
170
                    $html .= $ind . '</' . $this->getTagname() . '>' . PHP_EOL;
171
                }
172
                // close previous li tag
173
                //$html .= $myIndent . '    </li>' . PHP_EOL;
174
            } else {
175
                // close previous li tag
176
                //$html .= $myIndent . '    </li>' . PHP_EOL;
177
            }
178
179
            // render li tag and page
180
            $liClasses = [];
181
            // Is page active?
182
            if ($isActive) {
183
                $liClasses[] = $liActiveClass;
184
            }
185
            if (!empty($this->getDefaultLiClass())) {
186
                $liClasses[] = $this->getDefaultLiClass();
187
            }
188
            $isBelowMaxLevel = ($maxDepth > $depth) || ($maxDepth === null) || ($maxDepth === false);
189
            if (!empty($page->pages) && $isBelowMaxLevel) {
190
                $liClasses[] = ($depth == 0 ? $this->getSubLiClassLevel0() : $this->getSubLiClass());
191
            }
192
            // Add CSS class from page to <li>
193
            if ($addClassToListItem && $page->getClass()) {
194
                $liClasses[] = $page->getClass();
195
            }
196
            $liClass = empty($liClasses) ? '' : ' class="' . $escaper(implode(' ', $liClasses)) . '"';
0 ignored issues
show
Unused Code introduced by
The assignment to $liClass is dead and can be removed.
Loading history...
197
198
            $html .= /* $myIndent . '    <li' . $liClass . '>' . PHP_EOL
199
                . */ $myIndent . '        ' . $this->htmlify($page, $escapeLabels, $addClassToListItem, [
200
                    'data-test' => 'cta-toolbar-' . $this->slugify($page->getLabel()),
201
                ]) . PHP_EOL;
202
203
            // store as previous depth for next iteration
204
            $prevDepth = $depth;
205
        }
206
207
        if ($html) {
208
            // done iterating container; close open ul/li tags
209
            for ($i = $prevDepth+1; $i > 0; $i--) {
210
                $myIndent = $indent . str_repeat('        ', $i-1);
211
                $html .= /*$myIndent . '    </li>' . PHP_EOL
212
                    . */ $myIndent . '</' . $this->getTagname() . '>' . PHP_EOL;
213
            }
214
            $html = rtrim($html, PHP_EOL);
215
        }
216
217
        return $html;
218
    }
219
    
220
    //
221
    // component related getters/setters
222
    //
223
    
224
    /**
225
     * @return the $tagname
0 ignored issues
show
Bug introduced by
The type UIComponents\View\Helper\Components\the 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...
226
     */
227
    public function getTagname() {
228
        return $this->tagname;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->tagname returns the type string which is incompatible with the documented return type UIComponents\View\Helper\Components\the.
Loading history...
229
    }
230
231
    /**
232
     * @param string $tagname
233
     */
234
    public function setTagname($tagname) {
235
        if ( null !== $tagname ) {
0 ignored issues
show
introduced by
The condition null !== $tagname is always true.
Loading history...
236
            $this->tagname = $tagname;
237
        }
238
        return $this;
239
    }
240
241
}