Conditions | 34 |
Paths | > 20000 |
Total Lines | 213 |
Lines | 7 |
Ratio | 3.29 % |
Changes | 0 |
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:
If many parameters/temporary variables are present:
1 | <?php |
||
112 | public function start_el( &$output, $item, $depth = 0, $args = null, $id = 0 ) { |
||
113 | View Code Duplication | if ( isset( $args->item_spacing ) && 'discard' === $args->item_spacing ) { |
|
114 | $t = ''; |
||
115 | $n = ''; |
||
116 | } else { |
||
117 | $t = "\t"; |
||
118 | $n = "\n"; |
||
119 | } |
||
120 | $indent = ( $depth ) ? str_repeat( $t, $depth ) : ''; |
||
121 | |||
122 | if ( false !== strpos( $args->items_wrap, 'itemscope' ) && false === $this->has_schema ) { |
||
123 | $this->has_schema = true; |
||
124 | $args->link_before = '<span itemprop="name">' . $args->link_before; |
||
125 | $args->link_after .= '</span>'; |
||
126 | } |
||
127 | |||
128 | $classes = empty( $item->classes ) ? array() : (array) $item->classes; |
||
129 | |||
130 | // Updating the CSS classes of a menu item in the WordPress Customizer preview results in all classes defined |
||
131 | // in that particular input box to come in as one big class string. |
||
132 | $split_on_spaces = function ( $class ) { |
||
133 | return preg_split( '/\s+/', $class ); |
||
134 | }; |
||
135 | $classes = $this->flatten( array_map( $split_on_spaces, $classes ) ); |
||
136 | |||
137 | /* |
||
138 | * Initialize some holder variables to store specially handled item |
||
139 | * wrappers and icons. |
||
140 | */ |
||
141 | $linkmod_classes = array(); |
||
142 | $icon_classes = array(); |
||
143 | |||
144 | /* |
||
145 | * Get an updated $classes array without linkmod or icon classes. |
||
146 | * |
||
147 | * NOTE: linkmod and icon class arrays are passed by reference and |
||
148 | * are maybe modified before being used later in this function. |
||
149 | */ |
||
150 | $classes = self::separate_linkmods_and_icons_from_classes( $classes, $linkmod_classes, $icon_classes, $depth ); |
||
151 | |||
152 | // Join any icon classes plucked from $classes into a string. |
||
153 | $icon_class_string = join( ' ', $icon_classes ); |
||
154 | |||
155 | /** |
||
156 | * Filters the arguments for a single nav menu item. |
||
157 | * |
||
158 | * @since WP 4.4.0 |
||
159 | * |
||
160 | * @param WP_Nav_Menu_Args $args An object of wp_nav_menu() arguments. |
||
161 | * @param WP_Nav_Menu_Item $item Menu item data object. |
||
162 | * @param int $depth Depth of menu item. Used for padding. |
||
163 | * |
||
164 | * @var WP_Nav_Menu_Args |
||
165 | */ |
||
166 | $args = apply_filters( 'nav_menu_item_args', $args, $item, $depth ); |
||
167 | |||
168 | // Add .dropdown or .active classes where they are needed. |
||
169 | if ( $this->has_children ) { |
||
170 | $classes[] = 'dropdown'; |
||
171 | } |
||
172 | if ( in_array( 'current-menu-item', $classes, true ) || in_array( 'current-menu-parent', $classes, true ) ) { |
||
173 | $classes[] = 'active'; |
||
174 | } |
||
175 | |||
176 | // Add some additional default classes to the item. |
||
177 | $classes[] = 'menu-item-' . $item->ID; |
||
178 | $classes[] = 'nav-item'; |
||
179 | |||
180 | // Allow filtering the classes. |
||
181 | $classes = apply_filters( 'nav_menu_css_class', array_filter( $classes ), $item, $args, $depth ); |
||
182 | |||
183 | // Form a string of classes in format: class="class_names". |
||
184 | $class_names = join( ' ', $classes ); |
||
185 | $class_names = $class_names ? ' class="' . esc_attr( $class_names ) . '"' : ''; |
||
186 | |||
187 | /** |
||
188 | * Filters the ID applied to a menu item's list item element. |
||
189 | * |
||
190 | * @since WP 3.0.1 |
||
191 | * @since WP 4.1.0 The `$depth` parameter was added. |
||
192 | * |
||
193 | * @param string $menu_id The ID that is applied to the menu item's `<li>` element. |
||
194 | * @param WP_Nav_Menu_Item $item The current menu item. |
||
195 | * @param WP_Nav_Menu_Args $args An object of wp_nav_menu() arguments. |
||
196 | * @param int $depth Depth of menu item. Used for padding. |
||
197 | */ |
||
198 | $id = apply_filters( 'nav_menu_item_id', 'menu-item-' . $item->ID, $item, $args, $depth ); |
||
199 | $id = $id ? ' id="' . esc_attr( $id ) . '"' : ''; |
||
200 | |||
201 | $output .= $indent . '<li ' . $id . $class_names . '>'; |
||
202 | |||
203 | // Initialize array for holding the $atts for the link item. |
||
204 | $atts = array(); |
||
205 | $atts['title'] = ! empty( $item->attr_title ) ? $item->attr_title : ''; |
||
206 | $atts['target'] = ! empty( $item->target ) ? $item->target : ''; |
||
207 | if ( '_blank' === $item->target && empty( $item->xfn ) ) { |
||
208 | $atts['rel'] = 'noopener noreferrer'; |
||
209 | } else { |
||
210 | $atts['rel'] = ! empty( $item->xfn ) ? $item->xfn : ''; |
||
211 | } |
||
212 | |||
213 | // If the item has_children add atts to <a>. |
||
214 | if ( $this->has_children && 0 === $depth ) { |
||
215 | $atts['href'] = '#'; |
||
216 | $atts['data-toggle'] = 'dropdown'; |
||
217 | $atts['aria-haspopup'] = 'true'; |
||
218 | $atts['aria-expanded'] = 'false'; |
||
219 | $atts['class'] = 'dropdown-toggle nav-link'; |
||
220 | $atts['id'] = 'menu-item-dropdown-' . $item->ID; |
||
221 | } else { |
||
222 | if ( true === $this->has_schema ) { |
||
223 | $atts['itemprop'] = 'url'; |
||
224 | } |
||
225 | |||
226 | $atts['href'] = ! empty( $item->url ) ? $item->url : '#'; |
||
227 | // For items in dropdowns use .dropdown-item instead of .nav-link. |
||
228 | if ( $depth > 0 ) { |
||
229 | $atts['class'] = 'dropdown-item'; |
||
230 | } else { |
||
231 | $atts['class'] = 'nav-link'; |
||
232 | } |
||
233 | } |
||
234 | |||
235 | $atts['aria-current'] = $item->current ? 'page' : ''; |
||
236 | |||
237 | // Update atts of this item based on any custom linkmod classes. |
||
238 | $atts = self::update_atts_for_linkmod_type( $atts, $linkmod_classes ); |
||
239 | |||
240 | // Allow filtering of the $atts array before using it. |
||
241 | $atts = apply_filters( 'nav_menu_link_attributes', $atts, $item, $args, $depth ); |
||
242 | |||
243 | // Build a string of html containing all the atts for the item. |
||
244 | $attributes = ''; |
||
245 | foreach ( $atts as $attr => $value ) { |
||
246 | if ( ! empty( $value ) ) { |
||
247 | $value = ( 'href' === $attr ) ? esc_url( $value ) : esc_attr( $value ); |
||
248 | $attributes .= ' ' . $attr . '="' . $value . '"'; |
||
249 | } |
||
250 | } |
||
251 | |||
252 | // Set a typeflag to easily test if this is a linkmod or not. |
||
253 | $linkmod_type = self::get_linkmod_type( $linkmod_classes ); |
||
254 | |||
255 | // START appending the internal item contents to the output. |
||
256 | $item_output = isset( $args->before ) ? $args->before : ''; |
||
257 | |||
258 | /* |
||
259 | * This is the start of the internal nav item. Depending on what |
||
260 | * kind of linkmod we have we may need different wrapper elements. |
||
261 | */ |
||
262 | if ( '' !== $linkmod_type ) { |
||
263 | // Is linkmod, output the required element opener. |
||
264 | $item_output .= self::linkmod_element_open( $linkmod_type, $attributes ); |
||
265 | } else { |
||
266 | // With no link mod type set this must be a standard <a> tag. |
||
267 | $item_output .= '<a' . $attributes . '>'; |
||
268 | } |
||
269 | |||
270 | /* |
||
271 | * Initiate empty icon var, then if we have a string containing any |
||
272 | * icon classes form the icon markup with an <i> element. This is |
||
273 | * output inside of the item before the $title (the link text). |
||
274 | */ |
||
275 | $icon_html = ''; |
||
276 | if ( ! empty( $icon_class_string ) ) { |
||
277 | // Append an <i> with the icon classes to what is output before links. |
||
278 | $icon_html = '<i class="' . esc_attr( $icon_class_string ) . '" aria-hidden="true"></i> '; |
||
279 | } |
||
280 | |||
281 | /** This filter is documented in wp-includes/post-template.php */ |
||
282 | $title = apply_filters( 'the_title', $item->title, $item->ID ); |
||
283 | |||
284 | /** |
||
285 | * Filters a menu item's title. |
||
286 | * |
||
287 | * @since WP 4.4.0 |
||
288 | * |
||
289 | * @param string $title The menu item's title. |
||
290 | * @param WP_Nav_Menu_Item $item The current menu item. |
||
291 | * @param WP_Nav_Menu_Args $args An object of wp_nav_menu() arguments. |
||
292 | * @param int $depth Depth of menu item. Used for padding. |
||
293 | */ |
||
294 | $title = apply_filters( 'nav_menu_item_title', $title, $item, $args, $depth ); |
||
295 | |||
296 | // If the .sr-only class was set apply to the nav items text only. |
||
297 | if ( in_array( 'sr-only', $linkmod_classes, true ) ) { |
||
298 | $title = self::wrap_for_screen_reader( $title ); |
||
299 | $keys_to_unset = array_keys( $linkmod_classes, 'sr-only', true ); |
||
300 | foreach ( $keys_to_unset as $k ) { |
||
301 | unset( $linkmod_classes[ $k ] ); |
||
302 | } |
||
303 | } |
||
304 | |||
305 | // Put the item contents into $output. |
||
306 | $item_output .= isset( $args->link_before ) ? $args->link_before . $icon_html . $title . $args->link_after : ''; |
||
307 | |||
308 | /* |
||
309 | * This is the end of the internal nav item. We need to close the |
||
310 | * correct element depending on the type of link or link mod. |
||
311 | */ |
||
312 | if ( '' !== $linkmod_type ) { |
||
313 | // Is linkmod, output the required closing element. |
||
314 | $item_output .= self::linkmod_element_close( $linkmod_type ); |
||
315 | } else { |
||
316 | // With no link mod type set this must be a standard <a> tag. |
||
317 | $item_output .= '</a>'; |
||
318 | } |
||
319 | |||
320 | $item_output .= isset( $args->after ) ? $args->after : ''; |
||
321 | |||
322 | // END appending the internal item contents to the output. |
||
323 | $output .= apply_filters( 'walker_nav_menu_start_el', $item_output, $item, $depth, $args ); |
||
324 | } |
||
325 | |||
608 |
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.