From f453c9df153bb0358c4e2ed8c591543dcc35eef7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jaroslav=20Hansl=C3=ADk?= Date: Wed, 11 Dec 2024 21:54:12 +0100 Subject: [PATCH] Added more return type hints tests for ReflectionFunctionAbstract --- .../Php71NullableReturnTypeDeclarations.php | 13 ---- .../Fixture/Php7ReturnTypeDeclarations.php | 25 -------- test/unit/Fixture/ReturnTypeDeclarations.php | 45 +++++++++++++ .../ReflectionFunctionAbstractTest.php | 64 ++++++------------- 4 files changed, 65 insertions(+), 82 deletions(-) delete mode 100644 test/unit/Fixture/Php71NullableReturnTypeDeclarations.php delete mode 100644 test/unit/Fixture/Php7ReturnTypeDeclarations.php create mode 100644 test/unit/Fixture/ReturnTypeDeclarations.php diff --git a/test/unit/Fixture/Php71NullableReturnTypeDeclarations.php b/test/unit/Fixture/Php71NullableReturnTypeDeclarations.php deleted file mode 100644 index 2bdb80ca5..000000000 --- a/test/unit/Fixture/Php71NullableReturnTypeDeclarations.php +++ /dev/null @@ -1,13 +0,0 @@ -getLocatedSource()); } - /** @return list */ + /** @return list */ public static function returnTypeFunctionProvider(): array { return [ @@ -480,46 +480,30 @@ public static function returnTypeFunctionProvider(): array ['returnsNull', 'null'], ['returnsObject', stdClass::class], ['returnsVoid', 'void'], + ['returnsVoid', 'void'], + ['returnsNothing', null], + ['returnsUnion', 'int|string'], + ['returnsIntersection', 'DateTime&DateTimeImmutable'], ]; } #[DataProvider('returnTypeFunctionProvider')] - public function testGetReturnTypeWithDeclaredType(string $functionToReflect, string $expectedType): void + public function testGetReturnTypeWithDeclaredType(string $functionToReflect, string|null $expectedType): void { $functionInfo = (new DefaultReflector( - new SingleFileSourceLocator(__DIR__ . '/../Fixture/Php7ReturnTypeDeclarations.php', $this->astLocator), + new SingleFileSourceLocator(__DIR__ . '/../Fixture/ReturnTypeDeclarations.php', $this->astLocator), ))->reflectFunction($functionToReflect); - $reflectionType = $functionInfo->getReturnType(); - self::assertInstanceOf(ReflectionType::class, $reflectionType); - self::assertSame($expectedType, (string) $reflectionType); - } - - public function testGetReturnTypeReturnsNullWhenTypeIsNotDeclared(): void - { - $functionInfo = (new DefaultReflector( - new SingleFileSourceLocator(__DIR__ . '/../Fixture/Php7ReturnTypeDeclarations.php', $this->astLocator), - ))->reflectFunction('returnsNothing'); - - self::assertNull($functionInfo->getReturnType()); - } - - public function testHasReturnTypeWhenTypeDeclared(): void - { - $functionInfo = (new DefaultReflector( - new SingleFileSourceLocator(__DIR__ . '/../Fixture/Php7ReturnTypeDeclarations.php', $this->astLocator), - ))->reflectFunction('returnsString'); + if ($expectedType === null) { + self::assertFalse($functionInfo->hasReturnType()); + self::assertNull($functionInfo->getReturnType()); + } else { + self::assertTrue($functionInfo->hasReturnType()); - self::assertTrue($functionInfo->hasReturnType()); - } - - public function testHasReturnTypeWhenTypeIsNotDeclared(): void - { - $functionInfo = (new DefaultReflector( - new SingleFileSourceLocator(__DIR__ . '/../Fixture/Php7ReturnTypeDeclarations.php', $this->astLocator), - ))->reflectFunction('returnsNothing'); - - self::assertFalse($functionInfo->hasReturnType()); + $reflectionType = $functionInfo->getReturnType(); + self::assertInstanceOf(ReflectionType::class, $reflectionType); + self::assertSame($expectedType, (string) $reflectionType); + } } /** @return list */ @@ -536,7 +520,7 @@ public static function nullableReturnTypeFunctionProvider(): array public function testGetNullableReturnTypeWithDeclaredType(string $functionToReflect, string $expectedType): void { $functionInfo = (new DefaultReflector( - new SingleFileSourceLocator(__DIR__ . '/../Fixture/Php71NullableReturnTypeDeclarations.php', $this->astLocator), + new SingleFileSourceLocator(__DIR__ . '/../Fixture/ReturnTypeDeclarations.php', $this->astLocator), ))->reflectFunction($functionToReflect); $reflectionType = $functionInfo->getReturnType(); @@ -557,11 +541,13 @@ public function testHasTentativeReturnType(): void public function testHasNotTentativeReturnType(): void { $functionInfo = (new DefaultReflector( - new SingleFileSourceLocator(__DIR__ . '/../Fixture/Php7ReturnTypeDeclarations.php', $this->astLocator), + new SingleFileSourceLocator(__DIR__ . '/../Fixture/ReturnTypeDeclarations.php', $this->astLocator), ))->reflectFunction('returnsString'); self::assertFalse($functionInfo->hasTentativeReturnType()); + self::assertNull($functionInfo->getTentativeReturnType()); self::assertTrue($functionInfo->hasReturnType()); + self::assertNotNull($functionInfo->getReturnType()); } public function testGetTentativeReturnType(): void @@ -576,16 +562,6 @@ public function testGetTentativeReturnType(): void self::assertNull($methodInfo->getReturnType()); } - public function testNoTentativeReturnType(): void - { - $functionInfo = (new DefaultReflector( - new SingleFileSourceLocator(__DIR__ . '/../Fixture/Php7ReturnTypeDeclarations.php', $this->astLocator), - ))->reflectFunction('returnsString'); - - self::assertNull($functionInfo->getTentativeReturnType()); - self::assertNotNull($functionInfo->getReturnType()); - } - #[DataProvider('deprecatedDocCommentsProvider')] public function testFunctionsCanBeDeprecated(string $comment): void {