Completed
Push — master ( 2fd399...b41af7 )
by
unknown
08:39
created

BP_Nouveau_Nav_Customize_Control::render_content()   C

Complexity

Conditions 8
Paths 20

Size

Total Lines 67
Code Lines 43

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 8
eloc 43
nc 20
nop 0
dl 0
loc 67
rs 6.6523
c 0
b 0
f 0

How to fix   Long Method   

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
 * Customizer controls
4
 *
5
 * @since 1.0.0
6
 */
7
8
// Exit if accessed directly.
9
defined( 'ABSPATH' ) || exit;
10
11
/**
12
 * This control let users change the order of the BuddyPress
13
 * single items navigation items.
14
 *
15
 * NB: this is a first pass to improve by using Javascript templating as explained here:
16
 * https://developer.wordpress.org/themes/advanced-topics/customizer-api/#putting-the-pieces-together
17
 *
18
 * @since 1.0.0
19
 */
20
class BP_Nouveau_Nav_Customize_Control extends WP_Customize_Control {
21
	/**
22
	 * @var string
23
	 */
24
	public $type = '';
25
26
	/**
27
	 * Render the control's content.
28
	 *
29
	 * @since 1.0.0
30
	 */
31
	public function render_content() {
32
		$id       = 'customize-control-' . str_replace( '[', '-', str_replace( ']', '', $this->id ) );
33
		$class    = 'customize-control customize-control-' . $this->type;
34
		$setting  = "bp_nouveau_appearance[{$this->type}_nav_order]";
35
		$item_nav = array();
36
37
		// It's a group
38
		if ( 'group' === $this->type ) {
39
			$guide = __( 'Customizing the Groups navivation order needs you create at least one group first.', 'buddypress' );
40
41
			// Try to fetch any random group:
42
			$random = groups_get_groups(
43
				array(
44
					'type'        => 'random',
45
					'per_page'    => 1,
46
					'show_hidden' => true,
47
				)
48
			);
49
50
			if ( ! empty( $random['groups'] ) ) {
51
				$group    = reset( $random['groups'] );
52
				$nav      = new BP_Nouveau_Customizer_Group_Nav( $group->id );
53
				$item_nav = $nav->get_group_nav();
54
			}
55
56
			if ( $item_nav ) {
57
				$guide = __( 'All the possible group nav items are listed below, in some groups some of these nav items might not be active.', 'buddypress' );
58
			}
59
60
		// It's a user!
61
		} else {
62
			$item_nav = bp_nouveau_member_customizer_nav();
63
		}
64
		?>
65
66
		<?php if ( isset( $guide ) ) : ?>
67
			<p class="description">
68
				<?php echo esc_html( $guide ); ?>
69
			</p>
70
		<?php endif; ?>
71
72
		<?php if ( ! empty( $item_nav ) ) : ?>
73
			<ul id="<?php echo esc_attr( $id ); ?>" class="ui-sortable" style="margin-top: 0px; height: 500px;" data-bp-type="<?php echo esc_attr( $this->type ); ?>">
74
75
				<?php
76
				$i = 0;
77
				foreach ( $item_nav as $item ) :
0 ignored issues
show
Bug introduced by Hugo Ashmore
The expression $item_nav of type boolean|array is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
78
					$i += 1;
0 ignored issues
show
Coding Style introduced by Hugo Ashmore
Increment operators should be used where possible; found "$i += 1;" but expected "$i++"
Loading history...
79
				?>
80
					<li class="<?php echo esc_attr( $class ); echo ( 1 === $i ) ? ' ui-sortable-disabled' : ''; ?>" data-bp-nav="<?php echo esc_attr( $item->slug ); ?>">
0 ignored issues
show
Coding Style introduced by Hugo Ashmore
It is generally recommended to place each PHP statement on a line by itself.

Let’s take a look at an example:

// Bad
$a = 5; $b = 6; $c = 7;

// Good
$a = 5;
$b = 6;
$c = 7;
Loading history...
81
						<div class="menu-item-bar">
82
							<div class="menu-item-handle ui-sortable-handle">
83
								<span class="item-title" aria-hidden="true">
84
									<span class="menu-item-title"><?php echo esc_html( _bp_strip_spans_from_title( $item->name ) ); ?></span>
85
								</span>
86
							</div>
87
						</div>
88
					</li>
89
				<?php endforeach; ?>
90
91
			</ul>
92
		<?php endif; ?>
93
94
			<input id="<?php echo esc_attr( 'bp_item_' . $this->type ); ?>" type="hidden" value="" data-customize-setting-link="<?php echo esc_attr( $setting ); ?>" />
95
96
		<?php
97
	}
98
}
99