Completed
Push — master ( 203eb7...1d2391 )
by Mihail
07:50
created

Listing   B

Complexity

Total Complexity 39

Size/Duplication

Total Lines 134
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 8

Importance

Changes 17
Bugs 12 Features 0
Metric Value
wmc 39
c 17
b 12
f 0
lcom 1
cbo 8
dl 0
loc 134
rs 8.2857

1 Method

Rating   Name   Duplication   Size   Complexity  
F display() 0 125 39
1
<?php
2
3
namespace Ffcms\Core\Helper\HTML;
4
5
use Ffcms\Core\App;
6
use Ffcms\Core\Helper\Type\Arr;
7
use Ffcms\Core\Helper\Type\Obj;
8
use Ffcms\Core\Helper\Type\Str;
9
use Ffcms\Core\Helper\Url;
10
11
/**
12
 * Class HList
13
 * @package Ffcms\Core\Helper\HTML
14
 */
15
class Listing extends NativeGenerator
16
{
17
18
    /**
19
     * Construct listing elements,property's and future's
20
     * @param array $elements
21
     * @return string
22
     */
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;
0 ignored issues
show
Unused Code introduced by
$activeItem is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

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.

Loading history...
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
}