Skip to content

Improve return type precision of filter_input with invalid first args #2333

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
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 32 additions & 1 deletion src/Type/Php/FilterFunctionReturnTypeHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace PHPStan\Type\Php;

use PhpParser\Node;
use PHPStan\Php\PhpVersion;
use PHPStan\Reflection\ReflectionProvider;
use PHPStan\ShouldNotHappenException;
use PHPStan\TrinaryLogic;
Expand All @@ -19,6 +20,7 @@
use PHPStan\Type\IntegerRangeType;
use PHPStan\Type\IntegerType;
use PHPStan\Type\MixedType;
use PHPStan\Type\NeverType;
use PHPStan\Type\NullType;
use PHPStan\Type\StringType;
use PHPStan\Type\Type;
Expand Down Expand Up @@ -46,7 +48,9 @@ final class FilterFunctionReturnTypeHelper
/** @var array<int, list<string>>|null */
private ?array $filterTypeOptions = null;

public function __construct(private ReflectionProvider $reflectionProvider)
private ?Type $supportedFilterInputTypes = null;

public function __construct(private ReflectionProvider $reflectionProvider, private PhpVersion $phpVersion)
{
$this->flagsString = new ConstantStringType('flags');
}
Expand All @@ -69,6 +73,33 @@ public function getOffsetValueType(Type $inputType, Type $offsetType, ?Type $fil
: $filteredType;
}

public function getInputType(Type $typeType, Type $varNameType, ?Type $filterType, ?Type $flagsType): ?Type
{
$this->supportedFilterInputTypes ??= 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(),
);

if (!$typeType->isInteger()->yes() || $this->supportedFilterInputTypes->isSuperTypeOf($typeType)->no()) {
if ($this->phpVersion->throwsTypeErrorForInternalFunctions()) {
return new NeverType();
}

// Using a null as input mimics pre PHP 8 behaviour where filter_input
// would return the same as if the offset does not exist
$inputType = new NullType();
} else {
// 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->getOffsetValueType($inputType, $varNameType, $filterType, $flagsType);
}

public function getType(Type $inputType, ?Type $filterType, ?Type $flagsType): Type
{
$mixedType = new MixedType();
Expand Down
28 changes: 3 additions & 25 deletions src/Type/Php/FilterInputDynamicReturnTypeExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,17 @@

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 __construct(private FilterFunctionReturnTypeHelper $filterFunctionReturnTypeHelper)
{
}

Expand All @@ -33,24 +27,8 @@ public function getTypeFromFunctionCall(FunctionReflection $functionReflection,
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,
return $this->filterFunctionReturnTypeHelper->getInputType(
$scope->getType($functionCall->getArgs()[0]->value),
$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,
Expand Down
6 changes: 6 additions & 0 deletions tests/PHPStan/Analyser/NodeScopeResolverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -623,7 +623,13 @@ public function dataFileAsserts(): iterable
}

yield from $this->gatherAssertTypes(__DIR__ . '/data/filesystem-functions.php');

yield from $this->gatherAssertTypes(__DIR__ . '/data/filter-input.php');
if (PHP_VERSION_ID >= 80000) {
yield from $this->gatherAssertTypes(__DIR__ . '/data/filter-input-php8.php');
} else {
yield from $this->gatherAssertTypes(__DIR__ . '/data/filter-input-php7.php');
}
yield from $this->gatherAssertTypes(__DIR__ . '/data/filter-var.php');

if (PHP_VERSION_ID >= 80100) {
Expand Down
16 changes: 16 additions & 0 deletions tests/PHPStan/Analyser/data/filter-input-php7.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php declare(strict_types=1);

namespace FilterInputPhp7;

use function PHPStan\Testing\assertType;

class FilterInputPhp7
{

public function invalidTypesOrVarNames($mixed): void
{
assertType('null', filter_input(-1, 'foo', FILTER_VALIDATE_INT));
assertType('false', filter_input(-1, 'foo', FILTER_VALIDATE_INT, FILTER_NULL_ON_FAILURE));
}

}
16 changes: 16 additions & 0 deletions tests/PHPStan/Analyser/data/filter-input-php8.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php declare(strict_types=1);

namespace FilterInputPhp8;

use function PHPStan\Testing\assertType;

class FilterInputPhp8
{

public function invalidTypesOrVarNames($mixed): void
{
assertType('*NEVER*', filter_input(-1, 'foo', FILTER_VALIDATE_INT));
assertType('*NEVER*', filter_input(-1, 'foo', FILTER_VALIDATE_INT, FILTER_NULL_ON_FAILURE));
}

}
1 change: 0 additions & 1 deletion tests/PHPStan/Analyser/data/filter-input.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ 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));
}
Expand Down