Completed
Push — master ( 205170...4d736f )
by
unknown
8s
created

Menu   B

Complexity

Total Complexity 36

Size/Duplication

Total Lines 183
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 4
Bugs 1 Features 3
Metric Value
c 4
b 1
f 3
dl 0
loc 183
rs 8.8
wmc 36
lcom 1
cbo 1

8 Methods

Rating   Name   Duplication   Size   Complexity  
B __construct() 0 21 8
B init() 0 22 6
A get_first_menu_id() 0 11 4
A get_locations_menu_id() 0 11 3
B check_menu_id() 0 13 5
B get_menu_id_by_name() 0 13 5
A get_menu_name_by_id() 0 11 4
A get_items() 0 3 1
1
<?php
2
/**
3
 * Class for handling menu functionality.
4
 *
5
 * @package Classy
6
 */
7
8
namespace Classy;
9
10
/**
11
 * Class Menu.
12
 */
13
class Menu {
14
15
	/**
16
	 * Menu ID.
17
	 *
18
	 * @var int
19
	 */
20
	public $ID;
21
22
	/**
23
	 * Menu title.
24
	 *
25
	 * @var string
26
	 */
27
	public $title;
28
29
	/**
30
	 * Items.
31
	 *
32
	 * @var array
33
	 */
34
	protected $items;
35
36
	/**
37
	 * Main constructor. Tries to find menu id based on provided arg (or not) and inits menu.
38
	 *
39
	 * @param string $arg It can be menu id, slug or full name.
40
	 */
41
	public function __construct( $arg = null ) {
42
		$locations = get_nav_menu_locations();
43
44
		if ( is_numeric( $arg ) && 0 !== absint( $arg ) ) {
45
			$menu_id = $this->check_menu_id( $arg );
46
		} elseif ( is_array( $locations ) && count( $locations ) ) {
47
			$menu_id = $this->get_locations_menu_id( $locations, $arg );
48
		} elseif ( is_string( $arg ) ) {
49
			$menu_id = $this->get_menu_id_by_name( $arg );
50
		}
51
52
		if ( ! isset( $menu_id ) ) {
53
			$menu_id = $this->get_first_menu_id();
54
		}
55
56
		if ( $menu_id ) {
57
			$this->ID = $menu_id;
0 ignored issues
show
Documentation Bug introduced by
It seems like $menu_id can also be of type boolean. However, the property $ID is declared as type integer. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
58
			$this->title = $this->get_menu_name_by_id($menu_id);
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->get_menu_name_by_id($menu_id) of type integer or boolean is incompatible with the declared type string of property $title.

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...
59
			$this->init();
60
		}
61
	}
62
63
	/**
64
	 * Init menu.
65
	 */
66
	protected function init() {
67
		$_return = array();
68
		$items = wp_get_nav_menu_items( $this->ID );
69
70
		foreach ( $items as $item ) {
71
			$_return[ $item->ID ] = new Menu_Item( $item );
72
		}
73
74
		// Apply nesting.
75
		foreach ( $_return as $item_id => $item ) {
76
			if (
77
				isset( $item->menu_item_parent ) &&
78
				$item->menu_item_parent &&
79
				isset( $_return[ $item->menu_item_parent ] )
80
			) {
81
				$_return[ $item->menu_item_parent ]->add_child( $item );
82
				unset( $_return[ $item_id ] );
83
			}
84
		}
85
86
		$this->items = $_return;
87
	}
88
89
	/**
90
	 * Returns first menu id or false if there are no menus.
91
	 *
92
	 * @return int|bool
93
	 */
94
	protected function get_first_menu_id() {
95
		$menus = get_terms( 'nav_menu', array( 'hide_empty' => true ) );
96
97
		if ( is_array( $menus ) && count( $menus ) ) {
98
			if ( isset( $menus[0]->term_id ) ) {
99
				return $menus[0]->term_id;
100
			}
101
		}
102
103
		return false;
104
	}
105
106
	/**
107
	 * Performs menu id lookup inside navigation Menus.
108
	 *
109
	 * @param  array      $locations locations that are returned by get_nav_menu_locations().
110
	 * @param  int|string $arg navigation id.
111
	 * @return int
112
	 */
113
	protected function get_locations_menu_id( $locations, $arg ) {
114
		if ( is_numeric( $arg ) ) {
115
			$arg = array_search( $arg, $locations );
116
		}
117
118
		if ( isset( $locations[ $arg ] ) ) {
119
			$menu_id = $locations[ $arg ];
120
121
			return $menu_id;
122
		}
123
	}
124
125
	/**
126
	 * Checks if the provided menu id exists.
127
	 *
128
	 * @param int $menu_id Menu ID.
129
	 *
130
	 * @return int|boolean
131
	 */
132
	protected function check_menu_id( $menu_id ) {
133
		$menus = get_terms( 'nav_menu', array( 'hide_empty' => true ) );
134
135
		if ( is_array( $menus ) && count( $menus ) ) {
136
			foreach ( $menus as $menu ) {
137
				if ( absint( $menu->term_id ) === absint( $menu_id ) ) {
138
					return $menu_id;
139
				}
140
			}
141
		}
142
143
		return false;
144
	}
145
146
	/**
147
	 * Returns menu id by menu name/slug.
148
	 *
149
	 * @param string $slug Menu's name.
150
	 *
151
	 * @return int|bool
152
	 */
153
	protected function get_menu_id_by_name( $slug = null ) {
154
		if ( $slug && is_string( $slug ) ) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $slug of type string|null is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
155
			if ( $menu_id = get_term_by( 'slug', $slug, 'nav_menu' ) ) {
156
				return $menu_id;
157
			}
158
159
			if ( $menu_id = get_term_by( 'name', $slug, 'nav_menu' ) ) {
160
				return $menu_id;
161
			}
162
		}
163
164
		return false;
165
	}
166
167
	/**
168
	 * Returns menu name by menu id.
169
	 *
170
	 * @param string $name Menu's name.
0 ignored issues
show
Bug introduced by
There is no parameter named $name. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
171
	 *
172
	 * @return int|bool
173
	 */
174
	protected function get_menu_name_by_id( $id = null ) {
175
		if ( $id && is_int( $id ) ) {
176
			$menu_obj = wp_get_nav_menu_object($id);
177
178
			if ( $menu_obj ) {
179
				return $menu_obj->name;
180
			}
181
		}
182
183
		return false;
184
	}
185
186
187
	/**
188
	 * Returns menu items.
189
	 *
190
	 * @return array
191
	 */
192
	public function get_items() {
193
		return $this->items;
194
	}
195
}
196