specify switch-condition in switch-branch-scope#1602
specify switch-condition in switch-branch-scope#1602staabm wants to merge 2 commits intophpstan:1.8.xfrom
Conversation
There was a problem hiding this comment.
had to add this case, as otherwise phpstan detects after this PR, that the Exception would always be thrown and $nullOverwrittenInSwitchToOne can never be reached
|
looked into the test failures. I think these are related to the new ValueType stuff and unrelated to this PR |
| $throwPoints = array_merge($throwPoints, $caseResult->getThrowPoints()); | ||
| $branchScope = $scopeForBranches->filterByTruthyValue($condExpr); | ||
|
|
||
| $branchScope = $branchScope->assignExpression($stmt->cond, $branchScope->getType($caseNode->cond)); |
There was a problem hiding this comment.
I don't agree with this, switch uses == semantics. So if you have:
switch ($foo) {
case true: doFoo();
break;
}$foo doesn't have to be true, it can be any truthy-string for example...
The way to achieve this is to make PHPStan aware of these semantics in TypeSpecifier, to make it learn the whole table "Loose comparisons with ==" here https://www.php.net/manual/en/types.comparisons.php.
If you want better analysis now, switch to the match expression which uses === semantics and is a much better alternative.
There was a problem hiding this comment.
Thx for the hint, will look into it.
I also realized that same type switch already works like I expected
killing some implicit mixed types, see https://phpstan.org/r/493a6a00-051c-4646-bf58-f508b3d7930e