From 21775d733cf19692b4d3b9adce46b316af592e3f Mon Sep 17 00:00:00 2001 From: Jan Nedbal Date: Mon, 20 Nov 2023 16:38:15 +0100 Subject: [PATCH 1/3] BaseExpressionDynamicReturnTypeExtension: support only add, addMultiple --- ...seExpressionDynamicReturnTypeExtension.php | 3 +- ...pressionDynamicReturnTypeExtensionTest.php | 35 +++++++++++++++++++ .../data/QueryResult/baseExpressionAndOr.php | 13 +++++++ 3 files changed, 50 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..0b96ac0c --- /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..ccdf8812 --- /dev/null +++ b/tests/Type/Doctrine/data/QueryResult/baseExpressionAndOr.php @@ -0,0 +1,13 @@ +count(); +assertType("int<0, max>", $count1); + +$modifiedAnd = $and->add('a = b'); +assertType("Doctrine\ORM\Query\Expr\Andx", $modifiedAnd); From cdacc6097fc5259b36ccddc74de5e3414533fb82 Mon Sep 17 00:00:00 2001 From: Jan Nedbal Date: Mon, 20 Nov 2023 16:42:55 +0100 Subject: [PATCH 2/3] Fix namespace --- .../Expr/BaseExpressionDynamicReturnTypeExtensionTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/Type/Doctrine/QueryBuilder/Expr/BaseExpressionDynamicReturnTypeExtensionTest.php b/tests/Type/Doctrine/QueryBuilder/Expr/BaseExpressionDynamicReturnTypeExtensionTest.php index 0b96ac0c..0bc668b2 100644 --- a/tests/Type/Doctrine/QueryBuilder/Expr/BaseExpressionDynamicReturnTypeExtensionTest.php +++ b/tests/Type/Doctrine/QueryBuilder/Expr/BaseExpressionDynamicReturnTypeExtensionTest.php @@ -1,6 +1,6 @@ Date: Mon, 20 Nov 2023 16:45:55 +0100 Subject: [PATCH 3/3] test even __toString to be generic --- tests/Type/Doctrine/data/QueryResult/baseExpressionAndOr.php | 3 +++ 1 file changed, 3 insertions(+) diff --git a/tests/Type/Doctrine/data/QueryResult/baseExpressionAndOr.php b/tests/Type/Doctrine/data/QueryResult/baseExpressionAndOr.php index ccdf8812..9fcf71a3 100644 --- a/tests/Type/Doctrine/data/QueryResult/baseExpressionAndOr.php +++ b/tests/Type/Doctrine/data/QueryResult/baseExpressionAndOr.php @@ -11,3 +11,6 @@ $modifiedAnd = $and->add('a = b'); assertType("Doctrine\ORM\Query\Expr\Andx", $modifiedAnd); + +$string = $and->__toString(); +assertType("string", $string);