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

Menu   A

Complexity

Total Complexity 27

Size/Duplication

Total Lines 162
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 162
rs 10
wmc 27
lcom 1
cbo 1

6 Methods

Rating   Name   Duplication   Size   Complexity  
B __construct() 0 27 6
B init() 0 27 6
A get_first_menu_id() 0 16 4
B check_menu_id() 0 19 5
B get_menu_id_by_name() 0 16 5
A get_items() 0 5 1
1
<?php
2
namespace Classy;
3
4
/**
5
 * Class for handling menu functionality
6
 */
7
8
class Menu {
9
10
	/**
11
	 * Menu ID
12
	 * @var int
13
	 */
14
	public $ID;
15
16
	/**
17
	 * Items
18
	 * @var array
19
	 */
20
	protected $items;
21
22
	/**
23
	 * Main constructor. Tries to find menu id based on provided arg (or not) and inits menu
24
	 *
25
	 * @param string $arg it can be menu id, slug or full name
26
	 */
27
	public function __construct( $arg = null ) {
28
29
		if ( is_numeric( $arg ) && 0 !== absint( $arg ) ) {
30
31
			$menu_id = $this->check_menu_id( $arg );
32
33
		} elseif ( is_string( $arg ) ) {
34
35
			$menu_id = $this->get_menu_id_by_name( $arg );
36
37
		}
38
39
		if ( ! isset( $menu_id ) ) {
40
41
			$menu_id = $this->get_first_menu_id();
42
43
		}
44
45
		if ( $menu_id ) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $menu_id of type integer|false is loosely compared to true; this is ambiguous if the integer can be zero. 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 integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
46
47
			$this->ID = $menu_id;
48
49
			$this->init();
50
51
		}
52
53
	}
54
55
	/**
56
	 * Inits menu
57
	 *
58
	 * @return void
59
	 */
60
	protected function init() {
61
62
		$_return = array();
63
64
		$items = wp_get_nav_menu_items( $this->ID );
65
66
		foreach ( $items as $item ) {
67
68
			$_return[ $item->ID ] = new Menu_Item( $item );
69
		}
70
71
		// Apply nesting
72
73
		foreach ( $_return as $item_id => $item ) {
74
75
			if ( isset( $item->menu_item_parent ) && $item->menu_item_parent && isset( $_return[ $item->menu_item_parent ] ) ) {
76
77
				$_return[ $item->menu_item_parent ]->add_child( $item );
78
79
				unset( $_return[ $item_id ] );
80
81
			}
82
		}
83
84
		$this->items = $_return;
85
86
	}
87
88
	/**
89
	 * Retuns first menu id or false if there are no menus
90
	 *
91
	 * @return int
92
	 */
93
	protected function get_first_menu_id() {
94
95
		$menus = get_terms( 'nav_menu', array( 'hide_empty' => true ) );
96
97
		if ( is_array( $menus ) && count( $menus ) ) {
98
99
			if ( isset( $menus[0]->term_id ) ) {
100
101
				return $menus[0]->term_id;
102
103
			}
104
		}
105
106
		return false;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return false; (false) is incompatible with the return type documented by Classy\Menu::get_first_menu_id of type integer.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

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

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
107
108
	}
109
110
	/**
111
	 * Checks if the provided menu id exists
112
	 *
113
	 * @param  int $menu_id
114
	 * @return int|boolean
115
	 */
116
	protected function check_menu_id( $menu_id ) {
117
118
		$menus = get_terms( 'nav_menu', array( 'hide_empty' => true ) );
119
120
		if ( is_array( $menus ) && count( $menus ) ) {
121
122
			foreach ( $menus as $menu ) {
123
124
				if ( absint( $menu->term_id ) === absint( $menu_id ) ) {
125
126
					return $menu_id;
127
128
				}
129
			}
130
		}
131
132
		return false;
133
134
	}
135
136
	/**
137
	 * Returns menu id by menu name/slug
138
	 *
139
	 * @param  string $slug
140
	 * @return int
141
	 */
142
	protected function get_menu_id_by_name( $slug = null ) {
143
144
		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...
145
146
			$menu_id = get_term_by( 'slug', $slug, 'nav_menu' );
147
148
			if ( $menu_id ) { return $menu_id; }
149
150
			$menu_id = get_term_by( 'name', $slug, 'nav_menu' );
151
152
			if ( $menu_id ) { return $menu_id; }
153
		}
154
155
		return false;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return false; (false) is incompatible with the return type documented by Classy\Menu::get_menu_id_by_name of type integer.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

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

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
156
157
	}
158
159
	/**
160
	 * Returns menu items
161
	 *
162
	 * @return array
163
	 */
164
	public function get_items() {
165
166
		return $this->items;
167
168
	}
169
}
170