New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Prevent ShouldNotHappenException in filter_* extensions #2951
Open
staabm
wants to merge
14
commits into
phpstan:1.10.x
Choose a base branch
from
staabm:no-exc
base: 1.10.x
Could not load branches
Branch not found: {{ refName }}
Could not load tags
Nothing to show
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
fa127be
Prevent ShouldNotHappenException in filter_* extensions
staabm 2e9e8a2
added type inference test
staabm a314e58
Revert "added type inference test"
staabm 3ae45f8
added e2e test
staabm 146c35b
Update e2e-tests.yml
staabm 073072e
php-version
staabm 9a12e32
Update FilterFunctionReturnTypeHelper.php
staabm a9a924a
fix
staabm a49aa49
Update e2e-tests.yml
staabm 5603a96
Update e2e-tests.yml
staabm d1b508f
downgrade e2e
staabm 87cd19a
Update e2e-tests.yml
staabm f939720
assert a invalid constant turns into mixed
staabm a6bf872
Revert "assert a invalid constant turns into mixed"
staabm File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
<?php | ||
|
||
// constant that's used in the Filter extension that was introduced in a later version of PHP. | ||
// on earlier php version introduce the same constant via a bootstrap file but with a wrong type | ||
if(!defined("FILTER_SANITIZE_ADD_SLASHES"))define("FILTER_SANITIZE_ADD_SLASHES",false); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
parameters: | ||
level: 9 | ||
paths: | ||
- src | ||
|
||
bootstrapFiles: | ||
- bootstrap.php |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
<?php | ||
|
||
function doFoo(mixed $filter): void { | ||
\PHPStan\Testing\assertType('non-falsy-string|false', filter_var("no", FILTER_VALIDATE_REGEXP)); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,7 +5,6 @@ | |
use PhpParser\Node; | ||
use PHPStan\Php\PhpVersion; | ||
use PHPStan\Reflection\ReflectionProvider; | ||
use PHPStan\ShouldNotHappenException; | ||
use PHPStan\TrinaryLogic; | ||
use PHPStan\Type\Accessory\AccessoryNonEmptyStringType; | ||
use PHPStan\Type\Accessory\AccessoryNonFalsyStringType; | ||
|
@@ -31,7 +30,6 @@ | |
use function is_int; | ||
use function octdec; | ||
use function preg_match; | ||
use function sprintf; | ||
use const PHP_FLOAT_EPSILON; | ||
|
||
final class FilterFunctionReturnTypeHelper | ||
|
@@ -40,6 +38,8 @@ final class FilterFunctionReturnTypeHelper | |
/** All validation filters match 0x100. */ | ||
private const VALIDATION_FILTER_BITMASK = 0x100; | ||
|
||
private const UNKNOWN_CONSTANT = -9999999; | ||
|
||
private ConstantStringType $flagsString; | ||
|
||
/** @var array<int, Type>|null */ | ||
|
@@ -240,7 +240,7 @@ private function getConstant(string $constantName): int | |
$constant = $this->reflectionProvider->getConstant(new Node\Name($constantName), null); | ||
$valueType = $constant->getValueType(); | ||
if (!$valueType instanceof ConstantIntegerType) { | ||
throw new ShouldNotHappenException(sprintf('Constant %s does not have integer type.', $constantName)); | ||
return self::UNKNOWN_CONSTANT; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I thought about returning null, or catching this exception (or a new exception) at the call-sites, but this seemed too much for this case. instead I decided to return a negative constant value, which does not overlap with other existing FILTER_* constants |
||
} | ||
|
||
return $valueType->getValue(); | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Alright so I found out that the usual result on PHP versions where this constant exists is the same as here. Which makes me a bit suspicious. Can you explain that? We surely must be losing information when we return the unknown constant somewhere, right?