Walker_Nav_Menu::start_el()   F
last analyzed

Complexity

Conditions 14
Paths 2048

Size

Total Lines 125
Code Lines 35

Duplication

Lines 13
Ratio 10.4 %

Importance

Changes 0
Metric Value
cc 14
eloc 35
nc 2048
nop 5
dl 13
loc 125
rs 2
c 0
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**
3
 * Nav Menu API: Walker_Nav_Menu class
4
 *
5
 * @package WordPress
6
 * @subpackage Nav_Menus
7
 * @since 4.6.0
8
 */
9
10
/**
11
 * Core class used to implement an HTML list of nav menu items.
12
 *
13
 * @since 3.0.0
14
 *
15
 * @see Walker
16
 */
17
class Walker_Nav_Menu extends Walker {
18
	/**
19
	 * What the class handles.
20
	 *
21
	 * @since 3.0.0
22
	 * @access public
23
	 * @var string
24
	 *
25
	 * @see Walker::$tree_type
26
	 */
27
	public $tree_type = array( 'post_type', 'taxonomy', 'custom' );
28
29
	/**
30
	 * Database fields to use.
31
	 *
32
	 * @since 3.0.0
33
	 * @access public
34
	 * @todo Decouple this.
35
	 * @var array
36
	 *
37
	 * @see Walker::$db_fields
38
	 */
39
	public $db_fields = array( 'parent' => 'menu_item_parent', 'id' => 'db_id' );
40
41
	/**
42
	 * Starts the list before the elements are added.
43
	 *
44
	 * @since 3.0.0
45
	 *
46
	 * @see Walker::start_lvl()
47
	 *
48
	 * @param string   $output Passed by reference. Used to append additional content.
49
	 * @param int      $depth  Depth of menu item. Used for padding.
50
	 * @param stdClass $args   An object of wp_nav_menu() arguments.
0 ignored issues
show
Documentation introduced by
Should the type for parameter $args not be stdClass|array? Also, consider making the array more specific, something like array<String>, or String[].

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive. In addition it looks for parameters that have the generic type array and suggests a stricter type like array<String>.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
51
	 */
52
	public function start_lvl( &$output, $depth = 0, $args = array() ) {
53 View Code Duplication
		if ( isset( $args->item_spacing ) && 'discard' === $args->item_spacing ) {
54
			$t = '';
55
			$n = '';
56
		} else {
57
			$t = "\t";
58
			$n = "\n";
59
		}
60
		$indent = str_repeat( $t, $depth );
61
62
		// Default class.
63
		$classes = array( 'sub-menu' );
64
65
		/**
66
		 * Filters the CSS class(es) applied to a menu list element.
67
		 *
68
		 * @since 4.8.0
69
		 *
70
		 * @param array    $classes The CSS classes that are applied to the menu `<ul>` element.
71
		 * @param stdClass $args    An object of `wp_nav_menu()` arguments.
72
		 * @param int      $depth   Depth of menu item. Used for padding.
73
		 */
74
		$class_names = join( ' ', apply_filters( 'nav_menu_submenu_css_class', $classes, $args, $depth ) );
75
		$class_names = $class_names ? ' class="' . esc_attr( $class_names ) . '"' : '';
76
77
		$output .= "{$n}{$indent}<ul $class_names>{$n}";
78
	}
79
80
	/**
81
	 * Ends the list of after the elements are added.
82
	 *
83
	 * @since 3.0.0
84
	 *
85
	 * @see Walker::end_lvl()
86
	 *
87
	 * @param string   $output Passed by reference. Used to append additional content.
88
	 * @param int      $depth  Depth of menu item. Used for padding.
89
	 * @param stdClass $args   An object of wp_nav_menu() arguments.
0 ignored issues
show
Documentation introduced by
Should the type for parameter $args not be stdClass|array? Also, consider making the array more specific, something like array<String>, or String[].

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive. In addition it looks for parameters that have the generic type array and suggests a stricter type like array<String>.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
90
	 */
91
	public function end_lvl( &$output, $depth = 0, $args = array() ) {
92 View Code Duplication
		if ( isset( $args->item_spacing ) && 'discard' === $args->item_spacing ) {
93
			$t = '';
94
			$n = '';
95
		} else {
96
			$t = "\t";
97
			$n = "\n";
98
		}
99
		$indent = str_repeat( $t, $depth );
100
		$output .= "$indent</ul>{$n}";
101
	}
102
103
	/**
104
	 * Starts the element output.
105
	 *
106
	 * @since 3.0.0
107
	 * @since 4.4.0 The {@see 'nav_menu_item_args'} filter was added.
108
	 *
109
	 * @see Walker::start_el()
110
	 *
111
	 * @param string   $output Passed by reference. Used to append additional content.
112
	 * @param WP_Post  $item   Menu item data object.
113
	 * @param int      $depth  Depth of menu item. Used for padding.
114
	 * @param stdClass $args   An object of wp_nav_menu() arguments.
0 ignored issues
show
Documentation introduced by
Should the type for parameter $args not be stdClass|array? Also, consider making the array more specific, something like array<String>, or String[].

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive. In addition it looks for parameters that have the generic type array and suggests a stricter type like array<String>.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
115
	 * @param int      $id     Current item ID.
116
	 */
117
	public function start_el( &$output, $item, $depth = 0, $args = array(), $id = 0 ) {
118 View Code Duplication
		if ( isset( $args->item_spacing ) && 'discard' === $args->item_spacing ) {
119
			$t = '';
120
			$n = '';
121
		} else {
122
			$t = "\t";
123
			$n = "\n";
124
		}
125
		$indent = ( $depth ) ? str_repeat( $t, $depth ) : '';
126
127
		$classes = empty( $item->classes ) ? array() : (array) $item->classes;
0 ignored issues
show
Documentation introduced by
The property classes does not exist on object<WP_Post>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
128
		$classes[] = 'menu-item-' . $item->ID;
129
130
		/**
131
		 * Filters the arguments for a single nav menu item.
132
		 *
133
		 * @since 4.4.0
134
		 *
135
		 * @param stdClass $args  An object of wp_nav_menu() arguments.
136
		 * @param WP_Post  $item  Menu item data object.
137
		 * @param int      $depth Depth of menu item. Used for padding.
138
		 */
139
		$args = apply_filters( 'nav_menu_item_args', $args, $item, $depth );
140
141
		/**
142
		 * Filters the CSS class(es) applied to a menu item's list item element.
143
		 *
144
		 * @since 3.0.0
145
		 * @since 4.1.0 The `$depth` parameter was added.
146
		 *
147
		 * @param array    $classes The CSS classes that are applied to the menu item's `<li>` element.
148
		 * @param WP_Post  $item    The current menu item.
149
		 * @param stdClass $args    An object of wp_nav_menu() arguments.
150
		 * @param int      $depth   Depth of menu item. Used for padding.
151
		 */
152
		$class_names = join( ' ', apply_filters( 'nav_menu_css_class', array_filter( $classes ), $item, $args, $depth ) );
153
		$class_names = $class_names ? ' class="' . esc_attr( $class_names ) . '"' : '';
154
155
		/**
156
		 * Filters the ID applied to a menu item's list item element.
157
		 *
158
		 * @since 3.0.1
159
		 * @since 4.1.0 The `$depth` parameter was added.
160
		 *
161
		 * @param string   $menu_id The ID that is applied to the menu item's `<li>` element.
162
		 * @param WP_Post  $item    The current menu item.
163
		 * @param stdClass $args    An object of wp_nav_menu() arguments.
164
		 * @param int      $depth   Depth of menu item. Used for padding.
165
		 */
166
		$id = apply_filters( 'nav_menu_item_id', 'menu-item-'. $item->ID, $item, $args, $depth );
167
		$id = $id ? ' id="' . esc_attr( $id ) . '"' : '';
168
169
		$output .= $indent . '<li' . $id . $class_names .'>';
170
171
		$atts = array();
172
		$atts['title']  = ! empty( $item->attr_title ) ? $item->attr_title : '';
0 ignored issues
show
Documentation introduced by
The property attr_title does not exist on object<WP_Post>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
173
		$atts['target'] = ! empty( $item->target )     ? $item->target     : '';
0 ignored issues
show
Documentation introduced by
The property target does not exist on object<WP_Post>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
174
		$atts['rel']    = ! empty( $item->xfn )        ? $item->xfn        : '';
0 ignored issues
show
Documentation introduced by
The property xfn does not exist on object<WP_Post>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
175
		$atts['href']   = ! empty( $item->url )        ? $item->url        : '';
0 ignored issues
show
Documentation introduced by
The property url does not exist on object<WP_Post>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
176
177
		/**
178
		 * Filters the HTML attributes applied to a menu item's anchor element.
179
		 *
180
		 * @since 3.6.0
181
		 * @since 4.1.0 The `$depth` parameter was added.
182
		 *
183
		 * @param array $atts {
184
		 *     The HTML attributes applied to the menu item's `<a>` element, empty strings are ignored.
185
		 *
186
		 *     @type string $title  Title attribute.
187
		 *     @type string $target Target attribute.
188
		 *     @type string $rel    The rel attribute.
189
		 *     @type string $href   The href attribute.
190
		 * }
191
		 * @param WP_Post  $item  The current menu item.
192
		 * @param stdClass $args  An object of wp_nav_menu() arguments.
193
		 * @param int      $depth Depth of menu item. Used for padding.
194
		 */
195
		$atts = apply_filters( 'nav_menu_link_attributes', $atts, $item, $args, $depth );
196
197
		$attributes = '';
198 View Code Duplication
		foreach ( $atts as $attr => $value ) {
199
			if ( ! empty( $value ) ) {
200
				$value = ( 'href' === $attr ) ? esc_url( $value ) : esc_attr( $value );
201
				$attributes .= ' ' . $attr . '="' . $value . '"';
202
			}
203
		}
204
205
		/** This filter is documented in wp-includes/post-template.php */
206
		$title = apply_filters( 'the_title', $item->title, $item->ID );
0 ignored issues
show
Documentation introduced by
The property title does not exist on object<WP_Post>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
207
208
		/**
209
		 * Filters a menu item's title.
210
		 *
211
		 * @since 4.4.0
212
		 *
213
		 * @param string   $title The menu item's title.
214
		 * @param WP_Post  $item  The current menu item.
215
		 * @param stdClass $args  An object of wp_nav_menu() arguments.
216
		 * @param int      $depth Depth of menu item. Used for padding.
217
		 */
218
		$title = apply_filters( 'nav_menu_item_title', $title, $item, $args, $depth );
219
220
		$item_output = $args->before;
221
		$item_output .= '<a'. $attributes .'>';
222
		$item_output .= $args->link_before . $title . $args->link_after;
223
		$item_output .= '</a>';
224
		$item_output .= $args->after;
225
226
		/**
227
		 * Filters a menu item's starting output.
228
		 *
229
		 * The menu item's starting output only includes `$args->before`, the opening `<a>`,
230
		 * the menu item's title, the closing `</a>`, and `$args->after`. Currently, there is
231
		 * no filter for modifying the opening and closing `<li>` for a menu item.
232
		 *
233
		 * @since 3.0.0
234
		 *
235
		 * @param string   $item_output The menu item's starting HTML output.
236
		 * @param WP_Post  $item        Menu item data object.
237
		 * @param int      $depth       Depth of menu item. Used for padding.
238
		 * @param stdClass $args        An object of wp_nav_menu() arguments.
239
		 */
240
		$output .= apply_filters( 'walker_nav_menu_start_el', $item_output, $item, $depth, $args );
241
	}
242
243
	/**
244
	 * Ends the element output, if needed.
245
	 *
246
	 * @since 3.0.0
247
	 *
248
	 * @see Walker::end_el()
249
	 *
250
	 * @param string   $output Passed by reference. Used to append additional content.
251
	 * @param WP_Post  $item   Page data object. Not used.
252
	 * @param int      $depth  Depth of page. Not Used.
253
	 * @param stdClass $args   An object of wp_nav_menu() arguments.
0 ignored issues
show
Documentation introduced by
Should the type for parameter $args not be stdClass|array? Also, consider making the array more specific, something like array<String>, or String[].

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive. In addition it looks for parameters that have the generic type array and suggests a stricter type like array<String>.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
254
	 */
255
	public function end_el( &$output, $item, $depth = 0, $args = array() ) {
256 View Code Duplication
		if ( isset( $args->item_spacing ) && 'discard' === $args->item_spacing ) {
257
			$t = '';
258
			$n = '';
259
		} else {
260
			$t = "\t";
261
			$n = "\n";
262
		}
263
		$output .= "</li>{$n}";
264
	}
265
266
} // Walker_Nav_Menu
267