Conditions | 34 |
Paths | > 20000 |
Total Lines | 136 |
Lines | 53 |
Ratio | 38.97 % |
Changes | 0 |
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:
If many parameters/temporary variables are present:
Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.
There are several approaches to avoid long parameter lists:
1 | <?php |
||
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'); |
||
98 | |||
99 | View Code Duplication | 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 | View Code Duplication | 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) { |
||
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 | View Code Duplication | 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 | View Code Duplication | 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 | View Code Duplication | 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)) . '"'; |
||
197 | |||
198 | $html .= /* $myIndent . ' <li' . $liClass . '>' . PHP_EOL |
||
199 | . */ $myIndent . ' ' . $this->htmlify($page, $escapeLabels, $addClassToListItem) . PHP_EOL; |
||
200 | |||
201 | // store as previous depth for next iteration |
||
202 | $prevDepth = $depth; |
||
203 | } |
||
204 | |||
205 | View Code Duplication | if ($html) { |
|
206 | // done iterating container; close open ul/li tags |
||
207 | for ($i = $prevDepth+1; $i > 0; $i--) { |
||
208 | $myIndent = $indent . str_repeat(' ', $i-1); |
||
209 | $html .= /*$myIndent . ' </li>' . PHP_EOL |
||
210 | . */ $myIndent . '</' . $this->getTagname() . '>' . PHP_EOL; |
||
211 | } |
||
212 | $html = rtrim($html, PHP_EOL); |
||
213 | } |
||
214 | |||
215 | return $html; |
||
216 | } |
||
217 | |||
239 | } |
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: