Conditions | 39 |
Paths | > 20000 |
Total Lines | 125 |
Code Lines | 81 |
Lines | 0 |
Ratio | 0 % |
Changes | 17 | ||
Bugs | 12 | Features | 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 |
||
23 | public static function display($elements) |
||
24 | { |
||
25 | if (!Arr::in($elements['type'], ['ul', 'ol']) || count($elements['items']) < 1) { |
||
26 | return null; |
||
27 | } |
||
28 | |||
29 | $ulProperties = self::applyProperty($elements['property']); |
||
30 | $orderActiveLink = false; |
||
31 | if ($elements['activeOrder'] !== null) { |
||
32 | $orderActiveLink = $elements['activeOrder']; |
||
33 | } |
||
34 | |||
35 | $items = null; |
||
36 | // foreach elements and build schema |
||
37 | foreach ($elements['items'] as $item) { |
||
38 | // type is undefined, skip |
||
39 | if (!Arr::in($item['type'], ['text', 'link'])) { |
||
40 | continue; |
||
41 | } |
||
42 | // sounds like a link, try detect active element |
||
43 | if ($item['type'] === 'link') { |
||
44 | if (!Obj::isArray($item['link']) && !Str::startsWith('http', $item['link']) && !Str::startsWith('#', $item['link'])) { |
||
45 | $item['link'] = [$item['link']]; // just controller/action sended, to array |
||
46 | } |
||
47 | |||
48 | if (Obj::isArray($item['link'])) { |
||
49 | $elementPoint = Url::buildPathway($item['link']); |
||
50 | $currentPoint = Url::buildPathwayFromRequest(); |
||
51 | |||
52 | if (!isset($item['activeClass'])) { |
||
53 | $item['activeClass'] = 'active'; |
||
54 | } |
||
55 | |||
56 | $activeItem = false; |
||
|
|||
57 | // use special active element order type: controller, action |
||
58 | switch ($orderActiveLink) { |
||
59 | case 'controller': |
||
60 | $elementPoint = Str::firstIn($elementPoint, '/'); |
||
61 | $activeItem = Str::startsWith($elementPoint, $currentPoint); |
||
62 | break; |
||
63 | case 'action': |
||
64 | $elementArray = explode('/', $elementPoint); |
||
65 | if (!Str::contains('/', $elementPoint) || count($elementArray) < 2) { |
||
66 | $activeItem = $elementPoint === $currentPoint; |
||
67 | } else { |
||
68 | $elementPoint = $elementArray[0] . '/' . $elementArray[1]; |
||
69 | $activeItem = Str::startsWith($elementPoint, $currentPoint); |
||
70 | } |
||
71 | break; |
||
72 | case 'id': |
||
73 | $elementArray = explode('/', $elementPoint); |
||
74 | $elementPoint = $elementArray[0] . '/' . $elementArray[1]; |
||
75 | if (null !== $elementArray[2]) { |
||
76 | $elementPoint .= '/' . $elementArray[2]; |
||
77 | } |
||
78 | |||
79 | $activeItem = Str::startsWith($elementPoint, $currentPoint); |
||
80 | break; |
||
81 | default: |
||
82 | $activeItem = $elementPoint === $currentPoint; |
||
83 | break; |
||
84 | } |
||
85 | |||
86 | // if defined active on pathways lets try to find equals |
||
87 | if (isset($item['activeOn']) && Obj::isArray($item['activeOn'])) { |
||
88 | foreach ($item['activeOn'] as $activeUri) { |
||
89 | $activeUri = trim($activeUri, '/'); |
||
90 | if (Str::endsWith('*', $activeUri)) { |
||
91 | $activeUri = rtrim($activeUri, '*'); |
||
92 | if (Str::startsWith($activeUri, $currentPoint)) { |
||
93 | $activeItem = true; |
||
94 | } |
||
95 | } else { |
||
96 | if ($activeUri === $currentPoint) { |
||
97 | $activeItem = true; |
||
98 | } |
||
99 | } |
||
100 | } |
||
101 | } |
||
102 | |||
103 | |||
104 | // check if it active link for current pathway |
||
105 | if ($activeItem) { |
||
106 | $item['property']['class'] = Str::length($item['property']['class']) > 0 |
||
107 | ? $item['activeClass'] . ' ' . $item['property']['class'] |
||
108 | : $item['activeClass']; |
||
109 | } |
||
110 | } |
||
111 | } |
||
112 | |||
113 | $items .= '<li'; |
||
114 | if (isset($item['property']) && count($item['property']) > 0) { |
||
115 | foreach ($item['property'] as $attr => $value) { |
||
116 | $items .= ' ' . $attr . '="' . $value . '"'; |
||
117 | } |
||
118 | } |
||
119 | $items .= '>'; |
||
120 | |||
121 | // sounds like a text, build element |
||
122 | if ($item['type'] === 'text') { |
||
123 | $items .= ($item['html'] ? self::safe($item['text']) : self::nohtml($item['text'])); |
||
124 | } elseif ($item['type'] === 'link') { // sounds like link |
||
125 | $link = App::$Alias->baseUrl . '/'; |
||
126 | if (Obj::isArray($item['link'])) { |
||
127 | $link .= Url::buildPathway($item['link']); |
||
128 | } elseif (Str::startsWith('http', $item['link'])) { |
||
129 | $link = self::nohtml($item['link']); |
||
130 | } elseif (Str::startsWith('#', $item['link'])) { // allow pass #part |
||
131 | $link = self::nohtml($item['link']); |
||
132 | } else { |
||
133 | $link .= self::nohtml(trim($item['link'], '/')); |
||
134 | } |
||
135 | |||
136 | $htmlLink = '<a href="' . self::nohtml($link) . '"'; |
||
137 | if (isset($item['linkProperty']) && Obj::isArray($item['linkProperty'])) { |
||
138 | $htmlLink .= self::applyProperty($item['linkProperty']); |
||
139 | } |
||
140 | $htmlLink .= '>' . ((isset($item['html']) && $item['html'] === true ) ? App::$Security->secureHtml($item['text']) : App::$Security->strip_tags($item['text'])) . '</a>'; |
||
141 | $items .= $htmlLink; |
||
142 | } |
||
143 | $items .= '</li>'; |
||
144 | } |
||
145 | |||
146 | return '<' . $elements['type'] . $ulProperties . '>' . $items . '</' . $elements['type'] . '>'; |
||
147 | } |
||
148 | } |
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVar
assignment in line 1 and the$higher
assignment in line 2 are dead. The first because$myVar
is never used and the second because$higher
is always overwritten for every possible time line.