Skip to content

Commit

Permalink
Handle invalid input type in filter_var() better
Browse files Browse the repository at this point in the history
  • Loading branch information
herndlm committed Dec 29, 2022
1 parent e5c3aea commit f9f17d5
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/Type/Php/FilterVarDynamicReturnTypeExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,11 @@ public function getTypeFromFunctionCall(
$defaultType = $this->hasFlag($this->getConstant('FILTER_NULL_ON_FAILURE'), $flagsType)
? new NullType()
: new ConstantBooleanType(false);

if ($inputType->isScalar()->no() && $inputType->isNull()->no()) {
return $defaultType;
}

$exactType = $this->determineExactType($inputType, $filterValue, $defaultType, $flagsType);
$type = $exactType ?? $this->getFilterTypeMap()[$filterValue] ?? $mixedType;

Expand Down
10 changes: 10 additions & 0 deletions tests/PHPStan/Analyser/data/filter-var.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace FilterVar;

use stdClass;
use function PHPStan\Testing\assertType;

class FilterVar
Expand All @@ -18,6 +19,15 @@ public function doFoo($mixed): void
assertType('array<false>', filter_var(false, FILTER_VALIDATE_BOOLEAN, FILTER_FORCE_ARRAY | FILTER_NULL_ON_FAILURE));
}

/** @param resource $resource */
public function invalidInput(array $arr, object $object, $resource): void
{
assertType('false', filter_var($arr));
assertType('false', filter_var($object));
assertType('false', filter_var($resource));
assertType('null', filter_var(new stdClass(), FILTER_DEFAULT, FILTER_NULL_ON_FAILURE));
}

public function intToInt(int $int, array $options): void
{
assertType('int', filter_var($int, FILTER_VALIDATE_INT));
Expand Down

0 comments on commit f9f17d5

Please sign in to comment.