CMSMenuItem   A
last analyzed

Complexity

Total Complexity 15

Size/Duplication

Total Lines 113
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 32
c 1
b 0
f 0
dl 0
loc 113
rs 10
wmc 15

4 Methods

Rating   Name   Duplication   Size   Complexity  
A setAttributes() 0 3 1
A __construct() 0 7 1
C getAttributesHTML() 0 32 12
A addExtraClass() 0 5 1
1
<?php
2
3
namespace LeKoala\Admini;
4
5
use SilverStripe\Core\Convert;
6
use SilverStripe\Core\Injector\Injectable;
7
use SilverStripe\ORM\FieldType\DBField;
8
use SilverStripe\ORM\FieldType\DBHTMLText;
9
10
/**
11
 * A simple CMS menu item.
12
 *
13
 * Items can be added to the menu through custom {@link LeftAndMainExtension}
14
 * classes and {@link CMSMenu}.
15
 *
16
 * @see CMSMenu
17
 */
18
class CMSMenuItem
19
{
20
    use Injectable;
21
22
    /**
23
     * The (translated) menu title
24
     * @var string $title
25
     */
26
    public $title;
27
28
    /**
29
     * Relative URL
30
     * @var string $url
31
     */
32
    public $url;
33
34
    /**
35
     * Parent controller class name
36
     * @var string $controller
37
     */
38
    public $controller;
39
40
    /**
41
     * Menu priority (sort order)
42
     * @var integer $priority
43
     */
44
    public $priority;
45
46
    /**
47
     * Attributes for the link. For instance, custom data attributes or standard
48
     * HTML anchor properties.
49
     *
50
     * @var string
51
     */
52
    protected $attributes = [];
53
54
    /**
55
     * @var string
56
     */
57
    public $iconName;
58
59
    /**
60
     * Create a new CMS Menu Item
61
     *
62
     * @param string $title
63
     * @param string $url
64
     * @param string $controller Controller class name
65
     * @param integer $priority The sort priority of the item
66
     * @param string $iconName
67
     */
68
    public function __construct($title, $url, $controller = null, $priority = -1, $iconName = null)
69
    {
70
        $this->title = $title;
71
        $this->url = $url;
72
        $this->controller = $controller;
73
        $this->priority = $priority;
74
        $this->iconName = $iconName;
75
    }
76
77
    /**
78
     * @param array $attributes
79
     */
80
    public function setAttributes($attributes)
81
    {
82
        $this->attributes = $attributes;
0 ignored issues
show
Documentation Bug introduced by
It seems like $attributes of type array is incompatible with the declared type string of property $attributes.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
83
    }
84
85
    /**
86
     * @param string $extraClass
87
     */
88
    public function addExtraClass($extraClass)
89
    {
90
        $class = $this->attributes["class"] ?? "";
91
        $class .= " " . $extraClass;
92
        $this->attributes["class"] = $class;
93
    }
94
95
    /**
96
     * @param array $attrs
97
     * @return DBHTMLText
98
     */
99
    public function getAttributesHTML($attrs = null)
100
    {
101
        $excludeKeys = (is_string($attrs)) ? func_get_args() : null;
102
103
        if (!$attrs || is_string($attrs)) {
104
            $attrs = $this->attributes;
105
        }
106
107
        // Remove empty or excluded values
108
        foreach ($attrs as $key => $value) {
109
            if (($excludeKeys && in_array($key, $excludeKeys))
110
                || (!$value && $value !== 0 && $value !== '0')
111
            ) {
112
                unset($attrs[$key]);
113
                continue;
114
            }
115
        }
116
117
        // Create markkup
118
        $parts = array();
119
120
        foreach ($attrs as $name => $value) {
121
            if ($value === true) {
122
                $value = $name;
123
            }
124
125
            $parts[] = sprintf('%s="%s"', Convert::raw2att($name), Convert::raw2att($value));
0 ignored issues
show
Bug introduced by
It seems like SilverStripe\Core\Convert::raw2att($name) can also be of type array and array; however, parameter $values of sprintf() does only seem to accept double|integer|string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

125
            $parts[] = sprintf('%s="%s"', /** @scrutinizer ignore-type */ Convert::raw2att($name), Convert::raw2att($value));
Loading history...
126
        }
127
128
        /** @var DBHTMLText $fragment */
129
        $fragment = DBField::create_field('HTMLFragment', implode(' ', $parts));
130
        return $fragment;
131
    }
132
}
133