Menu_Item::get_children()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 4
Bugs 1 Features 1
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 3
rs 10
c 4
b 1
f 1
1
<?php
2
/**
3
 * Class for handling menu item functionality.
4
 *
5
 * @package Classy
6
 */
7
8
namespace Classy;
9
10
/**
11
 * Class Menu_Item.
12
 */
13
class Menu_Item extends Basis {
14
15
	/**
16
	 * Children.
17
	 *
18
	 * @var array
19
	 */
20
	protected $children = array();
21
22
	/**
23
	 * CSS Classes.
24
	 *
25
	 * @var array
26
	 */
27
	protected $classes = array();
28
29
	/**
30
	 * If item has child.
31
	 *
32
	 * @var boolean
33
	 */
34
	protected $has_child = false;
35
36
	/**
37
	 * Nesting level.
38
	 *
39
	 * @var integer
40
	 */
41
	public $level = 0;
42
43
	/**
44
	 * Item title.
45
	 *
46
	 * @var string
47
	 */
48
	public $title;
49
50
	/**
51
	 * Checks if provided arg is instance of WP_Post and inits it.
52
	 *
53
	 * @param \WP_Post $item WP_Post object.
54
	 */
55
	public function __construct( $item ) {
56
		if ( is_a( $item, '\WP_Post' ) ) {
57
			$this->import( $item );
58
			$this->filter_classes();
59
		}
60
	}
61
62
	/**
63
	 * Returns item title.
64
	 *
65
	 * @return string
66
	 */
67
	public function get_title() {
68
		return $this->title;
69
	}
70
71
	/**
72
	 * Returns item slug.
73
	 *
74
	 * @return string
75
	 */
76
	public function get_slug() {
77
		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...
78
	}
79
80
	/**
81
	 * Returns item link (url).
82
	 *
83
	 * @return string
84
	 */
85
	public function get_link() {
86
		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...
87
	}
88
89
	/**
90
	 * Returns item children, if there are any.
91
	 *
92
	 * @return array
93
	 */
94
	public function get_children() {
95
		return $this->children;
96
	}
97
98
	/**
99
	 * Returns menu item classes.
100
	 *
101
	 * @return string
102
	 */
103
	public function get_classes() {
104
		return implode( ' ', $this->classes );
105
	}
106
107
	/**
108
	 * Adds css class to classes array.
109
	 *
110
	 * @param string $class_name CSS class name.
111
	 */
112
	public function add_class( $class_name ) {
113
		$this->classes[] = $class_name;
114
	}
115
116
	/**
117
	 * Adds child to current Menu_Item.
118
	 *
119
	 * @param Menu_Item $item Menu_Item object.
120
	 */
121
	public function add_child( $item ) {
122
		if ( ! $this->has_child ) {
123
			$this->add_class( 'menu-item-has-children' );
124
			$this->has_child = true;
125
		}
126
127
		$this->children[] = $item;
128
		$item->level = $this->level + 1;
129
130
		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...
131
			$this->update_child_levels();
132
		}
133
	}
134
135
	/**
136
	 * Applies filters for item classes.
137
	 */
138
	protected function filter_classes() {
139
		$this->classes = apply_filters( 'nav_menu_css_class', $this->classes, $this );
140
	}
141
142
	/**
143
	 * Updates children nesting level param.
144
	 *
145
	 * @return boolean
146
	 */
147 View Code Duplication
	protected function update_child_levels() {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
148
		if ( is_array( $this->children ) ) {
149
			foreach ( $this->children as $child ) {
150
				$child->level = $this->level + 1;
151
				$child->update_child_levels();
152
			}
153
154
			return true;
155
		}
156
157
		return false;
158
	}
159
}
160