Completed
Push — master ( ab4944...534e9c )
by Andrew
02:30
created

ClassyMenuItem   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 172
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 2
Bugs 0 Features 2
Metric Value
c 2
b 0
f 2
dl 0
loc 172
rs 10
wmc 16
lcom 1
cbo 1

10 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 10 2
A get_title() 0 5 1
A get_slug() 0 5 1
A get_link() 0 5 1
A get_children() 0 5 1
A get_classes() 0 5 1
A add_class() 0 5 1
A add_child() 0 19 4
A filter_classes() 0 5 1
A update_child_levels() 0 16 3
1
<?php
2
3
/**
4
 * Class for handling menu item functionality
5
 */
6
7
class ClassyMenuItem extends ClassyBasis {
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
8
9
	/**
10
	 * Children
11
	 * @var array
12
	 */
13
	protected $children = array();
14
15
	/**
16
	 * CSS Classes
17
	 * @var array
18
	 */
19
	protected $classes = array();
20
21
	/**
22
	 * If item has child
23
	 * @var boolean
24
	 */
25
	protected $has_child = false;
26
27
	/**
28
	 * Nesting level
29
	 * @var integer
30
	 */
31
	public $level = 0;
32
	
33
	/**
34
	 * Item title
35
	 * @var string
36
	 */
37
	public $title;
38
39
	/**
40
	 * Checks if provided arg is instance of WP_Post and inits it
41
	 * 
42
	 * @param WP_Post $item
43
	 */
44
	public function __construct($item) {
45
46
		if (is_a($item, 'WP_Post')) {
47
48
			$this->import($item);
49
			$this->filter_classes();
50
			
51
		}
52
53
	}
54
55
	/**
56
	 * Returns item title
57
	 * 
58
	 * @return string
59
	 */
60
	public function get_title() {
61
62
		return $this->title;
63
64
	}
65
66
	/**
67
	 * Returns item slug
68
	 * 
69
	 * @return string
70
	 */
71
	public function get_slug() {
72
73
		return $this->post_name;
0 ignored issues
show
Bug introduced by
The property post_name does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
74
75
	}
76
77
	/**
78
	 * Returns item link (url)
79
	 * 
80
	 * @return string
81
	 */
82
	public function get_link() {
83
84
		return $this->url;
0 ignored issues
show
Bug introduced by
The property url does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
85
86
	}
87
88
	/**
89
	 * Retuns item children, if there are any
90
	 * 
91
	 * @return array
92
	 */
93
	public function get_children() {
94
95
		return $this->children;
96
97
	}
98
99
	/**
100
	 * Returns menu item classes
101
	 * @return string
102
	 */
103
	public function get_classes() {
104
105
		return implode(' ', $this->classes);
106
107
	}
108
109
	/**
110
	 * Adds css class to classes array
111
	 * 
112
	 * @param string $class_name
113
	 */
114
	public function add_class($class_name) {
115
		
116
		$this->classes[] = $class_name;
117
118
	}
119
120
	/**
121
	 * Adds child to current ClassyMenuItem
122
	 * 
123
	 * @param ClassyMenuItem $item 
124
	 */
125
	public function add_child($item) {
126
127
		if ( !$this->has_child ) {
128
			$this->add_class( 'menu-item-has-children' );
129
			$this->has_child = true;
130
		}
131
132
		if ( !isset( $this->children ) ) {
133
			$this->children = array();
134
		}
135
136
		$this->children[] = $item;
137
		$item->level = $this->level + 1;
138
139
		if ($item->children) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $item->children of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
140
			$this->update_child_levels();
141
		}
142
143
	}
144
145
	/**
146
	 * Applies filters for item classes
147
	 * 
148
	 * @return void
149
	 */
150
	protected function filter_classes() {
151
152
		$this->classes = apply_filters( 'nav_menu_css_class', $this->classes, $this );
153
	
154
	}
155
156
	/**
157
	 * Updates children nesting level param
158
	 * 
159
	 * @return boolean
160
	 */
161
	protected function update_child_levels() {
162
163
		if (is_array($this->children)) {
164
		
165
			foreach( $this->children as $child ) {
166
				$child->level = $this->level + 1;
167
				$child->update_child_levels();
168
			}
169
		
170
			return true;
171
		
172
		}
173
174
		return false;
175
176
	}
177
178
}