CMB2_Field_Display   B
last analyzed

Complexity

Total Complexity 32

Size/Duplication

Total Lines 139
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 17

Test Coverage

Coverage 89.29%

Importance

Changes 0
Metric Value
dl 0
loc 139
ccs 75
cts 84
cp 0.8929
rs 7.5428
c 0
b 0
f 0
wmc 32
lcom 1
cbo 17

4 Methods

Rating   Name   Duplication   Size   Complexity  
C get() 0 66 26
A __construct() 0 4 1
A _display() 0 3 1
A display() 0 20 4
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
	 *
18
	 * @var   CMB2_Field object
19
	 * @since 2.2.2
20
	 */
21
	public $field;
22
23
	/**
24
	 * The CMB field object's value.
25
	 *
26
	 * @var   mixed
27
	 * @since 2.2.2
28
	 */
29
	public $value;
30
31
	/**
32
	 * Get the corresponding display class for the field type.
33
	 *
34
	 * @since  2.2.2
35
	 * @param  CMB2_Field $field
36
	 * @return CMB2_Field_Display
37
	 */
38 33
	public static function get( CMB2_Field $field ) {
39 33
		switch ( $field->type() ) {
40 33
			case 'text_url':
41 1
				$type = new CMB2_Display_Text_Url( $field );
42 1
				break;
43 32
			case 'text_money':
44 1
				$type = new CMB2_Display_Text_Money( $field );
45 1
				break;
46 31
			case 'colorpicker':
47 1
				$type = new CMB2_Display_Colorpicker( $field );
48 3
				break;
49 30
			case 'checkbox':
50 1
				$type = new CMB2_Display_Checkbox( $field );
51 1
				break;
52 29
			case 'wysiwyg':
53 29
			case 'textarea_small':
54 3
				$type = new CMB2_Display_Textarea( $field );
55 3
				break;
56 26
			case 'textarea_code':
57 1
				$type = new CMB2_Display_Textarea_Code( $field );
58 1
				break;
59 25
			case 'text_time':
60 1
				$type = new CMB2_Display_Text_Time( $field );
61 1
				break;
62 24
			case 'text_date':
63 24
			case 'text_date_timestamp':
64 24
			case 'text_datetime_timestamp':
65 3
				$type = new CMB2_Display_Text_Date( $field );
66 3
				break;
67 21
			case 'text_datetime_timestamp_timezone':
68 1
				$type = new CMB2_Display_Text_Date_Timezone( $field );
69 1
				break;
70 20
			case 'select':
71 20
			case 'radio':
72 20
			case 'radio_inline':
73 3
				$type = new CMB2_Display_Select( $field );
74 3
				break;
75 17
			case 'multicheck':
76 17
			case 'multicheck_inline':
77 2
				$type = new CMB2_Display_Multicheck( $field );
78 2
				break;
79 15
			case 'taxonomy_radio':
80 15
			case 'taxonomy_radio_inline':
81 15
			case 'taxonomy_select':
82 3
				$type = new CMB2_Display_Taxonomy_Radio( $field );
83 3
				break;
84 12
			case 'taxonomy_multicheck':
85 12
			case 'taxonomy_multicheck_inline':
86 2
				$type = new CMB2_Display_Taxonomy_Multicheck( $field );
87 2
				break;
88 10
			case 'file':
89 1
				$type = new CMB2_Display_File( $field );
90 1
				break;
91 9
			case 'file_list':
92 1
				$type = new CMB2_Display_File_List( $field );
93 1
				break;
94 8
			case 'oembed':
95 1
				$type = new CMB2_Display_oEmbed( $field );
96 1
				break;
97 7
			default:
98 7
				$type = new self( $field );
99 7
				break;
100 33
		}// End switch().
0 ignored issues
show
Unused Code Comprehensibility introduced by
43% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

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