From 9952fff704cba36adf970da03283e023f120204f Mon Sep 17 00:00:00 2001 From: USAMI Kenta Date: Mon, 4 Nov 2024 02:33:31 +0900 Subject: [PATCH 1/3] Rename ArrayFilterFunctionReturnTypeExtension to ArrayFilterFunctionReturnTypeHelper --- ...nTypeExtension.php => ArrayFilterFunctionReturnTypeHelper.php} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename src/Type/Php/{ArrayFilterFunctionReturnTypeExtension.php => ArrayFilterFunctionReturnTypeHelper.php} (100%) diff --git a/src/Type/Php/ArrayFilterFunctionReturnTypeExtension.php b/src/Type/Php/ArrayFilterFunctionReturnTypeHelper.php similarity index 100% rename from src/Type/Php/ArrayFilterFunctionReturnTypeExtension.php rename to src/Type/Php/ArrayFilterFunctionReturnTypeHelper.php From e22de3513c555d845a3f3fb909cc43e957e78f43 Mon Sep 17 00:00:00 2001 From: USAMI Kenta Date: Mon, 4 Nov 2024 02:35:27 +0900 Subject: [PATCH 2/3] Split ArrayFilterFunctionReturnTypeExtension to Helper --- conf/config.neon | 3 ++ ...ArrayFilterFunctionReturnTypeExtension.php | 32 +++++++++++++++++++ .../ArrayFilterFunctionReturnTypeHelper.php | 17 ++-------- 3 files changed, 38 insertions(+), 14 deletions(-) create mode 100644 src/Type/Php/ArrayFilterFunctionReturnTypeExtension.php diff --git a/conf/config.neon b/conf/config.neon index 7c88f87a31..f9d8ca018e 100644 --- a/conf/config.neon +++ b/conf/config.neon @@ -1207,6 +1207,9 @@ services: tags: - phpstan.broker.dynamicFunctionReturnTypeExtension + - + class: PHPStan\Type\Php\ArrayFilterFunctionReturnTypeHelper + - class: PHPStan\Type\Php\ArrayFilterFunctionReturnTypeExtension tags: diff --git a/src/Type/Php/ArrayFilterFunctionReturnTypeExtension.php b/src/Type/Php/ArrayFilterFunctionReturnTypeExtension.php new file mode 100644 index 0000000000..62dc52abf0 --- /dev/null +++ b/src/Type/Php/ArrayFilterFunctionReturnTypeExtension.php @@ -0,0 +1,32 @@ +getName() === 'array_filter'; + } + + public function getTypeFromFunctionCall(FunctionReflection $functionReflection, FuncCall $functionCall, Scope $scope): Type + { + $arrayArg = $functionCall->getArgs()[0]->value ?? null; + $callbackArg = $functionCall->getArgs()[1]->value ?? null; + $flagArg = $functionCall->getArgs()[2]->value ?? null; + + return $this->arrayFilterFunctionReturnTypeHelper->getType($scope, $arrayArg, $callbackArg, $flagArg); + } + +} diff --git a/src/Type/Php/ArrayFilterFunctionReturnTypeHelper.php b/src/Type/Php/ArrayFilterFunctionReturnTypeHelper.php index 4672fd1a96..5291652129 100644 --- a/src/Type/Php/ArrayFilterFunctionReturnTypeHelper.php +++ b/src/Type/Php/ArrayFilterFunctionReturnTypeHelper.php @@ -15,7 +15,6 @@ use PhpParser\Node\Stmt\Return_; use PHPStan\Analyser\MutatingScope; use PHPStan\Analyser\Scope; -use PHPStan\Reflection\FunctionReflection; use PHPStan\Reflection\ReflectionProvider; use PHPStan\ShouldNotHappenException; use PHPStan\Type\ArrayType; @@ -24,7 +23,6 @@ use PHPStan\Type\Constant\ConstantArrayTypeBuilder; use PHPStan\Type\Constant\ConstantBooleanType; use PHPStan\Type\Constant\ConstantIntegerType; -use PHPStan\Type\DynamicFunctionReturnTypeExtension; use PHPStan\Type\ErrorType; use PHPStan\Type\MixedType; use PHPStan\Type\NeverType; @@ -40,7 +38,7 @@ use function sprintf; use function substr; -final class ArrayFilterFunctionReturnTypeExtension implements DynamicFunctionReturnTypeExtension +final class ArrayFilterFunctionReturnTypeHelper { private const USE_BOTH = 1; @@ -51,17 +49,8 @@ public function __construct(private ReflectionProvider $reflectionProvider) { } - public function isFunctionSupported(FunctionReflection $functionReflection): bool + public function getType(Scope $scope, ?Expr $arrayArg, ?Expr $callbackArg, ?Expr $flagArg): Type { - return $functionReflection->getName() === 'array_filter'; - } - - public function getTypeFromFunctionCall(FunctionReflection $functionReflection, FuncCall $functionCall, Scope $scope): Type - { - $arrayArg = $functionCall->getArgs()[0]->value ?? null; - $callbackArg = $functionCall->getArgs()[1]->value ?? null; - $flagArg = $functionCall->getArgs()[2]->value ?? null; - if ($arrayArg === null) { return new ArrayType(new MixedType(), new MixedType()); } @@ -151,7 +140,7 @@ public function getTypeFromFunctionCall(FunctionReflection $functionReflection, return new ArrayType($keyType, $itemType); } - public function removeFalsey(Type $type): Type + private function removeFalsey(Type $type): Type { $falseyTypes = StaticTypeFactory::falsey(); From ffc3eccdfe74e558c7f38530acdc4aec9f3719c9 Mon Sep 17 00:00:00 2001 From: USAMI Kenta Date: Tue, 5 Nov 2024 23:35:57 +0900 Subject: [PATCH 3/3] Regenerate baseline --- phpstan-baseline.neon | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/phpstan-baseline.neon b/phpstan-baseline.neon index 1521bbb1a0..498207dbe7 100644 --- a/phpstan-baseline.neon +++ b/phpstan-baseline.neon @@ -1305,7 +1305,7 @@ parameters: - message: "#^Doing instanceof PHPStan\\\\Type\\\\Constant\\\\ConstantBooleanType is error\\-prone and deprecated\\. Use Type\\:\\:isTrue\\(\\) or Type\\:\\:isFalse\\(\\) instead\\.$#" count: 1 - path: src/Type/Php/ArrayFilterFunctionReturnTypeExtension.php + path: src/Type/Php/ArrayFilterFunctionReturnTypeHelper.php - message: "#^Doing instanceof PHPStan\\\\Type\\\\Constant\\\\ConstantStringType is error\\-prone and deprecated\\. Use Type\\:\\:getConstantStrings\\(\\) instead\\.$#"