-
Notifications
You must be signed in to change notification settings - Fork 545
[PHP 8.5] add new rule for filter_var #4553
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
Open
canvural
wants to merge
1
commit into
phpstan:2.1.x
Choose a base branch
from
canvural:filter-var-rule
base: 2.1.x
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
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.
+119
−1
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,69 @@ | ||
| <?php declare(strict_types = 1); | ||
|
|
||
| namespace PHPStan\Rules\Functions; | ||
|
|
||
| use PhpParser\Node; | ||
| use PhpParser\Node\Expr\FuncCall; | ||
| use PhpParser\Node\Name; | ||
| use PHPStan\Analyser\Scope; | ||
| use PHPStan\DependencyInjection\RegisteredRule; | ||
| use PHPStan\Reflection\ReflectionProvider; | ||
| use PHPStan\Rules\Rule; | ||
| use PHPStan\Rules\RuleErrorBuilder; | ||
| use PHPStan\Type\Php\FilterFunctionReturnTypeHelper; | ||
| use function count; | ||
|
|
||
| /** | ||
| * @implements Rule<Node\Expr\FuncCall> | ||
| */ | ||
| #[RegisteredRule(level: 0)] | ||
| final class FilterVarRule implements Rule | ||
| { | ||
|
|
||
| public function __construct( | ||
| private ReflectionProvider $reflectionProvider, | ||
| private FilterFunctionReturnTypeHelper $filterFunctionReturnTypeHelper, | ||
| ) | ||
| { | ||
| } | ||
|
|
||
| public function getNodeType(): string | ||
| { | ||
| return FuncCall::class; | ||
| } | ||
|
|
||
| public function processNode(Node $node, Scope $scope): array | ||
| { | ||
| if (!($node->name instanceof Node\Name)) { | ||
| return []; | ||
| } | ||
|
|
||
| if ($this->reflectionProvider->resolveFunctionName($node->name, $scope) !== 'filter_var') { | ||
| return []; | ||
| } | ||
|
|
||
| $args = $node->getArgs(); | ||
|
|
||
| if ($this->reflectionProvider->hasConstant(new Name\FullyQualified('FILTER_THROW_ON_FAILURE'), null)) { | ||
| if (count($args) < 3) { | ||
| return []; | ||
| } | ||
|
|
||
| $flagsType = $scope->getType($args[2]->value); | ||
|
|
||
| if ($this->filterFunctionReturnTypeHelper->hasFlag('FILTER_NULL_ON_FAILURE', $flagsType) | ||
| ->and($this->filterFunctionReturnTypeHelper->hasFlag('FILTER_THROW_ON_FAILURE', $flagsType)) | ||
| ->yes() | ||
| ) { | ||
| return [ | ||
| RuleErrorBuilder::message('Cannot use both FILTER_NULL_ON_FAILURE and FILTER_THROW_ON_FAILURE.') | ||
| ->identifier('filterVar.nullOnFailureAndThrowOnFailure') | ||
| ->build(), | ||
| ]; | ||
| } | ||
| } | ||
|
|
||
| return []; | ||
| } | ||
|
|
||
| } | ||
This file contains hidden or 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 hidden or 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,32 @@ | ||
| <?php declare(strict_types = 1); | ||
|
|
||
| namespace PHPStan\Rules\Functions; | ||
|
|
||
| use PHPStan\Rules\Rule; | ||
| use PHPStan\Testing\RuleTestCase; | ||
| use PHPStan\Type\Php\FilterFunctionReturnTypeHelper; | ||
| use PHPUnit\Framework\Attributes\RequiresPhp; | ||
|
|
||
| /** @extends RuleTestCase<FilterVarRule> */ | ||
| class FilterVarRuleTest extends RuleTestCase | ||
| { | ||
|
|
||
| protected function getRule(): Rule | ||
| { | ||
| return new FilterVarRule( | ||
| self::createReflectionProvider(), | ||
| self::getContainer()->getByType(FilterFunctionReturnTypeHelper::class), | ||
| ); | ||
| } | ||
|
|
||
| #[RequiresPhp('8.5')] | ||
| public function testRule(): void | ||
| { | ||
| $this->analyse([__DIR__ . '/data/filter_var_null_and_throw.php'], [ | ||
| ['Cannot use both FILTER_NULL_ON_FAILURE and FILTER_THROW_ON_FAILURE.', 5], | ||
| ['Cannot use both FILTER_NULL_ON_FAILURE and FILTER_THROW_ON_FAILURE.', 8], | ||
| ['Cannot use both FILTER_NULL_ON_FAILURE and FILTER_THROW_ON_FAILURE.', 10], | ||
| ]); | ||
| } | ||
|
|
||
| } |
17 changes: 17 additions & 0 deletions
17
tests/PHPStan/Rules/Functions/data/filter_var_null_and_throw.php
This file contains hidden or 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,17 @@ | ||
| <?php // lint >= 8.5 | ||
|
|
||
| namespace FilterVarNullAndThrow; | ||
|
|
||
| filter_var('foo@bar.test', FILTER_VALIDATE_EMAIL, FILTER_THROW_ON_FAILURE|FILTER_NULL_ON_FAILURE); | ||
|
|
||
| $flag = FILTER_NULL_ON_FAILURE|FILTER_THROW_ON_FAILURE; | ||
| filter_var(100, FILTER_VALIDATE_INT, $flag); | ||
|
|
||
| filter_var( | ||
| 'johndoe', | ||
| FILTER_VALIDATE_REGEXP, | ||
| ['options' => ['regexp' => '/^[a-z]+$/'], 'flags' => FILTER_THROW_ON_FAILURE|FILTER_NULL_ON_FAILURE] | ||
| ); | ||
| filter_var('foo@bar.test', FILTER_VALIDATE_EMAIL, FILTER_NULL_ON_FAILURE); | ||
| filter_var('foo@bar.test', FILTER_VALIDATE_EMAIL, FILTER_THROW_ON_FAILURE); | ||
|
|
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.
Maybe you should also report maybe if
reportMaybeis enabled.But I'm unsure how good those filterFunctionReturnTypeHelper::hasFlag works so far