Completed
Push — master ( 02e1e3...df1849 )
by
unknown
01:57
created

helpers.php ➔ lasso_editor_get_option()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 20
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 10
c 0
b 0
f 0
nc 5
nop 3
dl 0
loc 20
rs 8.8571
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 16 and the first side effect is on line 36.

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
if( !function_exists('lasso_editor_get_option')):
16 View Code Duplication
	function lasso_editor_get_option( $option, $section, $default = '' ) {
0 ignored issues
show
Best Practice introduced by
The function lasso_editor_get_option() has been defined more than once; this definition is ignored, only the first definition in lasso.php (L64-83) is considered.

This check looks for functions that have already been defined in other files.

Some Codebases, like WordPress, make a practice of defining functions multiple times. This may lead to problems with the detection of function parameters and types. If you really need to do this, you can mark the duplicate definition with the @ignore annotation.

/**
 * @ignore
 */
function getUser() {

}

function getUser($id, $realm) {

}

See also the PhpDoc documentation for @ignore.

Loading history...
Duplication introduced by
This function seems to be duplicated in 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...
17
18
		if ( empty( $option ) )
19
			return;
20
21
		if ( function_exists( 'is_multisite' ) && is_multisite() ) {
22
23
			$options = get_site_option( $section );
24
25
		} else {
26
27
			$options = get_option( $section );
28
		}
29
30
		if ( isset( $options[$option] ) ) {
31
			return $options[$option];
32
		}
33
34
		return $default;
35
	}
36
endif;
37
38
/**
39
 * Check to see if any Lasso galleries exist
40
 *
41
 * @since 1.0
42
 */
43
function lasso_editor_galleries_exist() {
44
45
	$q = new wp_query( array( 'post_type' => 'ai_galleries', 'post_status' => 'publish' ) );
46
47
	if ( $q->have_posts() )
48
		return true;
49
	else
50
		return false;
51
}
52
53
/**
54
 * Return a CSS class of an automatically supported theme
55
 *
56
 * @since 0.8.6
57
 * @return a css class if the theme is supported, false if nothing
58
 */
59
function lasso_get_supported_theme_class() {
60
61
	$name  	= wp_get_theme()->get('Name');
62
	$slug  	= lasso_clean_string( $name );
63
64
	switch ( $slug ) {
65
		case 'aesop-story-theme': // aesop
66
			$out = '.aesop-entry-content';
67
			break;
68
		case 'jorgen': // aesop
69
			$out = '.jorgen-entry-content';
70
			break;
71
		case 'novella': // aesop
72
			$out = '.novella-entry-content';
73
			break;
74
		case 'genji': // aesop
75
			$out = '.genji-entry-content';
76
			break;
77
		case 'kerouac': // aesop
78
			$out = '.kerouac-entry-content';
79
			break;
80
		case 'zealot': // aesop
81
			$out = '.zealot-entry-content';
82
			break;
83
		case 'fable': // aesop
84
			$out = '.fable--entry-content';
85
			break;
86
		case 'canvas': // wootheme..err...Automattic
87
			$out = '.entry';
88
			break;
89
		case 'kleo': // 
90
			$out = '.article-content';
91
			break;
92
		//case 'exposure': // 
93
		//	$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...
94
		//	break;
95
		//case 'lore': // 
96
		//	$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...
97
		//	break;
98
		//case 'worldview': // upthemes
99
		//	$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...
100
		//	break;
101
		//case 'genesis': // genesis
102
		//	$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...
103
		//	break;
104
		//case 'camera': // array.is
105
		//	$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...
106
		//	break;
107
		case 'longform': 
108
			$out = '.entry-content-wrapper';
109
			break;
110
111
	}
112
113
	return apply_filters('lasso_content_class', !empty( $out ) ? $out : false);
114
	//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...
115
}
116
117
function lasso_get_supported_theme_title_class() {
118
119
	$name  	= wp_get_theme()->get('Name');
120
	$slug  	= lasso_clean_string( $name );
121
122
	switch ( $slug ) {
123
124
		case 'aesop-story-theme': // aesop
125
			$out = '.aesop-entry-title';
126
			break;
127
		case 'jorgen': // aesop
128
			$out = '.jorgen-entry-title';
129
			break;
130
		case 'genji': // aesop
131
			$out = '.genji-entry-title';
132
			break;
133
		case 'kerouac': // aesop
134
			$out = '.kerouac-entry-title';
135
			break;
136
		case 'zealot': // aesop
137
			$out = '.zealot-entry-title';
138
			break;
139
		case 'fable': // aesop
140
			$out = '.fable--entry-title';
141
			break;
142
		case 'kleo': // 
143
			$out = '.page-title';
144
			break;
145
		case 'longform': // 
146
			$out = '.entry-title';
147
			break;
148
	}
149
150
	return apply_filters('lasso_title_class', !empty( $out ) ? $out : false);
151
}
152
153
//since 0.9.9.6
154
function lasso_get_supported_theme_featured_image_class() {
155
156
	$name  	= wp_get_theme()->get('Name');
157
	$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...
158
159
	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...
160
}
161
162
163
/**
164
*	Return a string of classes with items that Lasso will remove when entering the editor
165
*	so that we don't save them as HTML
166
*
167
*	@since 0.8.7
168
*	@return string of comma separated classes
169
*/
170
function lasso_supported_no_save(){
171
172
	return apply_filters('lasso_dont_save', '.lasso--ignore, .sharedaddy, .us_wrapper, .meta, .edit-link, .ssba, .jp-relatedposts, .fb-comments');
173
}
174
175
/**
176
 * Generic sanitization, useful for sanitization of arrays.
177
 *
178
 * @since 0.9.2
179
 *
180
 * @param array|object|string $data Data to sanatize.
181
 *
182
 * @return array|mixed|object|string|void
183
 */
184
function lasso_sanitize_data( $data ) {
185
	return \lasso\sanatize::do_sanitize( $data );
186
187
}
188
189
/**
190
 *	Return a comma delimited list of categories for a specific post object
191
 *
192
 *	@since 0.9.3
193
 *	@return string of comma delimited category slugs
194
*/
195
function lasso_get_post_objects( $postid = '', $taxonomy = 'category') {
196
197
	if ( empty( $postid ) )
198
		$postid = get_the_ID();
199
200
	$objects = 'category' == $taxonomy ? get_the_category( $postid ) : get_the_tags( $postid );
201
202
	if ( empty( $objects) )
203
		return;
204
205
	$out = '';
206
	foreach( $objects as $object ) {
207
		//$out .= $object->slug.', ';
0 ignored issues
show
Unused Code Comprehensibility introduced by
46% 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...
208
		$out .= $object->name.',';
209
	}
210
211
	return rtrim($out, ', ');
212
213
}
214
215
/**
216
 *	Return an array of categories for autocomplete
217
 *
218
 *	@since 0.9.3
219
 *	@return array all categoiries
220
*/
221
function lasso_get_objects( $taxonomy = 'category' ) {
222
223
	$objects = 'category' == $taxonomy ? get_categories(array('hide_empty' => 0)) : get_tags(array('hide_empty' => 0));
224
225
	if ( empty( $objects) )
226
		return;
227
228
	$out = "";
229
	foreach( $objects as $object ) {
230
		$out .= $object->name.',';
231
	}
232
233
	return $out;
234
}
235
236
237
/**
238
 * Get allowed post types for the post chooser modal.
239
 *
240
 *
241
 * @since 0.9.4
242
 */
243
function lasso_post_types_names() {
244
	$post_types = get_post_types( array(
245
		'public' => true,
246
	), 'objects' );
247
	$post_types = array_combine( array_keys( $post_types ), wp_list_pluck( $post_types, 'label' ) );
248
    unset( $post_types[ 'attachment' ] );
249
250
	/**
251
	 * Set which post types are allowed
252
	 *
253
	 * @since 0.9.4
254
	 *
255
	 * @param array $allowed_post_types Array of names (not labels) of allowed post types. Must be registered.
256
	 */
257
	$allowed_post_types = lasso_editor_get_option( 'allowed_post_types', 'lasso_editor', array( 'post', 'page') );
0 ignored issues
show
Documentation introduced by
array('post', 'page') is of type array<integer,string,{"0":"string","1":"string"}>, but the function expects a string.

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...
258
	$allowed_post_types = apply_filters( 'lasso_allowed_post_types', $allowed_post_types );
259
	if (!current_user_can('edit_pages')) {
260
		$allowed_post_types = array_diff($allowed_post_types,array('page'));
261
	}
262
	foreach( $post_types as $name => $label ) {
263
		if ( ! in_array( $name, $allowed_post_types ) ) {
264
			unset( $post_types[ $name ] );
265
		}
266
	}
267
	return $post_types;
268
}
269
270
271
function lasso_post_types() {
272
	$post_types = get_post_types( array(
273
		'public' => true,
274
	), 'names' );
275
    unset( $post_types[ 'attachment' ] );
276
277
	/**
278
	 * Set which post types are allowed
279
	 *
280
	 * @since 0.9.4
281
	 *
282
	 * @param array $allowed_post_types Array of names (not labels) of allowed post types. Must be registered.
283
	 */
284
	$allowed_post_types = lasso_editor_get_option( 'allowed_post_types', 'lasso_editor', array( 'post') );
0 ignored issues
show
Documentation introduced by
array('post') is of type array<integer,string,{"0":"string"}>, but the function expects a string.

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...
285
	$allowed_post_types = apply_filters( 'lasso_allowed_post_types', $allowed_post_types );
286
	foreach( $post_types as $name => $label ) {
287
		if ( ! in_array( $name, $allowed_post_types ) ) {
288
			unset( $post_types[ $name ] );
289
		}
290
	}
291
	return $post_types;
292
}
293
294
function lasso_post_types_rest_base() {
295
	global $wp_post_types;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

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

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...
397
			
398
			if (!current_user_can('edit_pages')) {
399
				$allowed_post_types = array_diff($allowed_post_types,array('page'));
400
			}
401
			
402
            if (!empty($allowed_post_types) && !empty($postid)) {
403
				$type = get_post_type( $postid );
404
                $allowed_post_types = apply_filters( 'lasso_allowed_post_types', $allowed_post_types );
405
				
406
                if ( in_array( $type, $allowed_post_types ) ) {
407
				   $result =  true;
408
			    }
409
            } else {
410
                //we are not checking against a post, return true
411
				$result =  true;
412
        	}
413
		} else {
414
			$result = false;
415
		}
416
		
417
		return apply_filters( 'lasso_user_can_filter', $result,  $action, $postid);
418
	}
419
endif;
420
421
/**
422
*	Empty state message thats shown when no data available
423
*
424
*	@since 0.9.5
425
*/
426
if ( !function_exists('lasso_editor_empty_results') ):
427
428
	function lasso_editor_empty_results( $type = 'posts' ){
429
430
		if ( 'posts' == $type ) {
431
432
			$string = apply_filters('lasso_empty_state_message', __('No posts to show', 'lasso') );
433
			$icon = 'lasso-icon-file-text2';
434
			$button = false;
435
436
		} elseif ( 'revision' == $type ) {
437
438
			$string = apply_filters('lasso_empty_state_message', __('No revisions found', 'lasso') );
439
			$icon = 'lasso-icon-history';
440
			$button = sprintf('<a href="#" class="lasso--btn-secondary" id="lasso--close-modal">%s</a>', __('Close','lasso') );
441
442
		}
443
444
		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...
445
	}
446
447
endif;
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462