Conditions | 13 |
Paths | 64 |
Total Lines | 39 |
Code Lines | 29 |
Lines | 0 |
Ratio | 0 % |
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:
1 | <?php |
||
88 | public static function showLi($object, $deep = 1, $maxDeep = 0, $hrefFunc = null, $order = [], $activeFunc = '', $activeClass = 'active') { |
||
89 | $count = 0; |
||
90 | $isset = false; |
||
91 | $class = get_class($object); |
||
92 | $item = $hrefFunc ? $hrefFunc($object) : "<a href='#'> {$object->name()}</a> "; |
||
93 | $attributes = []; |
||
94 | if ($activeFunc && $activeFunc($object)) { |
||
95 | $attributes['class'] = $activeClass; |
||
96 | } |
||
97 | |||
98 | if (is_array($item)) { |
||
99 | $attributes = $item['attributes']; |
||
100 | $item = $item['text']; |
||
101 | } |
||
102 | if (!isset($attributes['id'])) { |
||
103 | $attributes['id'] = str_replace('\\', '_', get_class($object)) . "-{$object->pk()}"; |
||
104 | } |
||
105 | if (!$maxDeep || $deep < $maxDeep) { |
||
106 | $items = $class::getList(['where' => ['parent_id', $object->pk()], 'order' => $order]); |
||
107 | $count += count($items); |
||
108 | foreach ($items as $objectChild) { |
||
109 | if (!$isset) { |
||
110 | $isset = true; |
||
111 | if ($activeFunc && $activeFunc($objectChild)) { |
||
112 | $attributes['class'] = $activeClass; |
||
113 | } |
||
114 | echo \Html::el('li', $attributes, $item, true); |
||
115 | echo '<ul>'; |
||
116 | } |
||
117 | $count += static::showLi($objectChild, $deep + 1, $maxDeep, $hrefFunc, $order, $activeFunc, $activeClass); |
||
118 | } |
||
119 | } |
||
120 | if ($isset) { |
||
121 | echo '</ul></li>'; |
||
122 | } else { |
||
123 | echo \Html::el('li', $attributes, $item); |
||
124 | } |
||
125 | return $count; |
||
126 | } |
||
127 | } |
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.