Completed
Push — trunk ( 3ba4de...9f13f9 )
by Justin
06:32
created

CMB2_Display_Taxonomy_Radio   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 24
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 93.75%

Importance

Changes 0
Metric Value
dl 0
loc 24
ccs 15
cts 16
cp 0.9375
rs 10
c 0
b 0
f 0
wmc 6
lcom 1
cbo 3

1 Method

Rating   Name   Duplication   Size   Complexity  
B _display() 0 18 6
1
<?php
2
/**
3
 * CMB2 field display base.
4
 *
5
 * @since 2.2.2
6
 *
7
 * @category  WordPress_Plugin
8
 * @package   CMB2
9
 * @author    WebDevStudios
10
 * @license   GPL-2.0+
11
 * @link      http://webdevstudios.com
12
 */
13
class CMB2_Field_Display {
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
14
15
	/**
16
	 * A CMB field object
17
	 * @var   CMB2_Field object
18
	 * @since 2.2.2
19
	 */
20
	public $field;
21
22
	/**
23
	 * The CMB field object's value.
24
	 * @var   mixed
25
	 * @since 2.2.2
26
	 */
27
	public $value;
28
29
	/**
30
	 * Get the corresponding display class for the field type.
31
	 * @since  2.2.2
32
	 * @param  CMB2_Field $field
33
	 * @return CMB2_Field_Display
34
	 */
35 33
	public static function get( CMB2_Field $field ) {
36 33
		switch ( $field->type() ) {
37 33
			case 'text_url':
38 1
				$type = new CMB2_Display_Text_Url( $field );
39 1
				break;
40 32
			case 'text_money':
41 1
				$type = new CMB2_Display_Text_Money( $field );
42 1
				break;
43 31
			case 'colorpicker':
44 1
				$type = new CMB2_Display_Colorpicker( $field );
45 1
				break;
46 30
			case 'checkbox':
47 3
				$type = new CMB2_Display_Checkbox( $field );
48 1
				break;
49 29
			case 'wysiwyg':
50 29
			case 'textarea_small':
51 3
				$type = new CMB2_Display_Textarea( $field );
52 3
				break;
53 26
			case 'textarea_code':
54 1
				$type = new CMB2_Display_Textarea_Code( $field );
55 1
				break;
56 25
			case 'text_time':
57 1
				$type = new CMB2_Display_Text_Time( $field );
58 1
				break;
59 24
			case 'text_date':
60 24
			case 'text_date_timestamp':
61 24
			case 'text_datetime_timestamp':
62 3
				$type = new CMB2_Display_Text_Date( $field );
63 3
				break;
64 21
			case 'text_datetime_timestamp_timezone':
65 1
				$type = new CMB2_Display_Text_Date_Timezone( $field );
66 1
				break;
67 20
			case 'select':
68 20
			case 'radio':
69 20
			case 'radio_inline':
70 3
				$type = new CMB2_Display_Select( $field );
71 3
				break;
72 17
			case 'multicheck':
73 17
			case 'multicheck_inline':
74 2
				$type = new CMB2_Display_Multicheck( $field );
75 2
				break;
76 15
			case 'taxonomy_radio':
77 15
			case 'taxonomy_radio_inline':
78 15
			case 'taxonomy_select':
79 3
				$type = new CMB2_Display_Taxonomy_Radio( $field );
80 3
				break;
81 12
			case 'taxonomy_multicheck':
82 12
			case 'taxonomy_multicheck_inline':
83 2
				$type = new CMB2_Display_Taxonomy_Multicheck( $field );
84 2
				break;
85 10
			case 'file':
86 1
				$type = new CMB2_Display_File( $field );
87 1
				break;
88 9
			case 'file_list':
89 1
				$type = new CMB2_Display_File_List( $field );
90 1
				break;
91 8
			case 'oembed':
92 1
				$type = new CMB2_Display_oEmbed( $field );
93 1
				break;
94 7
			default:
95 7
				$type = new self( $field );
96 7
				break;
97 33
		}
98
99 33
		return $type;
100
	}
101
102
	/**
103
	 * Setup our class vars
104
	 * @since 2.2.2
105
	 * @param CMB2_Field $field A CMB2 field object
106
	 */
107 33
	public function __construct( CMB2_Field $field ) {
108 33
		$this->field = $field;
109 33
		$this->value = $this->field->value;
110 33
	}
111
112
	/**
113
	 * Catchall method if field's 'display_cb' is NOT defined, or field type does
114
	 * not have a corresponding display method
115
	 * @since 2.2.2
116
	 */
117 33
	public function display() {
118
		// If repeatable
119 33
		if ( $this->field->args( 'repeatable' ) ) {
120
121
			// And has a repeatable value
122
			if ( is_array( $this->field->value ) ) {
123
124
				// Then loop and output.
125
				echo '<ul class="cmb2-'. str_replace( '_', '-', $this->field->type() ) .'">';
126
				foreach ( $this->field->value as $val ) {
127
					$this->value = $val;
128
					echo '<li>', $this->_display(), '</li>';
129
					;
130
				}
131
				echo '</ul>';
132
			}
133
134
		} else {
135 33
			$this->_display();
136
		}
137 33
	}
138
139
	/**
140
	 * Default fallback display method.
141
	 * @since 2.2.2
142
	 */
143 7
	protected function _display() {
144 7
		print_r( $this->value );
145 7
	}
146
}
147
148
class CMB2_Display_Text_Url extends CMB2_Field_Display {
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class should be in its own file to aid autoloaders.

Having each class in a dedicated file usually plays nice with PSR autoloaders and is therefore a well established practice. If you use other autoloaders, you might not want to follow this rule.

Loading history...
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
149
	/**
150
	 * Display url value.
151
	 * @since 2.2.2
152
	 */
153 1
	protected function _display() {
154 1
		echo make_clickable( esc_url( $this->value ) );
155 1
	}
156
}
157
158
class CMB2_Display_Text_Money extends CMB2_Field_Display {
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class should be in its own file to aid autoloaders.

Having each class in a dedicated file usually plays nice with PSR autoloaders and is therefore a well established practice. If you use other autoloaders, you might not want to follow this rule.

Loading history...
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
159
	/**
160
	 * Display text_money value.
161
	 * @since 2.2.2
162
	 */
163 1
	protected function _display() {
164 1
		$this->value = $this->value ? $this->value : '0';
165 1
		echo ( ! $this->field->get_param_callback_result( 'before_field' ) ? '$' : ' ' ), $this->value;
166 1
	}
167
}
168
169
class CMB2_Display_Colorpicker extends CMB2_Field_Display {
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class should be in its own file to aid autoloaders.

Having each class in a dedicated file usually plays nice with PSR autoloaders and is therefore a well established practice. If you use other autoloaders, you might not want to follow this rule.

Loading history...
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
170
	/**
171
	 * Display color picker value.
172
	 * @since 2.2.2
173
	 */
174 1
	protected function _display() {
175 1
		echo '<span class="cmb2-colorpicker-swatch"><span style="background-color:', esc_attr( $this->value ), '"></span> ', esc_html( $this->value ), '</span>';
176 1
	}
177
}
178
179
class CMB2_Display_Checkbox extends CMB2_Field_Display {
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class should be in its own file to aid autoloaders.

Having each class in a dedicated file usually plays nice with PSR autoloaders and is therefore a well established practice. If you use other autoloaders, you might not want to follow this rule.

Loading history...
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
180
	/**
181
	 * Display multicheck value.
182
	 * @since 2.2.2
183
	 */
184 1
	protected function _display() {
185 1
		echo $this->value === 'on' ? 'on' : 'off';
186 1
	}
187
}
188
189
class CMB2_Display_Select extends CMB2_Field_Display {
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class should be in its own file to aid autoloaders.

Having each class in a dedicated file usually plays nice with PSR autoloaders and is therefore a well established practice. If you use other autoloaders, you might not want to follow this rule.

Loading history...
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
190
	/**
191
	 * Display select value.
192
	 * @since 2.2.2
193
	 */
194 3
	protected function _display() {
195 3
		$options = $this->field->options();
196
197 3
		$fallback = $this->field->args( 'show_option_none' );
198 3
		if ( ! $fallback && isset( $options[''] ) ) {
199
			$fallback = $options[''];
200
		}
201 3
		if ( ! $this->value && $fallback ) {
202
			echo $fallback;
203 3
		} elseif ( isset( $options[ $this->value ] ) ) {
204 3
			echo $options[ $this->value ];
205 3
		} else {
206
			echo esc_attr( $this->value );
207
		}
208 3
	}
209
}
210
211
class CMB2_Display_Multicheck extends CMB2_Field_Display {
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class should be in its own file to aid autoloaders.

Having each class in a dedicated file usually plays nice with PSR autoloaders and is therefore a well established practice. If you use other autoloaders, you might not want to follow this rule.

Loading history...
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
212
	/**
213
	 * Display multicheck value.
214
	 * @since 2.2.2
215
	 */
216 2
	protected function _display() {
217 2
		if ( empty( $this->value ) || ! is_array( $this->value ) ) {
218
			return;
219
		}
220
221 2
		$options = $this->field->options();
222
223 2
		$output = array();
224 2
		foreach ( $this->value as $val ) {
225 2
			if ( isset( $options[ $val ] ) ) {
226 2
				$output[] = $options[ $val ];
227 2
			} else {
228
				$output[] = esc_attr( $val );
229
			}
230 2
		}
231
232 2
		echo implode( ', ', $output );
233 2
	}
234
}
235
236
class CMB2_Display_Textarea extends CMB2_Field_Display {
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class should be in its own file to aid autoloaders.

Having each class in a dedicated file usually plays nice with PSR autoloaders and is therefore a well established practice. If you use other autoloaders, you might not want to follow this rule.

Loading history...
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
237
	/**
238
	 * Display textarea value.
239
	 * @since 2.2.2
240
	 */
241 3
	protected function _display() {
242 3
		echo wpautop( wp_kses_post( $this->value ) );
243 3
	}
244
}
245
246
class CMB2_Display_Textarea_Code extends CMB2_Field_Display {
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class should be in its own file to aid autoloaders.

Having each class in a dedicated file usually plays nice with PSR autoloaders and is therefore a well established practice. If you use other autoloaders, you might not want to follow this rule.

Loading history...
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
247
	/**
248
	 * Display textarea_code value.
249
	 * @since 2.2.2
250
	 */
251 1
	protected function _display() {
252 1
		echo '<xmp class="cmb2-code">'. print_r( $this->value, true ) .'</xmp>';
253 1
	}
254
}
255
256
class CMB2_Display_Text_Time extends CMB2_Field_Display {
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class should be in its own file to aid autoloaders.

Having each class in a dedicated file usually plays nice with PSR autoloaders and is therefore a well established practice. If you use other autoloaders, you might not want to follow this rule.

Loading history...
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
257
	/**
258
	 * Display text_time value.
259
	 * @since 2.2.2
260
	 */
261 1
	protected function _display() {
262 1
		echo $this->field->get_timestamp_format( 'time_format', $this->value );
263 1
	}
264
}
265
266
class CMB2_Display_Text_Date extends CMB2_Field_Display {
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class should be in its own file to aid autoloaders.

Having each class in a dedicated file usually plays nice with PSR autoloaders and is therefore a well established practice. If you use other autoloaders, you might not want to follow this rule.

Loading history...
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
267
	/**
268
	 * Display text_date value.
269
	 * @since 2.2.2
270
	 */
271 3
	protected function _display() {
272 3
		echo $this->field->get_timestamp_format( 'date_format', $this->value );
273 3
	}
274
}
275
276
class CMB2_Display_Text_Date_Timezone extends CMB2_Field_Display {
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class should be in its own file to aid autoloaders.

Having each class in a dedicated file usually plays nice with PSR autoloaders and is therefore a well established practice. If you use other autoloaders, you might not want to follow this rule.

Loading history...
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
277
	/**
278
	 * Display text_datetime_timestamp_timezone value.
279
	 * @since 2.2.2
280
	 */
281 1
	protected function _display() {
282 1
		$field = $this->field;
0 ignored issues
show
Unused Code introduced by
$field is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
283
284 1
		if ( empty( $this->value ) ) {
285
			return;
286
		}
287
288 1
		$datetime = maybe_unserialize( $this->value );
289 1
		$this->value = $tzstring = '';
290
291 1 View Code Duplication
		if ( $datetime && $datetime instanceof DateTime ) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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.

Loading history...
292 1
			$tz       = $datetime->getTimezone();
293 1
			$tzstring = $tz->getName();
294 1
			$this->value    = $datetime->getTimestamp();
295 1
		}
296
297 1
		$date = $this->field->get_timestamp_format( 'date_format', $this->value );
298 1
		$time = $this->field->get_timestamp_format( 'time_format', $this->value );
299
300 1
		echo $date, ( $time ? ' ' . $time : '' ), ( $tzstring ? ', ' . $tzstring : '' );
301 1
	}
302
}
303
304
class CMB2_Display_Taxonomy_Radio extends CMB2_Field_Display {
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class should be in its own file to aid autoloaders.

Having each class in a dedicated file usually plays nice with PSR autoloaders and is therefore a well established practice. If you use other autoloaders, you might not want to follow this rule.

Loading history...
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
305
	/**
306
	 * Display single taxonomy value.
307
	 * @since 2.2.2
308
	 */
309 3
	protected function _display() {
310 3
		$taxonomy = $this->field->args( 'taxonomy' );
311 3
		$types    = new CMB2_Types( $this->field );
312 3
		$type     = $types->get_new_render_type( 'CMB2_Type_Taxonomy_Radio' );
313 3
		$terms    = $type->get_object_terms();
0 ignored issues
show
Documentation Bug introduced by
The method get_object_terms does not exist on object<CMB2_Type_Base>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
314 3
		$term     = false;
315
316 3
		if ( is_wp_error( $terms ) || empty( $terms ) && ( $default = $this->field->get_default() ) ) {
317
			$term = get_term_by( 'slug', $default, $taxonomy );
0 ignored issues
show
Bug introduced by
The variable $default does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
318 3
		} elseif ( ! empty( $terms ) ) {
319 3
			$term = $terms[key( $terms )];
320 3
		}
321
322 3
		if ( $term ) {
323 3
			$link = get_edit_term_link( $term->term_id, $taxonomy );
324 3
			echo '<a href="', esc_url( $link ), '">', esc_html( $term->name ), '</a>';
325 3
		}
326 3
	}
327
}
328
329
class CMB2_Display_Taxonomy_Multicheck extends CMB2_Field_Display {
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class should be in its own file to aid autoloaders.

Having each class in a dedicated file usually plays nice with PSR autoloaders and is therefore a well established practice. If you use other autoloaders, you might not want to follow this rule.

Loading history...
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
330
	/**
331
	 * Display taxonomy values.
332
	 * @since 2.2.2
333
	 */
334 2
	protected function _display() {
335 2
		$taxonomy = $this->field->args( 'taxonomy' );
336 2
		$types    = new CMB2_Types( $this->field );
337 2
		$type     = $types->get_new_render_type( 'CMB2_Type_Taxonomy_Multicheck' );
338 2
		$terms    = $type->get_object_terms();
0 ignored issues
show
Documentation Bug introduced by
The method get_object_terms does not exist on object<CMB2_Type_Base>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
339
340 2
		if ( is_wp_error( $terms ) || empty( $terms ) && ( $default = $this->field->get_default() ) ) {
341
			$terms = array();
342
			if ( is_array( $default ) ) {
343
				foreach ( $default as $slug ) {
0 ignored issues
show
Bug introduced by
The variable $default does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
344
					$terms[] = get_term_by( 'slug', $slug, $taxonomy );
345
				}
346
			} else {
347
				$terms[] = get_term_by( 'slug', $default, $taxonomy );
348
			}
349
		}
350
351 2
		if ( is_array( $terms ) ) {
352
353 2
			$links = array();
354 2
			foreach ( $terms as $term ) {
355 2
				$link = get_edit_term_link( $term->term_id, $taxonomy );
356 2
				$links[] = '<a href="'. esc_url( $link ) .'">'. esc_html( $term->name ) .'</a>';
357 2
			}
358
			// Then loop and output.
359 2
			echo '<div class="cmb2-taxonomy-terms-', esc_attr( $taxonomy ), '">';
360 2
			echo implode( ', ', $links );
361 2
			echo '</div>';
362 2
		}
363 2
	}
364
}
365
366
class CMB2_Display_File extends CMB2_Field_Display {
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class should be in its own file to aid autoloaders.

Having each class in a dedicated file usually plays nice with PSR autoloaders and is therefore a well established practice. If you use other autoloaders, you might not want to follow this rule.

Loading history...
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
367
	/**
368
	 * Display file value.
369
	 * @since 2.2.2
370
	 */
371 1
	protected function _display() {
372 1
		if ( empty( $this->value ) ) {
373
			return;
374
		}
375
376 1
		$this->value = esc_url_raw( $this->value );
377
378 1
		$types = new CMB2_Types( $this->field );
379 1
		$type  = $types->get_new_render_type( 'CMB2_Type_File_Base' );
380
381 1
		$id = $this->field->get_field_clone( array(
382 1
			'id' => $this->field->_id() . '_id',
383 1
		) )->escaped_value( 'absint' );
384
385 1
		$this->file_output( $this->value, $id, $type );
0 ignored issues
show
Compatibility introduced by
$type of type object<CMB2_Type_Base> is not a sub-type of object<CMB2_Type_File_Base>. It seems like you assume a child class of the class CMB2_Type_Base to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
386 1
	}
387
388 2
	protected function file_output( $url_value, $id, CMB2_Type_File_Base $field_type ) {
389
		// If there is no ID saved yet, try to get it from the url
390 2
		if ( $url_value && ! $id ) {
391
			$id = CMB2_Utils::image_id_from_url( esc_url_raw( $url_value ) );
392
		}
393
394 2
		if ( $field_type->is_valid_img_ext( $url_value ) ) {
395
			$img_size = $this->field->args( 'preview_size' );
396
397 View Code Duplication
			if ( $id ) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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.

Loading history...
398
				$image = wp_get_attachment_image( $id, $img_size, null, array( 'class' => 'cmb-image-display' ) );
399
			} else {
400
				$size = is_array( $img_size ) ? $img_size[0] : 200;
401
				$image = '<img class="cmb-image-display" style="max-width: ' . absint( $size ) . 'px; width: 100%; height: auto;" src="' . $url_value . '" alt="" />';
402
			}
403
404
			echo $image;
405
406
		} else {
407
408 2
			printf( '<div class="file-status"><span>%1$s <strong><a href="%2$s">%3$s</a></strong></span></div>',
409 2
				esc_html( $field_type->_text( 'file_text', esc_html__( 'File:', 'cmb2' ) ) ),
0 ignored issues
show
Documentation Bug introduced by
The method _text does not exist on object<CMB2_Type_File_Base>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
410 2
				$url_value,
411 2
				CMB2_Utils::get_file_name_from_path( $url_value )
412 2
			);
413
414
		}
415 2
	}
416
}
417
418
class CMB2_Display_File_List extends CMB2_Display_File {
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class should be in its own file to aid autoloaders.

Having each class in a dedicated file usually plays nice with PSR autoloaders and is therefore a well established practice. If you use other autoloaders, you might not want to follow this rule.

Loading history...
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
419
	/**
420
	 * Display file_list value.
421
	 * @since 2.2.2
422
	 */
423 1
	protected function _display() {
424 1
		if ( empty( $this->value ) || ! is_array( $this->value ) ) {
425
			return;
426
		}
427
428 1
		$types = new CMB2_Types( $this->field );
429 1
		$type  = $types->get_new_render_type( 'CMB2_Type_File_Base' );
430
431 1
		echo '<ul class="cmb2-display-file-list">';
432 1
		foreach ( $this->value as $id => $fullurl ) {
433 1
			echo '<li>', $this->file_output( esc_url_raw( $fullurl ), $id, $type ), '</li>';
0 ignored issues
show
Compatibility introduced by
$type of type object<CMB2_Type_Base> is not a sub-type of object<CMB2_Type_File_Base>. It seems like you assume a child class of the class CMB2_Type_Base to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
434 1
		}
435 1
		echo '</ul>';
436 1
	}
437
}
438
439
class CMB2_Display_oEmbed extends CMB2_Field_Display {
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class should be in its own file to aid autoloaders.

Having each class in a dedicated file usually plays nice with PSR autoloaders and is therefore a well established practice. If you use other autoloaders, you might not want to follow this rule.

Loading history...
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
440
	/**
441
	 * Display oembed value.
442
	 * @since 2.2.2
443
	 */
444 1
	protected function _display() {
445 1
		if ( ! $this->value ) {
446
			return;
447
		}
448
449 1
		cmb2_do_oembed( array(
450 1
			'url'         => $this->value,
451 1
			'object_id'   => $this->field->object_id,
0 ignored issues
show
Documentation introduced by
The property $object_id is declared protected in CMB2_Base. Since you implemented __get(), maybe consider adding a @property or @property-read annotation. This makes it easier for IDEs to provide auto-completion.

Since your code implements the magic setter _set, this function will be called for any write 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.");
        }
    }

}

Since the property has write access only, you can use the @property-write 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...
452 1
			'object_type' => $this->field->object_type,
0 ignored issues
show
Documentation introduced by
The property $object_type is declared protected in CMB2_Base. Since you implemented __get(), maybe consider adding a @property or @property-read annotation. This makes it easier for IDEs to provide auto-completion.

Since your code implements the magic setter _set, this function will be called for any write 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.");
        }
    }

}

Since the property has write access only, you can use the @property-write 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...
453 1
			'oembed_args' => array( 'width' => '300' ),
454 1
			'field_id'    => $this->field->id(),
455 1
		) );
456 1
	}
457
}
458