Completed
Push — master ( af41fc...386a3a )
by Mihail
02:49
created

Listing::display()   C

Complexity

Conditions 15
Paths 65

Size

Total Lines 58
Code Lines 28

Duplication

Lines 0
Ratio 0 %

Importance

Changes 20
Bugs 13 Features 0
Metric Value
c 20
b 13
f 0
dl 0
loc 58
rs 6.4662
cc 15
eloc 28
nc 65
nop 1

How to fix   Long Method    Complexity   

Long Method

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:

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
        $orderActiveLink = false;
30
        if ($elements['activeOrder'] !== null) {
31
            $orderActiveLink = $elements['activeOrder'];
32
        }
33
34
        $items = null;
35
        // foreach elements and build schema
36
        foreach ($elements['items'] as $item) {
37
            // type is undefined, skip
38
            if (!Arr::in($item['type'], ['text', 'link'])) {
39
                continue;
40
            }
41
            // sounds like a link, try detect active element
42
            if ($item['type'] === 'link') {
43
                if (!Obj::isArray($item['link']) && !Str::startsWith('http', $item['link']) && !Str::startsWith('#',
44
                        $item['link'])
45
                ) {
46
                    $item['link'] = [$item['link']]; // just controller/action sended, to array
47
                }
48
49
                // check if current element is link and is active
50
                if (Obj::isArray($item['link'])) {
51
                    if (!isset($item['activeClass'])) {
52
                        $item['activeClass'] = 'active';
53
                    }
54
55
                    $activeItem = self::isCurrentLink($item['link'], $item['activeOn'], $orderActiveLink);
56
57
                    // check if it active link for current pathway
58
                    if ($activeItem) {
59
                        $item['property']['class'] = Str::length($item['property']['class']) > 0
60
                            ? $item['activeClass'] . ' ' . $item['property']['class']
61
                            : $item['activeClass'];
62
                    }
63
                }
64
            }
65
66
            $innerItem = self::applyEscape($item['text'], $item['html'], $item['!secure']);
67
            // sounds like a hyperlink?
68
            if ($item['type'] === 'link') {
69
                // parse uri-link type to classic link
70
                $item['linkProperty']['href'] = self::convertLink($item['link']);
71
                // make classic tag inside with text value
72
                $innerItem = self::buildContainerTag('a', $item['linkProperty'], $innerItem, $item['html']);
0 ignored issues
show
Bug introduced by
It seems like $innerItem can also be of type array<integer,string|false|null>; however, Ffcms\Core\Helper\HTML\N...or::buildContainerTag() does only seem to accept null|string, maybe add an additional type check?

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:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
73
            }
74
            // store item
75
            $items .= self::buildContainerTag('li', $item['property'], $innerItem, true);
0 ignored issues
show
Bug introduced by
It seems like $innerItem defined by self::applyEscape($item[...ml'], $item['!secure']) on line 66 can also be of type array<integer,string|false|null>; however, Ffcms\Core\Helper\HTML\N...or::buildContainerTag() does only seem to accept null|string, maybe add an additional type check?

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:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
76
        }
77
78
        // return <ul/> or <ol/> full container
79
        return self::buildContainerTag($elements['type'], $elements['property'], $items, true);
80
    }
81
}