Question

I've next case

switch ($var) {
    case 'a':
        $model = 'x';
        break;
    case 'b':
        $model = 'y';
        break;
    // others cases ...
    default:
        // does actions and calls exit(), 
        // so i don't have to put return/break after call
        // because it is unreachable, but phpstorm don't know it
        myfunc(); 
}

// here inspector says me "variable $model might not be defined"
// but always have $model here
anotherfunc($model);
Was it helpful?

Solution

Best way would be to defined $module right before the switch or using the following comment:

/** @var string $model */

This comment will tell php-storm that $model is set and that its a string. You may also use mixed instead of string which says that it could be everything but its set.

I would recomment using the comment since it has no performance-impact on the script. Also using is_set would solve the problem but is using performance of the script.

Anyway you may only use such comments if there is no way it could be anything else and is definitly set.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top