From 68fc764ee1f5fa0930d3c31b75dc6f5dab896df4 Mon Sep 17 00:00:00 2001 From: Jan Nedbal Date: Tue, 21 Nov 2023 10:40:01 +0100 Subject: [PATCH] BaseExpressionDynamicReturnTypeExtension: support only add, addMultiple --- ...seExpressionDynamicReturnTypeExtension.php | 3 +- ...pressionDynamicReturnTypeExtensionTest.php | 35 +++++++++++++++++++ .../data/QueryResult/baseExpressionAndOr.php | 16 +++++++++ 3 files changed, 53 insertions(+), 1 deletion(-) create mode 100644 tests/Type/Doctrine/QueryBuilder/Expr/BaseExpressionDynamicReturnTypeExtensionTest.php create mode 100644 tests/Type/Doctrine/data/QueryResult/baseExpressionAndOr.php diff --git a/src/Type/Doctrine/QueryBuilder/Expr/BaseExpressionDynamicReturnTypeExtension.php b/src/Type/Doctrine/QueryBuilder/Expr/BaseExpressionDynamicReturnTypeExtension.php index 1534f11c..8564d374 100644 --- a/src/Type/Doctrine/QueryBuilder/Expr/BaseExpressionDynamicReturnTypeExtension.php +++ b/src/Type/Doctrine/QueryBuilder/Expr/BaseExpressionDynamicReturnTypeExtension.php @@ -11,6 +11,7 @@ use PHPStan\Type\DynamicMethodReturnTypeExtension; use PHPStan\Type\Type; use function get_class; +use function in_array; use function is_object; use function method_exists; @@ -34,7 +35,7 @@ public function getClass(): string public function isMethodSupported(MethodReflection $methodReflection): bool { - return true; + return in_array($methodReflection->getName(), ['add', 'addMultiple'], true); } public function getTypeFromMethodCall(MethodReflection $methodReflection, MethodCall $methodCall, Scope $scope): Type diff --git a/tests/Type/Doctrine/QueryBuilder/Expr/BaseExpressionDynamicReturnTypeExtensionTest.php b/tests/Type/Doctrine/QueryBuilder/Expr/BaseExpressionDynamicReturnTypeExtensionTest.php new file mode 100644 index 00000000..0bc668b2 --- /dev/null +++ b/tests/Type/Doctrine/QueryBuilder/Expr/BaseExpressionDynamicReturnTypeExtensionTest.php @@ -0,0 +1,35 @@ + */ + public function dataFileAsserts(): iterable + { + yield from $this->gatherAssertTypes(__DIR__ . '/../../data/QueryResult/baseExpressionAndOr.php'); + } + + /** + * @dataProvider dataFileAsserts + * @param mixed ...$args + */ + public function testFileAsserts( + string $assertType, + string $file, + ...$args + ): void + { + $this->assertFileAsserts($assertType, $file, ...$args); + } + + /** @return string[] */ + public static function getAdditionalConfigFiles(): array + { + return [__DIR__ . '/../../data/QueryResult/config.neon']; + } + +} diff --git a/tests/Type/Doctrine/data/QueryResult/baseExpressionAndOr.php b/tests/Type/Doctrine/data/QueryResult/baseExpressionAndOr.php new file mode 100644 index 00000000..9fcf71a3 --- /dev/null +++ b/tests/Type/Doctrine/data/QueryResult/baseExpressionAndOr.php @@ -0,0 +1,16 @@ +count(); +assertType("int<0, max>", $count1); + +$modifiedAnd = $and->add('a = b'); +assertType("Doctrine\ORM\Query\Expr\Andx", $modifiedAnd); + +$string = $and->__toString(); +assertType("string", $string);