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
9 changes: 9 additions & 0 deletions src/Type/Php/FilterFunctionReturnTypeHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use PHPStan\Type\Constant\ConstantIntegerType;
use PHPStan\Type\Constant\ConstantStringType;
use PHPStan\Type\ConstantScalarType;
use PHPStan\Type\ErrorType;
use PHPStan\Type\FloatType;
use PHPStan\Type\IntegerRangeType;
use PHPStan\Type\IntegerType;
Expand Down Expand Up @@ -129,6 +130,10 @@ public function getType(Type $inputType, ?Type $filterType, ?Type $flagsType): T
$inputIsArray = $inputType->isArray();
$hasRequireArrayFlag = $this->hasFlag('FILTER_REQUIRE_ARRAY', $flagsType);
if ($inputIsArray->no() && $hasRequireArrayFlag) {
if ($this->hasFlag('FILTER_THROW_ON_FAILURE', $flagsType)) {
return new ErrorType();
}

return $defaultType;
}

Expand Down Expand Up @@ -174,6 +179,10 @@ public function getType(Type $inputType, ?Type $filterType, ?Type $flagsType): T
return new ArrayType($inputArrayKeyType ?? $mixedType, $type);
}

if ($this->hasFlag('FILTER_THROW_ON_FAILURE', $flagsType)) {
$type = TypeCombinator::remove($type, $defaultType);
}

return $type;
}

Expand Down
72 changes: 72 additions & 0 deletions src/Type/Php/FilterVarThrowTypeExtension.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
<?php declare(strict_types = 1);

namespace PHPStan\Type\Php;

use PhpParser\Node\Expr\FuncCall;
use PhpParser\Node\Name;
use PHPStan\Analyser\Scope;
use PHPStan\DependencyInjection\AutowiredService;
use PHPStan\Reflection\FunctionReflection;
use PHPStan\Reflection\ReflectionProvider;
use PHPStan\Type\Constant\ConstantIntegerType;
use PHPStan\Type\Constant\ConstantStringType;
use PHPStan\Type\DynamicFunctionThrowTypeExtension;
use PHPStan\Type\ObjectType;
use PHPStan\Type\Type;

#[AutowiredService]
final class FilterVarThrowTypeExtension implements DynamicFunctionThrowTypeExtension
{

public function __construct(
private ReflectionProvider $reflectionProvider,
)
{
}

public function isFunctionSupported(
FunctionReflection $functionReflection,
): bool
{
return $functionReflection->getName() === 'filter_var'
&& $this->reflectionProvider->hasConstant(new Name\FullyQualified('FILTER_THROW_ON_FAILURE'), null);
}

public function getThrowTypeFromFunctionCall(
FunctionReflection $functionReflection,
FuncCall $funcCall,
Scope $scope,
): ?Type
{
if (!isset($funcCall->getArgs()[3])) {
return null;
}

$flagsExpr = $funcCall->getArgs()[3]->value;
$flagsType = $scope->getType($flagsExpr);

if ($flagsType->isConstantArray()->yes()) {
$flagsType = $flagsType->getOffsetValueType(new ConstantStringType('flags'));
}

$flag = $this->getConstant();

if ($flag !== null && $flagsType instanceof ConstantIntegerType && ($flagsType->getValue() & $flag) === $flag) {
return new ObjectType('Filter\FilterFailedException');
}

return null;
}

private function getConstant(): ?int
{
$constant = $this->reflectionProvider->getConstant(new Name('FILTER_THROW_ON_FAILURE'), null);
$valueType = $constant->getValueType();
if (!$valueType instanceof ConstantIntegerType) {
return null;
}

return $valueType->getValue();
}

}
26 changes: 26 additions & 0 deletions tests/PHPStan/Analyser/nsrt/filter-var-php85.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php // lint >= 8.5

declare(strict_types=1);

namespace FilterVarPHP85;

use PHPStan\TrinaryLogic;
use function PHPStan\Testing\assertType;
use function PHPStan\Testing\assertVariableCertainty;

class FilterVarPHP85
{

public function doFoo($mixed): void
{
try {
filter_var($mixed, FILTER_VALIDATE_INT, FILTER_THROW_ON_FAILURE);
$foo = 1;
} catch (\Filter\FilterFailedException $e) {
assertVariableCertainty(TrinaryLogic::createNo(), $foo);
}

assertType('int', filter_var($mixed, FILTER_VALIDATE_INT, FILTER_THROW_ON_FAILURE));
assertType('int', filter_var($mixed, FILTER_VALIDATE_INT, ['flags' => FILTER_THROW_ON_FAILURE]));
}
}
Loading