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
26 changes: 4 additions & 22 deletions src/Type/Php/FilterVarThrowTypeExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@
use PHPStan\Php\PhpVersion;
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;
Expand All @@ -22,6 +20,7 @@ final class FilterVarThrowTypeExtension implements DynamicFunctionThrowTypeExten
public function __construct(
private ReflectionProvider $reflectionProvider,
private PhpVersion $phpVersion,
private FilterFunctionReturnTypeHelper $filterFunctionReturnTypeHelper,
)
{
}
Expand All @@ -39,7 +38,7 @@ public function getThrowTypeFromFunctionCall(
Scope $scope,
): ?Type
{
if (!isset($funcCall->getArgs()[3])) {
if (!isset($funcCall->getArgs()[2])) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thats a interessting bug.

seems the tests in #4495 were not sufficient

@canvural could you double check?

return null;
}

Expand All @@ -50,31 +49,14 @@ public function getThrowTypeFromFunctionCall(
return null;
}

$flagsExpr = $funcCall->getArgs()[3]->value;
$flagsExpr = $funcCall->getArgs()[2]->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) {
if (!$this->filterFunctionReturnTypeHelper->hasFlag('FILTER_THROW_ON_FAILURE', $flagsType)->no()) {
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();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,12 @@ public function testBug4863(): void
$this->analyse([__DIR__ . '/data/bug-4863.php'], []);
}

#[RequiresPhp('>= 8.5.0')]
public function testFilterVarThrowOnFailure(): void
{
$this->analyse([__DIR__ . '/data/filter-var-throw-on-failure.php'], []);
}

#[RequiresPhp('>= 8.0.0')]
public function testBug5866(): void
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php // lint >= 8.5

declare(strict_types = 1);

namespace FilterVarThrowOnFailure;

function validateMac(mixed $value): void
{
try {
filter_var($value, FILTER_VALIDATE_MAC, FILTER_THROW_ON_FAILURE);
} catch (\Filter\FilterFailedException) {
}
}

function validateMacWithFlagsArray(mixed $value): void
{
try {
filter_var($value, FILTER_VALIDATE_MAC, ['flags' => FILTER_THROW_ON_FAILURE]);
} catch (\Filter\FilterFailedException) {
}
}

function validateInt(mixed $value): void
{
try {
filter_var($value, FILTER_VALIDATE_INT, FILTER_THROW_ON_FAILURE);
} catch (\Filter\FilterFailedException) {
}
}
Loading