Completed
Push — master ( bc6754...ed64c2 )
by
unknown
07:19
created

Menu_Item   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 167
Duplicated Lines 9.58 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 16
loc 167
rs 10
wmc 15
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 15 3
A filter_classes() 0 5 1
A update_child_levels() 16 16 3

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace Classy;
4
5
/**
6
 * Class for handling menu item functionality
7
 */
8
9
class Menu_Item extends Basis {
10
11
	/**
12
	 * Children
13
	 * @var array
14
	 */
15
	protected $children = array();
16
17
	/**
18
	 * CSS Classes
19
	 * @var array
20
	 */
21
	protected $classes = array();
22
23
	/**
24
	 * If item has child
25
	 * @var boolean
26
	 */
27
	protected $has_child = false;
28
29
	/**
30
	 * Nesting level
31
	 * @var integer
32
	 */
33
	public $level = 0;
34
35
	/**
36
	 * Item title
37
	 * @var string
38
	 */
39
	public $title;
40
41
	/**
42
	 * Checks if provided arg is instance of WP_Post and inits it
43
	 *
44
	 * @param \WP_Post $item
45
	 */
46
	public function __construct( $item ) {
47
48
		if ( is_a( $item, '\WP_Post' ) ) {
49
50
			$this->import( $item );
51
			$this->filter_classes();
52
53
		}
54
55
	}
56
57
	/**
58
	 * Returns item title
59
	 *
60
	 * @return string
61
	 */
62
	public function get_title() {
63
64
		return $this->title;
65
66
	}
67
68
	/**
69
	 * Returns item slug
70
	 *
71
	 * @return string
72
	 */
73
	public function get_slug() {
74
75
		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...
76
77
	}
78
79
	/**
80
	 * Returns item link (url)
81
	 *
82
	 * @return string
83
	 */
84
	public function get_link() {
85
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
	/**
91
	 * Retuns item children, if there are any
92
	 *
93
	 * @return array
94
	 */
95
	public function get_children() {
96
97
		return $this->children;
98
99
	}
100
101
	/**
102
	 * Returns menu item classes
103
	 * @return string
104
	 */
105
	public function get_classes() {
106
107
		return implode( ' ', $this->classes );
108
109
	}
110
111
	/**
112
	 * Adds css class to classes array
113
	 *
114
	 * @param string $class_name
115
	 */
116
	public function add_class( $class_name ) {
117
118
		$this->classes[] = $class_name;
119
120
	}
121
122
	/**
123
	 * Adds child to current Menu_Item
124
	 *
125
	 * @param Menu_Item $item
126
	 */
127
	public function add_child( $item ) {
128
129
		if ( ! $this->has_child ) {
130
			$this->add_class( 'menu-item-has-children' );
131
			$this->has_child = true;
132
		}
133
134
		$this->children[] = $item;
135
		$item->level = $this->level + 1;
136
137
		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...
138
			$this->update_child_levels();
139
		}
140
141
	}
142
143
	/**
144
	 * Applies filters for item classes
145
	 *
146
	 * @return void
147
	 */
148
	protected function filter_classes() {
149
150
		$this->classes = apply_filters( 'nav_menu_css_class', $this->classes, $this );
151
152
	}
153
154
	/**
155
	 * Updates children nesting level param
156
	 *
157
	 * @return boolean
158
	 */
159 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...
160
161
		if ( is_array( $this->children ) ) {
162
163
			foreach ( $this->children as $child ) {
164
				$child->level = $this->level + 1;
165
				$child->update_child_levels();
166
			}
167
168
			return true;
169
170
		}
171
172
		return false;
173
174
	}
175
}
176