Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Rework filter var dynamic return to use typesystem
This includes support for bit masks, and force array. It also updates the old implementation of retrieving the constant, to use the type system as well, allow usage of variables.
  • Loading branch information
BackEndTea committed Jan 2, 2020
commit bc1a93caa631eb4ef9d09b1a06225bc35226db8c
140 changes: 89 additions & 51 deletions src/Type/Php/FilterVarDynamicReturnTypeExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,15 @@
namespace PHPStan\Type\Php;

use PhpParser\Node;
use PhpParser\Node\Expr\ConstFetch;
use PhpParser\Node\Expr\FuncCall;
use PHPStan\Analyser\Scope;
use PHPStan\Reflection\FunctionReflection;
use PHPStan\Type\ArrayType;
use PHPStan\Type\BooleanType;
use PHPStan\Type\Constant\ConstantArrayType;
use PHPStan\Type\Constant\ConstantBooleanType;
use PHPStan\Type\Constant\ConstantIntegerType;
use PHPStan\Type\Constant\ConstantStringType;
use PHPStan\Type\DynamicFunctionReturnTypeExtension;
use PHPStan\Type\FloatType;
use PHPStan\Type\IntegerType;
Expand All @@ -21,52 +24,64 @@
class FilterVarDynamicReturnTypeExtension implements DynamicFunctionReturnTypeExtension
{

/** @var array<string, Type> */
/** @var array<int, Type> */
private $filterTypesHashMaps;

/** @var array<string, Type> */
/** @var array<int, Type> */
private $nullableTypes;

/** @var ConstantStringType */
private $flagsString;

public function __construct()
{
$booleanType = new BooleanType();
$nullableBooleanType = new UnionType([new BooleanType(), new NullType()]);
$floatOrFalseType = new UnionType([new FloatType(), new ConstantBooleanType(false)]);
$nullableFloatType = new UnionType([new FloatType(), new NullType()]);
$intOrFalseType = new UnionType([new IntegerType(), new ConstantBooleanType(false)]);
$nullableIntType = new UnionType([new IntegerType(), new NullType()]);
$stringOrFalseType = new UnionType([new StringType(), new ConstantBooleanType(false)]);
$nullableStringType = new UnionType([new StringType(), new NullType()]);
$floatType = new FloatType();
$intType = new IntegerType();
$stringType = new StringType();

$nullType = new NullType();
$falseType = new ConstantBooleanType(false);

$nullableBooleanType = new UnionType([$booleanType, $nullType]);
$floatOrFalseType = new UnionType([$floatType, $falseType]);
$nullableFloatType = new UnionType([$floatType, $nullType]);
$intOrFalseType = new UnionType([$intType, $falseType]);
$nullableIntType = new UnionType([$intType, $nullType]);
$stringOrFalseType = new UnionType([$stringType, $falseType]);
$nullableStringType = new UnionType([$stringType, $nullType]);

$this->filterTypesHashMaps = [
'FILTER_SANITIZE_EMAIL' => $stringOrFalseType,
'FILTER_SANITIZE_ENCODED' => $stringOrFalseType,
'FILTER_SANITIZE_MAGIC_QUOTES' => $stringOrFalseType,
'FILTER_SANITIZE_NUMBER_FLOAT' => $stringOrFalseType,
'FILTER_SANITIZE_NUMBER_INT' => $stringOrFalseType,
'FILTER_SANITIZE_SPECIAL_CHARS' => $stringOrFalseType,
'FILTER_SANITIZE_STRING' => $stringOrFalseType,
'FILTER_SANITIZE_URL' => $stringOrFalseType,
'FILTER_VALIDATE_BOOLEAN' => $booleanType,
'FILTER_VALIDATE_EMAIL' => $stringOrFalseType,
'FILTER_VALIDATE_FLOAT' => $floatOrFalseType,
'FILTER_VALIDATE_INT' => $intOrFalseType,
'FILTER_VALIDATE_IP' => $stringOrFalseType,
'FILTER_VALIDATE_MAC' => $stringOrFalseType,
'FILTER_VALIDATE_REGEXP' => $stringOrFalseType,
'FILTER_VALIDATE_URL' => $stringOrFalseType,
FILTER_SANITIZE_EMAIL => $stringOrFalseType,
FILTER_SANITIZE_ENCODED => $stringOrFalseType,
FILTER_SANITIZE_MAGIC_QUOTES => $stringOrFalseType,
FILTER_SANITIZE_NUMBER_FLOAT => $stringOrFalseType,
FILTER_SANITIZE_NUMBER_INT => $stringOrFalseType,
FILTER_SANITIZE_SPECIAL_CHARS => $stringOrFalseType,
FILTER_SANITIZE_STRING => $stringOrFalseType,
FILTER_SANITIZE_URL => $stringOrFalseType,
FILTER_VALIDATE_BOOLEAN => $booleanType,
FILTER_VALIDATE_EMAIL => $stringOrFalseType,
FILTER_VALIDATE_FLOAT => $floatOrFalseType,
FILTER_VALIDATE_INT => $intOrFalseType,
FILTER_VALIDATE_IP => $stringOrFalseType,
FILTER_VALIDATE_MAC => $stringOrFalseType,
FILTER_VALIDATE_REGEXP => $stringOrFalseType,
FILTER_VALIDATE_URL => $stringOrFalseType,
];

$this->nullableTypes = [
'FILTER_VALIDATE_BOOLEAN' => $nullableBooleanType,
'FILTER_VALIDATE_EMAIL' => $nullableStringType,
'FILTER_VALIDATE_FLOAT' => $nullableFloatType,
'FILTER_VALIDATE_INT' => $nullableIntType,
'FILTER_VALIDATE_IP' => $nullableStringType,
'FILTER_VALIDATE_MAC' => $nullableStringType,
'FILTER_VALIDATE_REGEXP' => $nullableStringType,
'FILTER_VALIDATE_URL' => $nullableStringType,
FILTER_VALIDATE_BOOLEAN => $nullableBooleanType,
FILTER_VALIDATE_EMAIL => $nullableStringType,
FILTER_VALIDATE_FLOAT => $nullableFloatType,
FILTER_VALIDATE_INT => $nullableIntType,
FILTER_VALIDATE_IP => $nullableStringType,
FILTER_VALIDATE_MAC => $nullableStringType,
FILTER_VALIDATE_REGEXP => $nullableStringType,
FILTER_VALIDATE_URL => $nullableStringType,
];

$this->flagsString = new ConstantStringType('flags');
}

public function isFunctionSupported(FunctionReflection $functionReflection): bool
Expand All @@ -87,36 +102,59 @@ public function getTypeFromFunctionCall(
return $mixedType;
}

$filterExpr = $filterArg->value;
if (!$filterExpr instanceof ConstFetch) {
$filterType = $scope->getType($filterArg->value);
if (!$filterType instanceof ConstantIntegerType) {
return $mixedType;
}

$filterName = (string) $filterExpr->name;
$filterValue = $filterType->getValue();

$flagsArg = $functionCall->args[2] ?? null;
if ($this->isNullableType($filterValue, $flagsArg, $scope)) {
$type = $this->nullableTypes[$filterValue];
} else {
$type = $this->filterTypesHashMaps[$filterValue] ?? $mixedType;
}

if ($this->isForcedArrayType($flagsArg, $scope)) {
return new ArrayType(new MixedType(), $type);
}

return $type;
}

if ($this->isNullableType($filterName, $functionCall->args[2] ?? null)) {
return $this->nullableTypes[$filterName];
private function isNullableType(int $filterValue, ?Node\Arg $flagsArg, Scope $scope): bool
{
if ($flagsArg === null || !array_key_exists($filterValue, $this->nullableTypes)) {
return false;
}

return $this->filterTypesHashMaps[$filterName] ?? $mixedType;
return $this->hasFlag(FILTER_NULL_ON_FAILURE, $flagsArg, $scope);
}

private function isNullableType(string $filterName, ?Node\Arg $thirdArg): bool
private function isForcedArrayType(?Node\Arg $flagsArg, Scope $scope): bool
{
if ($thirdArg === null || !array_key_exists($filterName, $this->nullableTypes)) {
if ($flagsArg === null) {
return false;
}

$expr = $thirdArg->value;
if ($expr instanceof Node\Expr\Array_) {
foreach ($expr->items as $item) {
if ($item->key instanceof Node\Scalar\String_ && $item->key->value === 'flags') {
$expr = $item->value;
break;
}
}
return $this->hasFlag(FILTER_FORCE_ARRAY, $flagsArg, $scope);
}

private function hasFlag(int $flag, Node\Arg $expression, Scope $scope): bool
{
$type = $this->getFlagsValue($scope->getType($expression->value));

return $type instanceof ConstantIntegerType && ($type->getValue() & $flag) === $flag;
}

private function getFlagsValue(Type $exprType): Type
{
if (!$exprType instanceof ConstantArrayType) {
return $exprType;
}
return $expr instanceof ConstFetch && (string) $expr->name === 'FILTER_NULL_ON_FAILURE';

return $exprType->getOffsetValueType($this->flagsString);
}

}
53 changes: 53 additions & 0 deletions tests/PHPStan/Analyser/NodeScopeResolverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8006,6 +8006,10 @@ public function dataFilterVar(): array
'string|false',
'filter_var($mixed, FILTER_SANITIZE_EMAIL)',
],
[
'array<string|false>',
'filter_var($mixed, FILTER_SANITIZE_EMAIL, FILTER_FORCE_ARRAY)',
],
[
'string|false',
'filter_var($mixed, FILTER_SANITIZE_ENCODED)',
Expand Down Expand Up @@ -8066,6 +8070,19 @@ public function dataFilterVar(): array
'float|null',
'filter_var($mixed, FILTER_VALIDATE_FLOAT, FILTER_NULL_ON_FAILURE)',
],

[
'array<float|false>',
'filter_var($mixed, FILTER_VALIDATE_FLOAT, FILTER_FORCE_ARRAY)',
],
[
'array<float|null>',
'filter_var($mixed, FILTER_VALIDATE_FLOAT, FILTER_FORCE_ARRAY | FILTER_NULL_ON_FAILURE)',
],
[
'array<float|null>',
'filter_var($mixed, FILTER_VALIDATE_FLOAT, $forceArrayFilter | $nullFilter)',
],
[
'float|null',
'filter_var($mixed, FILTER_VALIDATE_FLOAT ,["flags" => FILTER_NULL_ON_FAILURE])',
Expand All @@ -8086,6 +8103,10 @@ public function dataFilterVar(): array
'string|false',
'filter_var($mixed, FILTER_VALIDATE_IP)',
],
[
'string|false',
'filter_var($mixed, $filterIp)',
],
[
'string|false',
'filter_var($mixed, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4)',
Expand All @@ -8094,6 +8115,14 @@ public function dataFilterVar(): array
'string|null',
'filter_var($mixed, FILTER_VALIDATE_IP, FILTER_NULL_ON_FAILURE)',
],
[
'string|null',
'filter_var($mixed, FILTER_VALIDATE_IP, FILTER_NULL_ON_FAILURE | FILTER_FLAG_IPV4)',
],
[
'array<string|null>',
'filter_var($mixed, FILTER_VALIDATE_IP, FILTER_NULL_ON_FAILURE | FILTER_FLAG_IPV4 | FILTER_FORCE_ARRAY)',
],
[
'string|null',
'filter_var($mixed, FILTER_VALIDATE_IP ,["flags" => FILTER_NULL_ON_FAILURE])',
Expand All @@ -8114,6 +8143,10 @@ public function dataFilterVar(): array
'string|false',
'filter_var($mixed, FILTER_VALIDATE_REGEXP)',
],
[
'string|false',
'filter_var($mixed, FILTER_VALIDATE_REGEXP, ["options" => ["regexp" => "/match/"]])',
],
[
'string|null',
'filter_var($mixed, FILTER_VALIDATE_REGEXP, FILTER_NULL_ON_FAILURE)',
Expand All @@ -8122,18 +8155,38 @@ public function dataFilterVar(): array
'string|null',
'filter_var($mixed, FILTER_VALIDATE_REGEXP ,["flags" => FILTER_NULL_ON_FAILURE])',
],
[
'string|null',
'filter_var($mixed, FILTER_VALIDATE_REGEXP ,["flags" => FILTER_NULL_ON_FAILURE, "options" => ["regexp" => "/match/"]])',
],
[
'string|false',
'filter_var($mixed, FILTER_VALIDATE_URL)',
],
[
'string|false',
'filter_var($mixed, FILTER_VALIDATE_URL, $mixed)',
],
[
'string|false',
'filter_var($mixed, FILTER_VALIDATE_URL ,["flags" => $mixed])',
],
[
'string|null',
'filter_var($mixed, FILTER_VALIDATE_URL, FILTER_NULL_ON_FAILURE)',
],
[
'string|null',
'filter_var($mixed, FILTER_VALIDATE_URL, $nullFilter)',
],
[
'string|null',
'filter_var($mixed, FILTER_VALIDATE_URL ,["flags" => FILTER_NULL_ON_FAILURE])',
],
[
'string|null',
'filter_var($mixed, FILTER_VALIDATE_URL ,["flags" => $nullFilter])',
],
];
}

Expand Down
3 changes: 3 additions & 0 deletions tests/PHPStan/Analyser/data/filterVar.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
function () {
/** @var mixed $mixed */
$mixed = null;
$nullFilter = \FILTER_NULL_ON_FAILURE;
$forceArrayFilter = \FILTER_FORCE_ARRAY;
$filterIp = FILTER_VALIDATE_IP;

die;
};