Completed
Push — master ( bbb282...43d0b8 )
by Daniel
25s
created

CMSMenuItem::getAttributesHTML()   C

Complexity

Conditions 12
Paths 36

Size

Total Lines 27
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
cc 12
eloc 14
c 1
b 1
f 0
nc 36
nop 1
dl 0
loc 27
rs 5.1612

How to fix   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 SilverStripe\Admin;
4
5
use SilverStripe\ORM\FieldType\DBField;
6
use Object;
7
use Convert;
8
use SilverStripe\ORM\FieldType\DBHTMLText;
9
10
11
/**
12
 * A simple CMS menu item.
13
 *
14
 * Items can be added to the menu through custom {@link LeftAndMainExtension}
15
 * classes and {@link CMSMenu}.
16
 *
17
 * @see CMSMenu
18
 *
19
 * @package framework
20
 * @subpackage admin
21
 */
22
class CMSMenuItem extends Object {
23
24
	/**
25
	 * The (translated) menu title
26
	 * @var string $title
27
	 */
28
	public $title;
29
30
	/**
31
	 * Relative URL
32
	 * @var string $url
33
	 */
34
	public $url;
35
36
	/**
37
	 * Parent controller class name
38
	 * @var string $controller
39
	 */
40
	public $controller;
41
42
	/**
43
	 * Menu priority (sort order)
44
	 * @var integer $priority
45
	 */
46
	public $priority;
47
48
	/**
49
	 * Attributes for the link. For instance, custom data attributes or standard
50
	 * HTML anchor properties.
51
	 *
52
	 * @var string
53
	 */
54
	protected $attributes = array();
55
56
	/**
57
	 * Create a new CMS Menu Item
58
	 *
59
	 * @param string $title
60
	 * @param string $url
61
	 * @param string $controller Controller class name
62
	 * @param integer $priority The sort priority of the item
63
	 */
64
	public function __construct($title, $url, $controller = null, $priority = -1) {
65
		$this->title = $title;
66
		$this->url = $url;
67
		$this->controller = $controller;
68
		$this->priority = $priority;
69
70
		parent::__construct();
71
	}
72
73
	/**
74
	 * @param array $attributes
75
	 */
76
	public function setAttributes($attributes) {
77
		$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...
78
	}
79
80
	/**
81
	 * @param array $attrs
82
	 * @return DBHTMLText
83
	 */
84
	public function getAttributesHTML($attrs = null) {
85
		$excludeKeys = (is_string($attrs)) ? func_get_args() : null;
86
87
		if(!$attrs || is_string($attrs)) {
88
			$attrs = $this->attributes;
89
		}
90
91
		// Remove empty or excluded values
92
		foreach ($attrs as $key => $value) {
0 ignored issues
show
Bug introduced by
The expression $attrs of type string|array is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
93
			if (
94
				($excludeKeys && in_array($key, $excludeKeys))
95
				|| (!$value && $value !== 0 && $value !== '0')
96
			) {
97
				unset($attrs[$key]);
98
				continue;
99
			}
100
		}
101
102
		// Create markkup
103
		$parts = array();
104
105
		foreach($attrs as $name => $value) {
0 ignored issues
show
Bug introduced by
The expression $attrs of type string|array is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
106
			$parts[] = ($value === true) ? "{$name}=\"{$name}\"" : "{$name}=\"" . Convert::raw2att($value) . "\"";
107
		}
108
109
		return DBField::create_field('HTMLFragment', implode(' ', $parts));
110
	}
111
}
112