Completed
Push — master ( 5586ba...13831d )
by
unknown
02:56
created

helpers.php ➔ lasso_user_can()   C

Complexity

Conditions 8
Paths 16

Size

Total Lines 30
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 8
eloc 18
nc 16
nop 2
dl 0
loc 30
rs 5.3846
c 0
b 0
f 0
1
<?php
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 15 and the first side effect is on line 402.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

Loading history...
2
3
/**
4
 * Grab an option from our settings
5
 *
6
 * If we're on multsite we'll grab the site option which is stored in the main blogs site option tables, otherwise
7
 * we'll grab the option which is stored on the single blogs option tables
8
 *
9
 * @param unknown $option  string name of the option
10
 * @param unknown $section string name of the section
11
 * @param unknown $default string/int default option value
12
 * @return the option value
13
 * @since 1.0
14
 */
15
function lasso_editor_get_option( $option, $section, $default = '' ) {
16
17
	if ( empty( $option ) )
18
		return;
19
20
	if ( function_exists( 'is_multisite' ) && is_multisite() ) {
21
22
		$options = get_site_option( $section );
23
24
	} else {
25
26
		$options = get_option( $section );
27
	}
28
29
	if ( isset( $options[$option] ) ) {
30
		return $options[$option];
31
	}
32
33
	return $default;
34
}
35
36
/**
37
 * Check to see if any Lasso galleries exist
38
 *
39
 * @since 1.0
40
 */
41
function lasso_editor_galleries_exist() {
42
43
	$q = new wp_query( array( 'post_type' => 'ai_galleries', 'post_status' => 'publish' ) );
44
45
	if ( $q->have_posts() )
46
		return true;
47
	else
48
		return false;
49
}
50
51
/**
52
 * Return a CSS class of an automatically supported theme
53
 *
54
 * @since 0.8.6
55
 * @return a css class if the theme is supported, false if nothing
56
 */
57
function lasso_get_supported_theme_class() {
58
59
	$name  	= wp_get_theme()->get('Name');
60
	$slug  	= lasso_clean_string( $name );
61
62
	switch ( $slug ) {
63
		case 'aesop-story-theme': // aesop
64
			$out = '.aesop-entry-content';
65
			break;
66
		case 'jorgen': // aesop
67
			$out = '.jorgen-entry-content';
68
			break;
69
		case 'novella': // aesop
70
			$out = '.novella-entry-content';
71
			break;
72
		case 'genji': // aesop
73
			$out = '.genji-entry-content';
74
			break;
75
		case 'kerouac': // aesop
76
			$out = '.kerouac-entry-content';
77
			break;
78
		case 'zealot': // aesop
79
			$out = '.zealot-entry-content';
80
			break;
81
		case 'fable': // aesop
82
			$out = '.fable--entry-content';
83
			break;
84
		case 'canvas': // wootheme..err...Automattic
85
			$out = '.entry';
86
			break;
87
		case 'kleo': // 
88
			$out = '.article-content';
89
			break;
90
		//case 'exposure': // 
91
		//	$out = '.entry-content';
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...
92
		//	break;
93
		//case 'lore': // 
94
		//	$out = '.entry-content';
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...
95
		//	break;
96
		//case 'worldview': // upthemes
97
		//	$out = '.entry-content';
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...
98
		//	break;
99
		//case 'genesis': // genesis
100
		//	$out = '.entry-content';
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
		//	break;
102
		//case 'camera': // array.is
103
		//	$out = '.entry-content';
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...
104
		//	break;
105
		case 'longform': 
106
			$out = '.entry-content-wrapper';
107
			break;
108
109
	}
110
111
	return apply_filters('lasso_content_class', !empty( $out ) ? $out : false);
112
	//return !empty( $out ) ? $out : false;
0 ignored issues
show
Unused Code Comprehensibility introduced by
62% 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...
113
}
114
115
function lasso_get_supported_theme_title_class() {
116
117
	$name  	= wp_get_theme()->get('Name');
118
	$slug  	= lasso_clean_string( $name );
119
120
	switch ( $slug ) {
121
122
		case 'aesop-story-theme': // aesop
123
			$out = '.aesop-entry-title';
124
			break;
125
		case 'jorgen': // aesop
126
			$out = '.jorgen-entry-title';
127
			break;
128
		case 'genji': // aesop
129
			$out = '.genji-entry-title';
130
			break;
131
		case 'kerouac': // aesop
132
			$out = '.kerouac-entry-title';
133
			break;
134
		case 'zealot': // aesop
135
			$out = '.zealot-entry-title';
136
			break;
137
		case 'fable': // aesop
138
			$out = '.fable--entry-title';
139
			break;
140
		case 'kleo': // 
141
			$out = '.page-title';
142
			break;
143
		case 'longform': // 
144
			$out = '.entry-title';
145
			break;
146
	}
147
148
	return apply_filters('lasso_title_class', !empty( $out ) ? $out : false);
149
}
150
151
//since 0.9.9.6
152
function lasso_get_supported_theme_featured_image_class() {
153
154
	$name  	= wp_get_theme()->get('Name');
155
	$slug  	= lasso_clean_string( $name );
0 ignored issues
show
Unused Code introduced by
$slug 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...
156
157
	return apply_filters('lasso_featured_image_class', !empty( $out ) ? $out : false);
0 ignored issues
show
Bug introduced by
The variable $out seems to never exist, and therefore empty should always return true. Did you maybe rename this variable?

This check looks for calls to isset(...) or empty() on variables that are yet undefined. These calls will always produce the same result and can be removed.

This is most likely caused by the renaming of a variable or the removal of a function/method parameter.

Loading history...
158
}
159
160
161
/**
162
*	Return a string of classes with items that Lasso will remove when entering the editor
163
*	so that we don't save them as HTML
164
*
165
*	@since 0.8.7
166
*	@return string of comma separated classes
167
*/
168
function lasso_supported_no_save(){
169
170
	return apply_filters('lasso_dont_save', '.lasso--ignore, .sharedaddy, .us_wrapper, .twitter-tweet, .meta, .edit-link, .ssba, .jp-relatedposts, .fb-comments');
171
}
172
173
/**
174
 * Generic sanitization, useful for sanitization of arrays.
175
 *
176
 * @since 0.9.2
177
 *
178
 * @param array|object|string $data Data to sanatize.
179
 *
180
 * @return array|mixed|object|string|void
181
 */
182
function lasso_sanitize_data( $data ) {
183
	return \lasso\sanatize::do_sanitize( $data );
184
185
}
186
187
/**
188
 *	Return a comma delimited list of categories for a specific post object
189
 *
190
 *	@since 0.9.3
191
 *	@return string of comma delimited category slugs
192
*/
193
function lasso_get_post_objects( $postid = '', $taxonomy = 'category') {
194
195
	if ( empty( $postid ) )
196
		$postid = get_the_ID();
197
198
	$objects = 'category' == $taxonomy ? get_the_category( $postid ) : get_the_tags( $postid );
199
200
	if ( empty( $objects) )
201
		return;
202
203
	$out = '';
204
	foreach( $objects as $object ) {
205
		$out .= $object->slug.', ';
206
	}
207
208
	return rtrim($out, ', ');
209
210
}
211
212
/**
213
 *	Return an array of categories for autocomplete
214
 *
215
 *	@since 0.9.3
216
 *	@return array all categoiries
217
*/
218
function lasso_get_objects( $taxonomy = 'category' ) {
219
220
	$objects = 'category' == $taxonomy ? get_categories(array('hide_empty' => 0)) : get_tags(array('hide_empty' => 0));
221
222
	if ( empty( $objects) )
223
		return;
224
225
	$out = array();
226
	foreach( $objects as $object ) {
227
		$out[] = $object->slug;
228
	}
229
230
	return $out;
231
}
232
233
234
/**
235
 * Get allowed post types for the post chooser modal.
236
 *
237
 *
238
 * @since 0.9.4
239
 */
240
function lasso_post_types_names() {
241
	$post_types = get_post_types( array(
242
		'public' => true,
243
	), 'objects' );
244
	$post_types = array_combine( array_keys( $post_types ), wp_list_pluck( $post_types, 'label' ) );
245
    unset( $post_types[ 'attachment' ] );
246
247
	/**
248
	 * Set which post types are allowed
249
	 *
250
	 * @since 0.9.4
251
	 *
252
	 * @param array $allowed_post_types Array of names (not labels) of allowed post types. Must be registered.
253
	 */
254
	$allowed_post_types = lasso_editor_get_option( 'allowed_post_types', 'lasso_editor', array( 'post', 'page') );
0 ignored issues
show
Documentation introduced by
'allowed_post_types' is of type string, but the function expects a object<unknown>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
Documentation introduced by
'lasso_editor' is of type string, but the function expects a object<unknown>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
Documentation introduced by
array('post', 'page') is of type array<integer,string,{"0":"string","1":"string"}>, but the function expects a string|object<unknown>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
255
	$allowed_post_types = apply_filters( 'lasso_allowed_post_types', $allowed_post_types );
256
	foreach( $post_types as $name => $label ) {
257
		if ( ! in_array( $name, $allowed_post_types ) ) {
258
			unset( $post_types[ $name ] );
259
		}
260
	}
261
	return $post_types;
262
}
263
264
265
function lasso_post_types() {
266
	$post_types = get_post_types( array(
267
		'public' => true,
268
	), 'names' );
269
	//$post_types = array_combine( array_keys( $post_types ), wp_list_pluck( $post_types, 'label' ) );
0 ignored issues
show
Unused Code Comprehensibility introduced by
49% 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...
270
    unset( $post_types[ 'attachment' ] );
271
272
	/**
273
	 * Set which post types are allowed
274
	 *
275
	 * @since 0.9.4
276
	 *
277
	 * @param array $allowed_post_types Array of names (not labels) of allowed post types. Must be registered.
278
	 */
279
	$allowed_post_types = lasso_editor_get_option( 'allowed_post_types', 'lasso_editor', array( 'post') );
0 ignored issues
show
Documentation introduced by
'allowed_post_types' is of type string, but the function expects a object<unknown>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
Documentation introduced by
'lasso_editor' is of type string, but the function expects a object<unknown>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
Documentation introduced by
array('post') is of type array<integer,string,{"0":"string"}>, but the function expects a string|object<unknown>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
280
	$allowed_post_types = apply_filters( 'lasso_allowed_post_types', $allowed_post_types );
281
	foreach( $post_types as $name => $label ) {
282
		if ( ! in_array( $name, $allowed_post_types ) ) {
283
			unset( $post_types[ $name ] );
284
		}
285
	}
286
	return $post_types;
287
}
288
289
290
291
////////////////////
292
// INTERNAL
293
////////////////////
294
/**
295
*	Used internally as a callback to build a tab or content area for modal addons
296
*
297
*	@param $tab object
298
*	@param $type string tab or content
299
*	@uses lasso_modal_addons()
300
*	@since 0.9.4
301
*/
302
function lasso_modal_addons_content( $tab = '', $type ){
303
304
	$name = lasso_clean_string( $tab['name'] );
305
306
	if ( 'tab' == $type ) {
307
308
		$out = sprintf( '<li data-addon-name="%s">%s</li>', $name, $tab['name'] );
309
310
	} else if ( 'content' == $type ){
311
312
		$content = isset( $tab['content'] ) && is_callable( $tab['content'] ) ? call_user_func( $tab['content'] ) : false;
313
		$options = isset( $tab['options'] ) && is_callable( $tab['options'] ) ? call_user_func( $tab['options'] ) : false;
314
315
		$out = sprintf( '<div class="lasso--modal__content not-visible" data-addon-content="%s">
316
			%s%s
317
			</div>', $name, $content, lasso_option_form( $name, $options ) );
318
319
	}
320
321
	return $out;
0 ignored issues
show
Bug introduced by
The variable $out 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...
322
}
323
324
/**
325
*	Helper function to clean a string and replace spaces with dash
326
*
327
*	@param $string string content
328
*	@since 0.9.4
329
*
330
* @return void|string
331
*/
332
function lasso_clean_string( $string = '' ) {
333
334
	if ( empty( $string ) )
335
		return;
336
337
	return sanitize_text_field( strtolower( preg_replace('/[\s_]/', '-', $string ) ) );
338
}
339
340
/**
341
 *	Helper function to switch - to _ after having.
342
 *
343
 * This is the evil twin of lasso_clean_string() and may or may not make your data forever unclean.
344
 *
345
 *	@param $string string content
346
 *	@since 0.9.5
347
 *
348
 * @return void|string
349
 */
350
function lasso_unclean_string( $string = '' ) {
351
352
	if ( empty( $string ) ) {
353
		return;
354
	}
355
356
	return sanitize_text_field( strtolower( str_replace( '-', '_', $string ) ) );
357
}
358
359
360
////////////////////
361
// PLUGGABLE
362
////////////////////
363
364
/**
365
 * Check if the user is logged in and has the correctly passed capability
366
 *
367
 * @param unknown $action string a capability such as edit_posts or publish_posts
368
 * @param unknown $postid int the id of the post object to check against
369
 * @since 0.9.9.7 added filter 'lasso_user_can_filter'
370
 */
371
if ( !function_exists( 'lasso_user_can' ) ):
372
	function lasso_user_can( $action = '', $postid = 0 ) {
373
        $result = false;
374
		if ( empty( $action ) )
375
			$action = 'edit_posts';
376
377
		if ( empty( $postid ) )
378
			$postid = get_the_ID();
379
380
		if ( is_user_logged_in() && current_user_can( $action, $postid ) ) {
381
			// check against post types:
382
			$allowed_post_types = lasso_editor_get_option( 'allowed_post_types', 'lasso_editor', array( 'post', 'page') );
0 ignored issues
show
Documentation introduced by
'allowed_post_types' is of type string, but the function expects a object<unknown>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
Documentation introduced by
'lasso_editor' is of type string, but the function expects a object<unknown>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
Documentation introduced by
array('post', 'page') is of type array<integer,string,{"0":"string","1":"string"}>, but the function expects a string|object<unknown>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
383
			
384
			
385
			
386
            if (!empty($allowed_post_types) && !empty($postid)) {
387
				$type = get_post_type( $postid );
388
                $allowed_post_types = apply_filters( 'lasso_allowed_post_types', $allowed_post_types );
389
				
390
                if ( in_array( $type, $allowed_post_types ) ) {
391
				   $result =  true;
392
			    }
393
            } else {
394
				$result =  true;
395
        	}
396
		} else {
397
			$result = false;
398
		}
399
		
400
		return apply_filters( 'lasso_user_can_filter', $result,  $action, $postid);
401
	}
402
endif;
403
404
/**
405
*	Empty state message thats shown when no data available
406
*
407
*	@since 0.9.5
408
*/
409
if ( !function_exists('lasso_editor_empty_results') ):
410
411
	function lasso_editor_empty_results( $type = 'posts' ){
412
413
		if ( 'posts' == $type ) {
414
415
			$string = apply_filters('lasso_empty_state_message', __('No posts to show', 'lasso') );
416
			$icon = 'lasso-icon-file-text2';
417
			$button = false;
418
419
		} elseif ( 'revision' == $type ) {
420
421
			$string = apply_filters('lasso_empty_state_message', __('No revisions found', 'lasso') );
422
			$icon = 'lasso-icon-history';
423
			$button = sprintf('<a href="#" class="lasso--btn-secondary" id="lasso--close-modal">%s</a>', __('Close','lasso') );
424
425
		}
426
427
		return sprintf('<div id="lasso--empty-state" class="lasso--empty-state"><i class="lasso--empty-state-icon lasso-icon %s"></i><p>%s</p>%s</div>', $icon, $string, $button );
0 ignored issues
show
Bug introduced by
The variable $icon 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...
Bug introduced by
The variable $string 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...
Bug introduced by
The variable $button 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...
428
	}
429
430
endif;
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445