Skip to content
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
42 changes: 31 additions & 11 deletions src/Type/Php/FilterVarDynamicReturnTypeExtension.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 PhpParser\Node\Arg;
use PhpParser\Node\Expr\FuncCall;
use PHPStan\Analyser\Scope;
use PHPStan\Reflection\FunctionReflection;
Expand All @@ -26,7 +27,10 @@
use PHPStan\Type\StringType;
use PHPStan\Type\Type;
use PHPStan\Type\TypeCombinator;
use function hexdec;
use function is_int;
use function octdec;
use function preg_match;
use function sprintf;
use function strtolower;

Expand Down Expand Up @@ -151,11 +155,15 @@ public function getTypeFromFunctionCall(

$flagsArg = $functionCall->getArgs()[2] ?? null;
$inputType = $scope->getType($functionCall->getArgs()[0]->value);
$exactType = $this->determineExactType($inputType, $filterValue);

$defaultType = $this->hasFlag($this->getConstant('FILTER_NULL_ON_FAILURE'), $flagsArg, $scope)
? new NullType()
: new ConstantBooleanType(false);
$exactType = $this->determineExactType($inputType, $filterValue, $defaultType, $flagsArg, $scope);
$type = $exactType ?? $this->getFilterTypeMap()[$filterValue] ?? $mixedType;

$typeOptionNames = $this->getFilterTypeOptions()[$filterValue] ?? [];
$otherTypes = $this->getOtherTypes($flagsArg, $scope, $typeOptionNames);
$otherTypes = $this->getOtherTypes($flagsArg, $scope, $typeOptionNames, $defaultType);

if ($inputType->isNonEmptyString()->yes()
&& $type->isString()->yes()
Expand Down Expand Up @@ -190,14 +198,32 @@ public function getTypeFromFunctionCall(
return $type;
}

private function determineExactType(Type $in, int $filterValue): ?Type
private function determineExactType(Type $in, int $filterValue, Type $defaultType, ?Arg $flagsArg, Scope $scope): ?Type
{
if (($filterValue === $this->getConstant('FILTER_VALIDATE_BOOLEAN') && $in instanceof BooleanType)
|| ($filterValue === $this->getConstant('FILTER_VALIDATE_INT') && $in instanceof IntegerType)
|| ($filterValue === $this->getConstant('FILTER_VALIDATE_FLOAT') && $in instanceof FloatType)) {
return $in;
}

if ($filterValue === $this->getConstant('FILTER_VALIDATE_INT') && $in instanceof ConstantStringType) {
$value = $in->getValue();
$allowOctal = $this->hasFlag($this->getConstant('FILTER_FLAG_ALLOW_OCTAL'), $flagsArg, $scope);
$allowHex = $this->hasFlag($this->getConstant('FILTER_FLAG_ALLOW_HEX'), $flagsArg, $scope);

if ($allowOctal && preg_match('/\A0[oO][0-7]+\z/', $value) === 1) {
$octalValue = octdec($value);
return is_int($octalValue) ? new ConstantIntegerType($octalValue) : $defaultType;
}

if ($allowHex && preg_match('/\A0[xX][0-9A-Fa-f]+\z/', $value) === 1) {
$hexValue = hexdec($value);
return is_int($hexValue) ? new ConstantIntegerType($hexValue) : $defaultType;
}

return preg_match('/\A[+-]?(?:0|[1-9][0-9]*)\z/', $value) === 1 ? $in->toInteger() : $defaultType;
}

if ($filterValue === $this->getConstant('FILTER_VALIDATE_FLOAT') && $in instanceof IntegerType) {
return $in->toFloat();
}
Expand All @@ -209,21 +235,15 @@ private function determineExactType(Type $in, int $filterValue): ?Type
* @param list<string> $typeOptionNames
* @return array{default: Type, range?: Type}
*/
private function getOtherTypes(?Node\Arg $flagsArg, Scope $scope, array $typeOptionNames): array
private function getOtherTypes(?Node\Arg $flagsArg, Scope $scope, array $typeOptionNames, Type $defaultType): array
{
$falseType = new ConstantBooleanType(false);
if ($flagsArg === null) {
return ['default' => $falseType];
}

$typeOptions = $this->getOptions($flagsArg, $scope, 'default', ...$typeOptionNames);
$defaultType = $typeOptions['default'] ?? null;
if ($defaultType === null) {
$defaultType = $this->hasFlag($this->getConstant('FILTER_NULL_ON_FAILURE'), $flagsArg, $scope)
? new NullType()
: $falseType;
}

$defaultType = $typeOptions['default'] ?? $defaultType;
$otherTypes = ['default' => $defaultType];
$range = [];
if (isset($typeOptions['min_range'])) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,31 @@ public function run(string $str, int $int, int $positive_int, int $negative_int)
$return = filter_var($str2, FILTER_VALIDATE_URL);
assertType('string|false', $return);

$str2 = 'foo';
$return = filter_var($str2, FILTER_VALIDATE_INT);
assertType('int|false', $return);
$return = filter_var('foo', FILTER_VALIDATE_INT);
assertType('false', $return);

$return = filter_var('foo', FILTER_VALIDATE_INT, FILTER_NULL_ON_FAILURE);
assertType('null', $return);

$return = filter_var('1', FILTER_VALIDATE_INT);
assertType('1', $return);

$return = filter_var('0', FILTER_VALIDATE_INT);
assertType('0', $return);

$return = filter_var('-1', FILTER_VALIDATE_INT);
assertType('-1', $return);

$return = filter_var('0o10', FILTER_VALIDATE_INT);
assertType('false', $return);

$return = filter_var('0o10', FILTER_VALIDATE_INT, FILTER_FLAG_ALLOW_OCTAL);
assertType('8', $return);

$return = filter_var('0x10', FILTER_VALIDATE_INT);
assertType('false', $return);

$return = filter_var('0x10', FILTER_VALIDATE_INT, FILTER_FLAG_ALLOW_HEX);
assertType('16', $return);
}
}