-
Notifications
You must be signed in to change notification settings - Fork 515
Specify return type for filter_input()
#2010
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
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
9e1b8ab
Specify return type for `filter_input()`
herndlm b80da01
Update src/Type/Php/FilterInputDynamicReturnTypeExtension.php
herndlm cd777e7
Update src/Type/Php/FilterInputDynamicReturnTypeExtension.php
herndlm 5628019
fix, improve and simplify
herndlm 7f85e3c
Use ArrayType instead of Type in helper method arg
herndlm 03bbbdb
Simplify and loosen-up sanity check
herndlm 35cf323
No need to limit input to array..
herndlm 0883ece
Support compound types as filter_input type constant
herndlm 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
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,60 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace PHPStan\Type\Php; | ||
|
||
use PhpParser\Node; | ||
use PhpParser\Node\Expr\FuncCall; | ||
use PHPStan\Analyser\Scope; | ||
use PHPStan\Reflection\FunctionReflection; | ||
use PHPStan\Reflection\ReflectionProvider; | ||
use PHPStan\Type\ArrayType; | ||
use PHPStan\Type\DynamicFunctionReturnTypeExtension; | ||
use PHPStan\Type\MixedType; | ||
use PHPStan\Type\StringType; | ||
use PHPStan\Type\Type; | ||
use PHPStan\Type\TypeCombinator; | ||
use function count; | ||
|
||
class FilterInputDynamicReturnTypeExtension implements DynamicFunctionReturnTypeExtension | ||
{ | ||
|
||
public function __construct(private FilterFunctionReturnTypeHelper $filterFunctionReturnTypeHelper, private ReflectionProvider $reflectionProvider) | ||
{ | ||
} | ||
|
||
public function isFunctionSupported(FunctionReflection $functionReflection): bool | ||
{ | ||
return $functionReflection->getName() === 'filter_input'; | ||
} | ||
|
||
public function getTypeFromFunctionCall(FunctionReflection $functionReflection, FuncCall $functionCall, Scope $scope): ?Type | ||
{ | ||
if (count($functionCall->getArgs()) < 2) { | ||
return null; | ||
} | ||
|
||
$supportedTypes = TypeCombinator::union( | ||
$this->reflectionProvider->getConstant(new Node\Name('INPUT_GET'), null)->getValueType(), | ||
$this->reflectionProvider->getConstant(new Node\Name('INPUT_POST'), null)->getValueType(), | ||
$this->reflectionProvider->getConstant(new Node\Name('INPUT_COOKIE'), null)->getValueType(), | ||
$this->reflectionProvider->getConstant(new Node\Name('INPUT_SERVER'), null)->getValueType(), | ||
$this->reflectionProvider->getConstant(new Node\Name('INPUT_ENV'), null)->getValueType(), | ||
); | ||
$typeType = $scope->getType($functionCall->getArgs()[0]->value); | ||
if (!$typeType->isInteger()->yes() || $supportedTypes->isSuperTypeOf($typeType)->no()) { | ||
return null; | ||
} | ||
|
||
// Pragmatical solution since global expressions are not passed through the scope for performance reasons | ||
// See https://github.com/phpstan/phpstan-src/pull/2012 for details | ||
$inputType = new ArrayType(new StringType(), new MixedType()); | ||
|
||
return $this->filterFunctionReturnTypeHelper->getOffsetValueType( | ||
$inputType, | ||
$scope->getType($functionCall->getArgs()[1]->value), | ||
isset($functionCall->getArgs()[2]) ? $scope->getType($functionCall->getArgs()[2]->value) : null, | ||
isset($functionCall->getArgs()[3]) ? $scope->getType($functionCall->getArgs()[3]->value) : null, | ||
); | ||
} | ||
|
||
} |
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
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,43 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace FilterInput; | ||
|
||
use function PHPStan\Testing\assertType; | ||
|
||
class FilterInput | ||
{ | ||
|
||
public function invalidTypesOrVarNames($mixed): void | ||
{ | ||
assertType('int|false|null', filter_input(INPUT_GET, $mixed, FILTER_VALIDATE_INT)); | ||
assertType('mixed', filter_input(-1, 'foo', FILTER_VALIDATE_INT)); | ||
assertType('null', filter_input(INPUT_GET, 17, FILTER_VALIDATE_INT)); | ||
assertType('false', filter_input(INPUT_GET, 17, FILTER_VALIDATE_INT, FILTER_NULL_ON_FAILURE)); | ||
} | ||
|
||
public function supportedSuperGlobals(): void | ||
{ | ||
assertType('int|false|null', filter_input(INPUT_GET, 'foo', FILTER_VALIDATE_INT)); | ||
assertType('int|false|null', filter_input(INPUT_POST, 'foo', FILTER_VALIDATE_INT)); | ||
assertType('int|false|null', filter_input(INPUT_COOKIE, 'foo', FILTER_VALIDATE_INT)); | ||
assertType('int|false|null', filter_input(INPUT_SERVER, 'foo', FILTER_VALIDATE_INT)); | ||
assertType('int|false|null', filter_input(INPUT_ENV, 'foo', FILTER_VALIDATE_INT)); | ||
} | ||
|
||
public function inputTypeUnion(): void | ||
{ | ||
assertType('int|false|null', filter_input(rand(0, 1) ? INPUT_GET : INPUT_POST, 'foo', FILTER_VALIDATE_INT)); | ||
} | ||
|
||
public function doFoo(string $foo): void | ||
{ | ||
assertType('int|false|null', filter_input(INPUT_GET, $foo, FILTER_VALIDATE_INT)); | ||
assertType('int|false|null', filter_input(INPUT_GET, 'foo', FILTER_VALIDATE_INT)); | ||
assertType('int|false|null', filter_input(INPUT_GET, 'foo', FILTER_VALIDATE_INT, ['flags' => FILTER_NULL_ON_FAILURE])); | ||
assertType("'invalid'|int|null", filter_input(INPUT_GET, 'foo', FILTER_VALIDATE_INT, ['options' => ['default' => 'invalid']])); | ||
assertType('array<int|false>|null', filter_input(INPUT_GET, 'foo', FILTER_VALIDATE_INT, ['flags' => FILTER_FORCE_ARRAY])); | ||
assertType('array<int|null>|false', filter_input(INPUT_GET, 'foo', FILTER_VALIDATE_INT, ['flags' => FILTER_FORCE_ARRAY|FILTER_NULL_ON_FAILURE])); | ||
assertType('0|int<17, 19>|null', filter_input(INPUT_GET, 'foo', FILTER_VALIDATE_INT, ['options' => ['default' => 0, 'min_range' => 17, 'max_range' => 19]])); | ||
} | ||
|
||
} |
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,15 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace Bug6261; | ||
|
||
function needs_int(int $x) : void {} | ||
|
||
function () { | ||
$x = filter_input(INPUT_POST, 'row_id', FILTER_VALIDATE_INT); | ||
|
||
if($x === false || $x === null) { | ||
die("I expected a numeric string!\n"); | ||
} | ||
|
||
needs_int($x); | ||
}; |
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.
Uh oh!
There was an error while loading. Please reload this page.