Completed
Push — trunk ( 5d123b...5dfd19 )
by Justin
06:53
created

CMB2_Display_Select   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 21
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 69.23%

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 21
rs 10
ccs 9
cts 13
cp 0.6923
wmc 6
lcom 1
cbo 2

1 Method

Rating   Name   Duplication   Size   Complexity  
B _display() 0 15 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
	 * @param  string $method    Non-existent method name
0 ignored issues
show
Bug introduced by
There is no parameter named $method. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
117
	 * @param  array  $arguments All arguments passed to the method
0 ignored issues
show
Bug introduced by
There is no parameter named $arguments. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
118
	 */
119 33
	public function display() {
120
		// If repeatable
121 33
		if ( $this->field->args( 'repeatable' ) ) {
122
123
			// And has a repeatable value
124
			if ( is_array( $this->field->value ) ) {
125
126
				// Then loop and output.
127
				echo '<ul class="cmb2-'. str_replace( '_', '-', $this->field->type() ) .'">';
128
				foreach ( $this->field->value as $val ) {
129
					$this->value = $val;
130
					echo '<li>', $this->_display(), '</li>';
131
					;
132
				}
133
				echo '</ul>';
134
			}
135
136
		} else {
137 33
			$this->_display();
138
		}
139 33
	}
140
141
	/**
142
	 * Default fallback display method.
143
	 * @since 2.2.2
144
	 */
145 7
	protected function _display() {
146 7
		print_r( $this->value );
147 7
	}
148
}
149
150
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...
151
	/**
152
	 * Display url value.
153
	 * @since 2.2.2
154
	 */
155 1
	protected function _display() {
156 1
		echo make_clickable( esc_url( $this->value ) );
157 1
	}
158
}
159
160
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...
161
	/**
162
	 * Display text_money value.
163
	 * @since 2.2.2
164
	 */
165 1
	protected function _display() {
166 1
		$this->value = $this->value ? $this->value : '0';
167 1
		echo ( ! $this->field->get_param_callback_result( 'before_field' ) ? '$' : ' ' ), $this->value;
168 1
	}
169
}
170
171
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...
172
	/**
173
	 * Display color picker value.
174
	 * @since 2.2.2
175
	 */
176 1
	protected function _display() {
177 1
		echo '<span class="cmb2-colorpicker-swatch"><span style="background-color:', esc_attr( $this->value ), '"></span> ', esc_html( $this->value ), '</span>';
178 1
	}
179
}
180
181
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...
182
	/**
183
	 * Display multicheck value.
184
	 * @since 2.2.2
185
	 */
186 1
	protected function _display() {
187 1
		echo $this->value === 'on' ? 'on' : 'off';
188 1
	}
189
}
190
191
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...
192
	/**
193
	 * Display select value.
194
	 * @since 2.2.2
195
	 */
196 3
	protected function _display() {
197 3
		$options = $this->field->options();
198
199 3
		$fallback = $this->field->args( 'show_option_none' );
200 3
		if ( ! $fallback && isset( $options[''] ) ) {
201
			$fallback = $options[''];
202
		}
203 3
		if ( ! $this->value && $fallback ) {
204
			echo $fallback;
205 3
		} elseif ( isset( $options[ $this->value ] ) ) {
206 3
			echo $options[ $this->value ];
207 3
		} else {
208
			echo esc_attr( $this->value );
209
		}
210 3
	}
211
}
212
213
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...
214
	/**
215
	 * Display multicheck value.
216
	 * @since 2.2.2
217
	 */
218 2
	protected function _display() {
219 2
		if ( empty( $this->value ) || ! is_array( $this->value ) ) {
220
			return;
221
		}
222
223 2
		$options = $this->field->options();
224
225 2
		$output = array();
226 2
		foreach ( $this->value as $val ) {
227 2
			if ( isset( $options[ $val ] ) ) {
228 2
				$output[] = $options[ $val ];
229 2
			} else {
230
				$output[] = esc_attr( $val );
231
			}
232 2
		}
233
234 2
		echo implode( ', ', $output );
235 2
	}
236
}
237
238
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...
239
	/**
240
	 * Display textarea value.
241
	 * @since 2.2.2
242
	 */
243 3
	protected function _display() {
244 3
		echo wpautop( wp_kses_post( $this->value ) );
245 3
	}
246
}
247
248
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...
249
	/**
250
	 * Display textarea_code value.
251
	 * @since 2.2.2
252
	 */
253 1
	protected function _display() {
254 1
		echo '<xmp class="cmb2-code">'. print_r( $this->value, true ) .'</xmp>';
255 1
	}
256
}
257
258
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...
259
	/**
260
	 * Display text_time value.
261
	 * @since 2.2.2
262
	 */
263 1
	protected function _display() {
264 1
		echo $this->field->get_timestamp_format( 'time_format', $this->value );
265 1
	}
266
}
267
268
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...
269
	/**
270
	 * Display text_date value.
271
	 * @since 2.2.2
272
	 */
273 3
	protected function _display() {
274 3
		echo $this->field->get_timestamp_format( 'date_format', $this->value );
275 3
	}
276
}
277
278
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...
279
	/**
280
	 * Display text_datetime_timestamp_timezone value.
281
	 * @since 2.2.2
282
	 */
283 1
	protected function _display() {
284 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...
285
286 1
		if ( empty( $this->value ) ) {
287
			return;
288
		}
289
290 1
		$datetime = maybe_unserialize( $this->value );
291 1
		$this->value = $tzstring = '';
292
293 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...
294 1
			$tz       = $datetime->getTimezone();
295 1
			$tzstring = $tz->getName();
296 1
			$this->value    = $datetime->getTimestamp();
297 1
		}
298
299 1
		$date = $this->field->get_timestamp_format( 'date_format', $this->value );
300 1
		$time = $this->field->get_timestamp_format( 'time_format', $this->value );
301
302 1
		echo $date, ( $time ? ' ' . $time : '' ), ( $tzstring ? ', ' . $tzstring : '' );
303 1
	}
304
}
305
306
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...
307
	/**
308
	 * Display single taxonomy value.
309
	 * @since 2.2.2
310
	 */
311 3
	protected function _display() {
312 3
		$taxonomy   = $this->field->args( 'taxonomy' );
313 3
		$field_type = new CMB2_Type_Taxonomy_Radio( new CMB2_Types( $this->field ) );
314 3
		$terms      = $field_type->get_object_terms();
315 3
		$term       = false;
316
317 3
		if ( is_wp_error( $terms ) || empty( $terms ) && ( $default = $this->field->get_default() ) ) {
318
			$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...
319 3
		} elseif ( ! empty( $terms ) ) {
320 3
			$term = $terms[key( $terms )];
321 3
		}
322
323 3
		if ( $term ) {
324 3
			$link = get_edit_term_link( $term->term_id, $taxonomy );
325 3
			echo '<a href="', esc_url( $link ), '">', esc_html( $term->name ), '</a>';
326 3
		}
327 3
	}
328
}
329
330
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...
331
	/**
332
	 * Display taxonomy values.
333
	 * @since 2.2.2
334
	 */
335 2
	protected function _display() {
336 2
		$taxonomy   = $this->field->args( 'taxonomy' );
337 2
		$field_type = new CMB2_Type_Taxonomy_Multicheck( new CMB2_Types( $this->field ) );
338 2
		$terms      = $field_type->get_object_terms();
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
		$field_type = new CMB2_Type_File_Base( new CMB2_Types( $this->field ) );
379
380 1
		$id = $this->field->get_field_clone( array(
381 1
			'id' => $field_type->_id() . '_id',
0 ignored issues
show
Documentation Bug introduced by
The method _id 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...
382 1
		) )->escaped_value( 'absint' );
383
384 1
		$this->file_output( $this->value, $id, $field_type );
385 1
	}
386
387 2
	protected function file_output( $url_value, $id, CMB2_Type_File_Base $field_type ) {
388
		// If there is no ID saved yet, try to get it from the url
389 2
		if ( $url_value && ! $id ) {
390
			$id = cmb2_utils()->image_id_from_url( esc_url_raw( $url_value ) );
391
		}
392
393 2
		if ( $field_type->is_valid_img_ext( $url_value ) ) {
394
			$img_size = $this->field->args( 'preview_size' );
395
396 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...
397
				$image = wp_get_attachment_image( $id, $img_size, null, array( 'class' => 'cmb-image-display' ) );
398
			} else {
399
				$size = is_array( $img_size ) ? $img_size[0] : 200;
400
				$image = '<img class="cmb-image-display" style="max-width: ' . absint( $size ) . 'px; width: 100%; height: auto;" src="' . $url_value . '" alt="" />';
401
			}
402
403
			echo $image;
404
405
		} else {
406
407 2
			printf( '<div class="file-status"><span>%1$s <strong><a href="%2$s">%3$s</a></strong></span></div>',
408 2
				esc_html( $field_type->_text( 'file_text', __( '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...
409 2
				$url_value,
410 2
				cmb2_utils()->get_file_name_from_path( $url_value )
411 2
			);
412
413
		}
414 2
	}
415
}
416
417
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...
418
	/**
419
	 * Display file_list value.
420
	 * @since 2.2.2
421
	 */
422 1
	protected function _display() {
423 1
		if ( empty( $this->value ) || ! is_array( $this->value ) ) {
424
			return;
425
		}
426
427 1
		$field_type = new CMB2_Type_File_Base( new CMB2_Types( $this->field ) );
428
429 1
		echo '<ul class="cmb2-display-file-list">';
430 1
		foreach ( $this->value as $id => $fullurl ) {
431 1
			echo '<li>', $this->file_output( esc_url_raw( $fullurl ), $id, $field_type ), '</li>';
432 1
		}
433 1
		echo '</ul>';
434 1
	}
435
}
436
437
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...
438
	/**
439
	 * Display oembed value.
440
	 * @since 2.2.2
441
	 */
442 1
	protected function _display() {
443 1
		if ( ! $this->value ) {
444
			return;
445
		}
446
447 1
		cmb2_do_oembed( array(
448 1
			'url'         => $this->value,
449 1
			'object_id'   => $this->field->object_id,
450 1
			'object_type' => $this->field->object_type,
451 1
			'oembed_args' => array( 'width' => '300' ),
452 1
			'field_id'    => $this->field->id(),
453 1
		) );
454 1
	}
455
}
456