From da906a905f2c4abb50323e5e1814a9f991bc74b6 Mon Sep 17 00:00:00 2001 From: Markus Staab Date: Fri, 18 Mar 2022 10:39:17 +0100 Subject: [PATCH 01/18] added failling value-of test --- .../PhpDoc/IncompatiblePhpDocTypeRuleTest.php | 12 +++++++++ .../Rules/PhpDoc/data/value-of-enum.php | 26 +++++++++++++++++++ 2 files changed, 38 insertions(+) create mode 100644 tests/PHPStan/Rules/PhpDoc/data/value-of-enum.php diff --git a/tests/PHPStan/Rules/PhpDoc/IncompatiblePhpDocTypeRuleTest.php b/tests/PHPStan/Rules/PhpDoc/IncompatiblePhpDocTypeRuleTest.php index d7cf8d5470..614c609a72 100644 --- a/tests/PHPStan/Rules/PhpDoc/IncompatiblePhpDocTypeRuleTest.php +++ b/tests/PHPStan/Rules/PhpDoc/IncompatiblePhpDocTypeRuleTest.php @@ -204,4 +204,16 @@ public function testEnums(): void ]); } + public function testValueOfEnum(): void + { + if (PHP_VERSION_ID < 80100) { + $this->markTestSkipped('This test needs PHP 8.1'); + } + + $this->analyse( + [__DIR__ . '/data/value-of-enum.php'], + [] + ); + } + } diff --git a/tests/PHPStan/Rules/PhpDoc/data/value-of-enum.php b/tests/PHPStan/Rules/PhpDoc/data/value-of-enum.php new file mode 100644 index 0000000000..97c001e970 --- /dev/null +++ b/tests/PHPStan/Rules/PhpDoc/data/value-of-enum.php @@ -0,0 +1,26 @@ += 8.1 + +declare(strict_types=1); + +namespace Bug6775; + +enum Country: string +{ + case NL = 'The Netherlands'; + case US = 'United States'; +} + +class Foo { + /** + * @param value-of $countryName + */ + function hello(string $countryName): void + { + // ... + } + + function doFoo() { + $this->hello(Country::NL); + } +} + From 698eb7a075d03c5d1b82e56959985e171104ed23 Mon Sep 17 00:00:00 2001 From: Markus Staab Date: Fri, 18 Mar 2022 10:47:06 +0100 Subject: [PATCH 02/18] support value-of --- src/PhpDoc/TypeNodeResolver.php | 8 ++++++++ .../Rules/PhpDoc/IncompatiblePhpDocTypeRuleTest.php | 2 ++ tests/PHPStan/Rules/PhpDoc/data/value-of-enum.php | 2 +- 3 files changed, 11 insertions(+), 1 deletion(-) diff --git a/src/PhpDoc/TypeNodeResolver.php b/src/PhpDoc/TypeNodeResolver.php index be7f04e1e4..d7b7abf0e3 100644 --- a/src/PhpDoc/TypeNodeResolver.php +++ b/src/PhpDoc/TypeNodeResolver.php @@ -525,6 +525,14 @@ private function resolveGenericTypeNode(GenericTypeNode $typeNode, NameScope $na return new ErrorType(); } elseif ($mainTypeName === 'value-of') { if (count($genericTypes) === 1) { // value-of + if ($genericTypes[0] instanceof ObjectType) { + $classReflection = $genericTypes[0]->getClassReflection(); + + if ($classReflection->isBackedEnum()) { + return $classReflection->getBackedEnumType(); + } + } + return $genericTypes[0]->getIterableValueType(); } diff --git a/tests/PHPStan/Rules/PhpDoc/IncompatiblePhpDocTypeRuleTest.php b/tests/PHPStan/Rules/PhpDoc/IncompatiblePhpDocTypeRuleTest.php index 614c609a72..5af17a4f55 100644 --- a/tests/PHPStan/Rules/PhpDoc/IncompatiblePhpDocTypeRuleTest.php +++ b/tests/PHPStan/Rules/PhpDoc/IncompatiblePhpDocTypeRuleTest.php @@ -210,6 +210,8 @@ public function testValueOfEnum(): void $this->markTestSkipped('This test needs PHP 8.1'); } + require __DIR__ . '/data/value-of-enum.php'; + $this->analyse( [__DIR__ . '/data/value-of-enum.php'], [] diff --git a/tests/PHPStan/Rules/PhpDoc/data/value-of-enum.php b/tests/PHPStan/Rules/PhpDoc/data/value-of-enum.php index 97c001e970..4760be931c 100644 --- a/tests/PHPStan/Rules/PhpDoc/data/value-of-enum.php +++ b/tests/PHPStan/Rules/PhpDoc/data/value-of-enum.php @@ -2,7 +2,7 @@ declare(strict_types=1); -namespace Bug6775; +namespace ValueOfEnum; enum Country: string { From 60398d47020e8eaf76c6da69e9b6f95d40fd32d9 Mon Sep 17 00:00:00 2001 From: Markus Staab Date: Fri, 18 Mar 2022 10:50:29 +0100 Subject: [PATCH 03/18] cs --- tests/PHPStan/Rules/PhpDoc/IncompatiblePhpDocTypeRuleTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/PHPStan/Rules/PhpDoc/IncompatiblePhpDocTypeRuleTest.php b/tests/PHPStan/Rules/PhpDoc/IncompatiblePhpDocTypeRuleTest.php index 5af17a4f55..2db2e02036 100644 --- a/tests/PHPStan/Rules/PhpDoc/IncompatiblePhpDocTypeRuleTest.php +++ b/tests/PHPStan/Rules/PhpDoc/IncompatiblePhpDocTypeRuleTest.php @@ -214,7 +214,7 @@ public function testValueOfEnum(): void $this->analyse( [__DIR__ . '/data/value-of-enum.php'], - [] + [], ); } From 2c1464f9daaeaaef8bbd6a0cbe7e75cb844f3e35 Mon Sep 17 00:00:00 2001 From: Markus Staab Date: Fri, 18 Mar 2022 10:56:47 +0100 Subject: [PATCH 04/18] add null check --- src/PhpDoc/TypeNodeResolver.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/PhpDoc/TypeNodeResolver.php b/src/PhpDoc/TypeNodeResolver.php index d7b7abf0e3..9c21bf2dcc 100644 --- a/src/PhpDoc/TypeNodeResolver.php +++ b/src/PhpDoc/TypeNodeResolver.php @@ -528,7 +528,7 @@ private function resolveGenericTypeNode(GenericTypeNode $typeNode, NameScope $na if ($genericTypes[0] instanceof ObjectType) { $classReflection = $genericTypes[0]->getClassReflection(); - if ($classReflection->isBackedEnum()) { + if ($classReflection !== null && $classReflection->isBackedEnum()) { return $classReflection->getBackedEnumType(); } } From 13f4350b31f866cf4d409ad004ba2e7d5f8a2abd Mon Sep 17 00:00:00 2001 From: Markus Staab Date: Fri, 18 Mar 2022 11:02:57 +0100 Subject: [PATCH 05/18] fix --- src/PhpDoc/TypeNodeResolver.php | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/PhpDoc/TypeNodeResolver.php b/src/PhpDoc/TypeNodeResolver.php index 9c21bf2dcc..ea4a268c7f 100644 --- a/src/PhpDoc/TypeNodeResolver.php +++ b/src/PhpDoc/TypeNodeResolver.php @@ -529,7 +529,13 @@ private function resolveGenericTypeNode(GenericTypeNode $typeNode, NameScope $na $classReflection = $genericTypes[0]->getClassReflection(); if ($classReflection !== null && $classReflection->isBackedEnum()) { - return $classReflection->getBackedEnumType(); + $enumType = $classReflection->getBackedEnumType(); + + if ($enumType !== null) { + return $enumType; + } + + return new ErrorType(); } } From 3ce68403702ba5812d212cbf7af5ac23782d69c8 Mon Sep 17 00:00:00 2001 From: Markus Staab Date: Fri, 18 Mar 2022 13:54:02 +0100 Subject: [PATCH 06/18] work on PR feedback --- src/PhpDoc/TypeNodeResolver.php | 16 +++++++------- .../PhpDoc/IncompatiblePhpDocTypeRuleTest.php | 14 ++++++++---- .../Rules/PhpDoc/data/value-of-enum.php | 22 +++++++++++++++++++ 3 files changed, 40 insertions(+), 12 deletions(-) diff --git a/src/PhpDoc/TypeNodeResolver.php b/src/PhpDoc/TypeNodeResolver.php index ea4a268c7f..58ebeef227 100644 --- a/src/PhpDoc/TypeNodeResolver.php +++ b/src/PhpDoc/TypeNodeResolver.php @@ -525,17 +525,17 @@ private function resolveGenericTypeNode(GenericTypeNode $typeNode, NameScope $na return new ErrorType(); } elseif ($mainTypeName === 'value-of') { if (count($genericTypes) === 1) { // value-of - if ($genericTypes[0] instanceof ObjectType) { - $classReflection = $genericTypes[0]->getClassReflection(); + if ($genericTypes[0] instanceof TypeWithClassName) { + if ($this->getReflectionProvider()->hasClass($genericTypes[0]->getClassName())) { + $classReflection = $this->getReflectionProvider()->getClass($genericTypes[0]->getClassName()); - if ($classReflection !== null && $classReflection->isBackedEnum()) { - $enumType = $classReflection->getBackedEnumType(); + if ($classReflection->isBackedEnum()) { + $enumType = $classReflection->getBackedEnumType(); - if ($enumType !== null) { - return $enumType; + if ($enumType !== null) { + return $enumType; + } } - - return new ErrorType(); } } diff --git a/tests/PHPStan/Rules/PhpDoc/IncompatiblePhpDocTypeRuleTest.php b/tests/PHPStan/Rules/PhpDoc/IncompatiblePhpDocTypeRuleTest.php index 2db2e02036..63f411fa06 100644 --- a/tests/PHPStan/Rules/PhpDoc/IncompatiblePhpDocTypeRuleTest.php +++ b/tests/PHPStan/Rules/PhpDoc/IncompatiblePhpDocTypeRuleTest.php @@ -212,10 +212,16 @@ public function testValueOfEnum(): void require __DIR__ . '/data/value-of-enum.php'; - $this->analyse( - [__DIR__ . '/data/value-of-enum.php'], - [], - ); + $this->analyse([__DIR__ . '/data/value-of-enum.php'], [ + [ + 'PHPDoc tag @param for parameter $shouldError with type string is incompatible with native type int.', + 31, + ], + [ + 'PHPDoc tag @param for parameter $shouldError with type int is incompatible with native type string.', + 38, + ] + ]); } } diff --git a/tests/PHPStan/Rules/PhpDoc/data/value-of-enum.php b/tests/PHPStan/Rules/PhpDoc/data/value-of-enum.php index 4760be931c..2b4b5d47e7 100644 --- a/tests/PHPStan/Rules/PhpDoc/data/value-of-enum.php +++ b/tests/PHPStan/Rules/PhpDoc/data/value-of-enum.php @@ -10,6 +10,12 @@ enum Country: string case US = 'United States'; } +enum CountryNo: int +{ + case NL = 1; + case US = 2; +} + class Foo { /** * @param value-of $countryName @@ -19,8 +25,24 @@ function hello(string $countryName): void // ... } + /** + * @param value-of $shouldError + */ + function helloError(int $shouldError): void + { + // ... + } + /** + * @param value-of $shouldError + */ + function helloError2(string $shouldError): void + { + // ... + } + function doFoo() { $this->hello(Country::NL); + $this->hello(CountryNo::NL); } } From b6bce3da5a32ac09e309e001b2b6f5f87c338075 Mon Sep 17 00:00:00 2001 From: Markus Staab Date: Fri, 18 Mar 2022 13:55:33 +0100 Subject: [PATCH 07/18] cs --- tests/PHPStan/Rules/PhpDoc/IncompatiblePhpDocTypeRuleTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/PHPStan/Rules/PhpDoc/IncompatiblePhpDocTypeRuleTest.php b/tests/PHPStan/Rules/PhpDoc/IncompatiblePhpDocTypeRuleTest.php index 63f411fa06..2783431de3 100644 --- a/tests/PHPStan/Rules/PhpDoc/IncompatiblePhpDocTypeRuleTest.php +++ b/tests/PHPStan/Rules/PhpDoc/IncompatiblePhpDocTypeRuleTest.php @@ -220,7 +220,7 @@ public function testValueOfEnum(): void [ 'PHPDoc tag @param for parameter $shouldError with type int is incompatible with native type string.', 38, - ] + ], ]); } From f04b4cdede932445bd4c1ca9cbd799a9631192f3 Mon Sep 17 00:00:00 2001 From: Markus Staab Date: Fri, 18 Mar 2022 14:20:43 +0100 Subject: [PATCH 08/18] remove unrelated line --- tests/PHPStan/Rules/PhpDoc/data/value-of-enum.php | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/PHPStan/Rules/PhpDoc/data/value-of-enum.php b/tests/PHPStan/Rules/PhpDoc/data/value-of-enum.php index 2b4b5d47e7..aa467170c7 100644 --- a/tests/PHPStan/Rules/PhpDoc/data/value-of-enum.php +++ b/tests/PHPStan/Rules/PhpDoc/data/value-of-enum.php @@ -42,7 +42,6 @@ function helloError2(string $shouldError): void function doFoo() { $this->hello(Country::NL); - $this->hello(CountryNo::NL); } } From d1e9f14dc885da1fdccaca3f0426ec779cc38f53 Mon Sep 17 00:00:00 2001 From: Markus Staab Date: Fri, 18 Mar 2022 14:22:39 +0100 Subject: [PATCH 09/18] added testcoverage in CallMethodsRuleTest --- .../Rules/Methods/CallMethodsRuleTest.php | 12 ++++++ .../Methods/data/call-method-in-enum.php | 40 +++++++++++++++++++ 2 files changed, 52 insertions(+) diff --git a/tests/PHPStan/Rules/Methods/CallMethodsRuleTest.php b/tests/PHPStan/Rules/Methods/CallMethodsRuleTest.php index e973f044e9..db7a817787 100644 --- a/tests/PHPStan/Rules/Methods/CallMethodsRuleTest.php +++ b/tests/PHPStan/Rules/Methods/CallMethodsRuleTest.php @@ -2287,6 +2287,18 @@ public function testEnums(): void 'Call to an undefined method CallMethodInEnum\Bar::doNonexistent().', 22, ], + [ + 'Parameter #1 $countryName of method CallMethodInEnum\FooCall::hello() expects string, CallMethodInEnum\CountryNo::NL given.', + 63, + ], + [ + 'Parameter #1 $countryMap of method CallMethodInEnum\FooCall::helloArray() expects array, array given.', + 67, + ], + [ + 'Parameter #1 $countryMap of method CallMethodInEnum\FooCall::helloArray() expects array, array given.', + 70, + ], ]); } diff --git a/tests/PHPStan/Rules/Methods/data/call-method-in-enum.php b/tests/PHPStan/Rules/Methods/data/call-method-in-enum.php index c7a2393a67..fe40ea7b00 100644 --- a/tests/PHPStan/Rules/Methods/data/call-method-in-enum.php +++ b/tests/PHPStan/Rules/Methods/data/call-method-in-enum.php @@ -30,3 +30,43 @@ enum Bar use FooTrait; } + +enum Country: string +{ + case NL = 'The Netherlands'; + case US = 'United States'; +} + +enum CountryNo: int +{ + case NL = 1; + case US = 2; +} + +class FooCall { + /** + * @param value-of $countryName + */ + function hello(string $countryName): void + { + // ... + } + + /** + * @param array, bool> $countryMap + */ + function helloArray(array $countryMap): void { + // ... + } + + function doFooArray() { + $this->hello(CountryNo::NL); + + // should not be allowed, 'abc' does not match value-of + $this->helloArray(['abc' => true]); + $this->helloArray(['abc' => 123]); + + // wrong key type + $this->helloArray([true]); + } +} From 54c2e3b82bdfdbc062089185f9416bf46ef7d89e Mon Sep 17 00:00:00 2001 From: Markus Staab Date: Fri, 18 Mar 2022 16:19:10 +0100 Subject: [PATCH 10/18] adjust test to better match the test-file-name --- tests/PHPStan/Rules/Methods/data/call-method-in-enum.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/PHPStan/Rules/Methods/data/call-method-in-enum.php b/tests/PHPStan/Rules/Methods/data/call-method-in-enum.php index fe40ea7b00..d3e4580b7c 100644 --- a/tests/PHPStan/Rules/Methods/data/call-method-in-enum.php +++ b/tests/PHPStan/Rules/Methods/data/call-method-in-enum.php @@ -43,7 +43,7 @@ enum CountryNo: int case US = 2; } -class FooCall { +enum FooCall { /** * @param value-of $countryName */ From b4eeba0b6fa224d6a78554eb747e66eebf565798 Mon Sep 17 00:00:00 2001 From: Markus Staab Date: Mon, 21 Mar 2022 18:29:57 +0100 Subject: [PATCH 11/18] added NodeScopeResolverTest --- .../Analyser/NodeScopeResolverTest.php | 5 +++ tests/PHPStan/Analyser/data/value-of-enum.php | 36 +++++++++++++++++++ 2 files changed, 41 insertions(+) create mode 100644 tests/PHPStan/Analyser/data/value-of-enum.php diff --git a/tests/PHPStan/Analyser/NodeScopeResolverTest.php b/tests/PHPStan/Analyser/NodeScopeResolverTest.php index 27e9bd2f01..e9001da901 100644 --- a/tests/PHPStan/Analyser/NodeScopeResolverTest.php +++ b/tests/PHPStan/Analyser/NodeScopeResolverTest.php @@ -805,6 +805,11 @@ public function dataFileAsserts(): iterable if (PHP_VERSION_ID >= 80000) { yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-6251.php'); } + + if (PHP_VERSION_ID >= 80100) { + require_once __DIR__ . '/data/value-of-enum.php'; + yield from $this->gatherAssertTypes(__DIR__ . '/data/value-of-enum.php'); + } yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-6584.php'); yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-6439.php'); diff --git a/tests/PHPStan/Analyser/data/value-of-enum.php b/tests/PHPStan/Analyser/data/value-of-enum.php new file mode 100644 index 0000000000..69a3cfbf53 --- /dev/null +++ b/tests/PHPStan/Analyser/data/value-of-enum.php @@ -0,0 +1,36 @@ += 8.1 + +declare(strict_types=1); + +namespace ValueOfEnum; + +use function PHPStan\Testing\assertType; + +enum Country: string +{ + case NL = 'The Netherlands'; + case US = 'United States'; +} + +class Foo { + /** + * @return value-of + */ + function us() + { + return Country::US; + } + + /** + * @param value-of $countryName + */ + function hello($countryName) + { + assertType('string', $countryName); + } + + function doFoo() { + assertType('string', $this->us()); + } +} + From d9bed9fe1cd915971ed2144c6b15665a417be53b Mon Sep 17 00:00:00 2001 From: Markus Staab Date: Wed, 23 Mar 2022 08:50:27 +0100 Subject: [PATCH 12/18] fix --- src/PhpDoc/TypeNodeResolver.php | 10 ++++++---- tests/PHPStan/Analyser/data/value-of-enum.php | 4 ++-- 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/src/PhpDoc/TypeNodeResolver.php b/src/PhpDoc/TypeNodeResolver.php index 58ebeef227..276084ffbd 100644 --- a/src/PhpDoc/TypeNodeResolver.php +++ b/src/PhpDoc/TypeNodeResolver.php @@ -28,6 +28,7 @@ use PHPStan\PhpDocParser\Ast\Type\ThisTypeNode; use PHPStan\PhpDocParser\Ast\Type\TypeNode; use PHPStan\PhpDocParser\Ast\Type\UnionTypeNode; +use PHPStan\Reflection\EnumCaseReflection; use PHPStan\Reflection\Native\NativeParameterReflection; use PHPStan\Reflection\PassedByReference; use PHPStan\Reflection\ReflectionProvider; @@ -530,11 +531,12 @@ private function resolveGenericTypeNode(GenericTypeNode $typeNode, NameScope $na $classReflection = $this->getReflectionProvider()->getClass($genericTypes[0]->getClassName()); if ($classReflection->isBackedEnum()) { - $enumType = $classReflection->getBackedEnumType(); - - if ($enumType !== null) { - return $enumType; + $cases = []; + foreach ($classReflection->getEnumCases() as $enumCaseReflection) { + $cases[] = $enumCaseReflection->getBackingValueType(); } + + return TypeCombinator::union(...$cases); } } } diff --git a/tests/PHPStan/Analyser/data/value-of-enum.php b/tests/PHPStan/Analyser/data/value-of-enum.php index 69a3cfbf53..34ee4447f6 100644 --- a/tests/PHPStan/Analyser/data/value-of-enum.php +++ b/tests/PHPStan/Analyser/data/value-of-enum.php @@ -26,11 +26,11 @@ function us() */ function hello($countryName) { - assertType('string', $countryName); + assertType("'The Netherlands'|'United States'", $countryName); } function doFoo() { - assertType('string', $this->us()); + assertType("'The Netherlands'|'United States'", $this->us()); } } From e85f61b2dd90c0963510c78368fb4b9d599314a0 Mon Sep 17 00:00:00 2001 From: Markus Staab Date: Wed, 23 Mar 2022 08:52:03 +0100 Subject: [PATCH 13/18] remove require --- tests/PHPStan/Analyser/NodeScopeResolverTest.php | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/tests/PHPStan/Analyser/NodeScopeResolverTest.php b/tests/PHPStan/Analyser/NodeScopeResolverTest.php index e9001da901..e0ec6c27d9 100644 --- a/tests/PHPStan/Analyser/NodeScopeResolverTest.php +++ b/tests/PHPStan/Analyser/NodeScopeResolverTest.php @@ -805,9 +805,8 @@ public function dataFileAsserts(): iterable if (PHP_VERSION_ID >= 80000) { yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-6251.php'); } - + if (PHP_VERSION_ID >= 80100) { - require_once __DIR__ . '/data/value-of-enum.php'; yield from $this->gatherAssertTypes(__DIR__ . '/data/value-of-enum.php'); } From 94fed32e8ce80d228f9ee3870e58a583572df5b6 Mon Sep 17 00:00:00 2001 From: Markus Staab Date: Wed, 23 Mar 2022 08:53:01 +0100 Subject: [PATCH 14/18] Revert "remove require" This reverts commit e85f61b2dd90c0963510c78368fb4b9d599314a0. --- src/PhpDoc/TypeNodeResolver.php | 8 ++++++-- tests/PHPStan/Analyser/NodeScopeResolverTest.php | 1 + 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/src/PhpDoc/TypeNodeResolver.php b/src/PhpDoc/TypeNodeResolver.php index 276084ffbd..3c832876b5 100644 --- a/src/PhpDoc/TypeNodeResolver.php +++ b/src/PhpDoc/TypeNodeResolver.php @@ -28,7 +28,6 @@ use PHPStan\PhpDocParser\Ast\Type\ThisTypeNode; use PHPStan\PhpDocParser\Ast\Type\TypeNode; use PHPStan\PhpDocParser\Ast\Type\UnionTypeNode; -use PHPStan\Reflection\EnumCaseReflection; use PHPStan\Reflection\Native\NativeParameterReflection; use PHPStan\Reflection\PassedByReference; use PHPStan\Reflection\ReflectionProvider; @@ -533,7 +532,12 @@ private function resolveGenericTypeNode(GenericTypeNode $typeNode, NameScope $na if ($classReflection->isBackedEnum()) { $cases = []; foreach ($classReflection->getEnumCases() as $enumCaseReflection) { - $cases[] = $enumCaseReflection->getBackingValueType(); + $backingType = $enumCaseReflection->getBackingValueType(); + if ($backingType === null) { + continue; + } + + $cases[] = $backingType; } return TypeCombinator::union(...$cases); diff --git a/tests/PHPStan/Analyser/NodeScopeResolverTest.php b/tests/PHPStan/Analyser/NodeScopeResolverTest.php index e0ec6c27d9..1a96321e51 100644 --- a/tests/PHPStan/Analyser/NodeScopeResolverTest.php +++ b/tests/PHPStan/Analyser/NodeScopeResolverTest.php @@ -807,6 +807,7 @@ public function dataFileAsserts(): iterable } if (PHP_VERSION_ID >= 80100) { + require_once __DIR__ . '/data/value-of-enum.php'; yield from $this->gatherAssertTypes(__DIR__ . '/data/value-of-enum.php'); } From 269e50e9d8ae2add758a04c3fb7c4d5a5dda4877 Mon Sep 17 00:00:00 2001 From: Markus Staab Date: Wed, 23 Mar 2022 09:15:45 +0100 Subject: [PATCH 15/18] fix tests --- tests/PHPStan/Rules/Methods/CallMethodsRuleTest.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/PHPStan/Rules/Methods/CallMethodsRuleTest.php b/tests/PHPStan/Rules/Methods/CallMethodsRuleTest.php index db7a817787..9c6886654d 100644 --- a/tests/PHPStan/Rules/Methods/CallMethodsRuleTest.php +++ b/tests/PHPStan/Rules/Methods/CallMethodsRuleTest.php @@ -2288,15 +2288,15 @@ public function testEnums(): void 22, ], [ - 'Parameter #1 $countryName of method CallMethodInEnum\FooCall::hello() expects string, CallMethodInEnum\CountryNo::NL given.', + 'Parameter #1 $countryName of method CallMethodInEnum\FooCall::hello() expects \'The Netherlands\'|\'United States\', CallMethodInEnum\CountryNo::NL given.', 63, ], [ - 'Parameter #1 $countryMap of method CallMethodInEnum\FooCall::helloArray() expects array, array given.', + 'Parameter #1 $countryMap of method CallMethodInEnum\FooCall::helloArray() expects array<\'The Netherlands\'|\'United States\', bool>, array given.', 67, ], [ - 'Parameter #1 $countryMap of method CallMethodInEnum\FooCall::helloArray() expects array, array given.', + 'Parameter #1 $countryMap of method CallMethodInEnum\FooCall::helloArray() expects array<\'The Netherlands\'|\'United States\', bool>, array given.', 70, ], ]); From 277f7e6bd38b00a181169f9efe550c4ae474cc63 Mon Sep 17 00:00:00 2001 From: Markus Staab Date: Wed, 23 Mar 2022 09:17:45 +0100 Subject: [PATCH 16/18] fix tests --- tests/PHPStan/Rules/Methods/CallMethodsRuleTest.php | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/tests/PHPStan/Rules/Methods/CallMethodsRuleTest.php b/tests/PHPStan/Rules/Methods/CallMethodsRuleTest.php index 9c6886654d..c14a2e3ee6 100644 --- a/tests/PHPStan/Rules/Methods/CallMethodsRuleTest.php +++ b/tests/PHPStan/Rules/Methods/CallMethodsRuleTest.php @@ -2291,6 +2291,10 @@ public function testEnums(): void 'Parameter #1 $countryName of method CallMethodInEnum\FooCall::hello() expects \'The Netherlands\'|\'United States\', CallMethodInEnum\CountryNo::NL given.', 63, ], + [ + 'Parameter #1 $countryMap of method CallMethodInEnum\FooCall::helloArray() expects array<\'The Netherlands\'|\'United States\', bool>, array{abc: true} given.', + 66, + ], [ 'Parameter #1 $countryMap of method CallMethodInEnum\FooCall::helloArray() expects array<\'The Netherlands\'|\'United States\', bool>, array given.', 67, From 2759d26fc7609e5e9f8a0857fc140bfd3beefb58 Mon Sep 17 00:00:00 2001 From: Markus Staab Date: Wed, 23 Mar 2022 09:26:29 +0100 Subject: [PATCH 17/18] fix tests --- tests/PHPStan/Rules/Methods/CallMethodsRuleTest.php | 4 ++-- tests/PHPStan/Rules/Methods/data/call-method-in-enum.php | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/PHPStan/Rules/Methods/CallMethodsRuleTest.php b/tests/PHPStan/Rules/Methods/CallMethodsRuleTest.php index c14a2e3ee6..68d33c7cfc 100644 --- a/tests/PHPStan/Rules/Methods/CallMethodsRuleTest.php +++ b/tests/PHPStan/Rules/Methods/CallMethodsRuleTest.php @@ -2296,11 +2296,11 @@ public function testEnums(): void 66, ], [ - 'Parameter #1 $countryMap of method CallMethodInEnum\FooCall::helloArray() expects array<\'The Netherlands\'|\'United States\', bool>, array given.', + 'Parameter #1 $countryMap of method CallMethodInEnum\FooCall::helloArray() expects array<\'The Netherlands\'|\'United States\', bool>, array{abc: 123} given.', 67, ], [ - 'Parameter #1 $countryMap of method CallMethodInEnum\FooCall::helloArray() expects array<\'The Netherlands\'|\'United States\', bool>, array given.', + 'Parameter #1 $countryMap of method CallMethodInEnum\FooCall::helloArray() expects array<\'The Netherlands\'|\'United States\', bool>, array{true} given.', 70, ], ]); diff --git a/tests/PHPStan/Rules/Methods/data/call-method-in-enum.php b/tests/PHPStan/Rules/Methods/data/call-method-in-enum.php index d3e4580b7c..986fc854cb 100644 --- a/tests/PHPStan/Rules/Methods/data/call-method-in-enum.php +++ b/tests/PHPStan/Rules/Methods/data/call-method-in-enum.php @@ -62,7 +62,7 @@ function helloArray(array $countryMap): void { function doFooArray() { $this->hello(CountryNo::NL); - // should not be allowed, 'abc' does not match value-of + // 'abc' does not match value-of $this->helloArray(['abc' => true]); $this->helloArray(['abc' => 123]); From 7b6269700c26adcb551bae3b350540c8a8133735 Mon Sep 17 00:00:00 2001 From: Markus Staab Date: Wed, 23 Mar 2022 10:00:17 +0100 Subject: [PATCH 18/18] remove require --- tests/PHPStan/Analyser/NodeScopeResolverTest.php | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/PHPStan/Analyser/NodeScopeResolverTest.php b/tests/PHPStan/Analyser/NodeScopeResolverTest.php index 1a96321e51..e0ec6c27d9 100644 --- a/tests/PHPStan/Analyser/NodeScopeResolverTest.php +++ b/tests/PHPStan/Analyser/NodeScopeResolverTest.php @@ -807,7 +807,6 @@ public function dataFileAsserts(): iterable } if (PHP_VERSION_ID >= 80100) { - require_once __DIR__ . '/data/value-of-enum.php'; yield from $this->gatherAssertTypes(__DIR__ . '/data/value-of-enum.php'); }