From 6070a2b1444372be73b79d7971cfc27bc32c6368 Mon Sep 17 00:00:00 2001 From: Vincent Langlet Date: Sat, 12 Oct 2024 12:05:53 +0200 Subject: [PATCH 1/4] Improve lowercase string verbosity level --- src/Type/VerbosityLevel.php | 8 +-- .../Rules/Methods/ReturnTypeRuleTest.php | 15 ++++++ .../PHPStan/Rules/Methods/data/bug-11835.php | 49 +++++++++++++++++++ 3 files changed, 68 insertions(+), 4 deletions(-) create mode 100644 tests/PHPStan/Rules/Methods/data/bug-11835.php diff --git a/src/Type/VerbosityLevel.php b/src/Type/VerbosityLevel.php index 52b66c154fb..47b0d2477c5 100644 --- a/src/Type/VerbosityLevel.php +++ b/src/Type/VerbosityLevel.php @@ -131,11 +131,11 @@ public static function getRecommendedLevelByType(Type $acceptingType, ?Type $acc } if ($moreVerbose) { - return self::value(); + $verbosity = self::value(); } if ($acceptedType === null) { - return self::typeOnly(); + return $verbosity ?? self::typeOnly(); } $containsInvariantTemplateType = false; @@ -163,7 +163,7 @@ public static function getRecommendedLevelByType(Type $acceptingType, ?Type $acc }); if (!$containsInvariantTemplateType) { - return self::typeOnly(); + return $verbosity ?? self::typeOnly(); } /** @var bool $moreVerbose */ @@ -176,7 +176,7 @@ public static function getRecommendedLevelByType(Type $acceptingType, ?Type $acc return self::precise(); } - return $moreVerbose ? self::value() : self::typeOnly(); + return $moreVerbose ? self::value() : $verbosity ?? self::typeOnly(); } /** diff --git a/tests/PHPStan/Rules/Methods/ReturnTypeRuleTest.php b/tests/PHPStan/Rules/Methods/ReturnTypeRuleTest.php index 6d374a6f1c9..c17cd2be088 100644 --- a/tests/PHPStan/Rules/Methods/ReturnTypeRuleTest.php +++ b/tests/PHPStan/Rules/Methods/ReturnTypeRuleTest.php @@ -1054,4 +1054,19 @@ public function testBug10715(): void $this->analyse([__DIR__ . '/data/bug-10715.php'], []); } + public function testBug11835(): void + { + $this->analyse([__DIR__ . '/data/bug-11835.php'], [ + [ + 'Method ProjectionCumulativeHeadersResolver::resolve() should return Collection but returns Collection.', + 37, + 'Template type TValue on class Collection is not covariant. Learn more: https://phpstan.org/blog/whats-up-with-template-covariant', + ], + [ + 'Method ProjectionCumulativeHeadersResolver::test() should return false but returns string.', + 47, + ], + ]); + } + } diff --git a/tests/PHPStan/Rules/Methods/data/bug-11835.php b/tests/PHPStan/Rules/Methods/data/bug-11835.php new file mode 100644 index 00000000000..58c271e628b --- /dev/null +++ b/tests/PHPStan/Rules/Methods/data/bug-11835.php @@ -0,0 +1,49 @@ + */ + public function chunk(int $size): self + { + return $this; + } + + /** + * @template TMapValue + * + * @param callable(TValue, TKey): TMapValue $callback + * @return self + */ + public function map(callable $callback): self + { + return $this; + } +} + +class DateHeader {} + +class ProjectionCumulativeHeadersResolver +{ + /** + * @param Collection $projectionMonthsHeaders + * @return Collection + */ + public function resolve(Collection $projectionMonthsHeaders): Collection + { + return $projectionMonthsHeaders->chunk(2) + ->map(fn ($_, $index) => $index * 2 + 2 . ' month'); + } + + /** + * @param lowercase-string $s + * @return false + */ + public function test(string $s) + { + return $s; + } +} From c5d74dfd74d87539c5799062629f6eb6657ffe6e Mon Sep 17 00:00:00 2001 From: Vincent Langlet Date: Sat, 12 Oct 2024 13:00:50 +0200 Subject: [PATCH 2/4] Fix --- tests/PHPStan/Rules/Methods/data/bug-11835.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/tests/PHPStan/Rules/Methods/data/bug-11835.php b/tests/PHPStan/Rules/Methods/data/bug-11835.php index 58c271e628b..f696b51b852 100644 --- a/tests/PHPStan/Rules/Methods/data/bug-11835.php +++ b/tests/PHPStan/Rules/Methods/data/bug-11835.php @@ -1,5 +1,7 @@ Date: Sat, 12 Oct 2024 16:29:23 +0200 Subject: [PATCH 3/4] Unit tests --- .../Rules/Methods/ReturnTypeRuleTest.php | 15 ----- .../PHPStan/Rules/Methods/data/bug-11835.php | 51 ---------------- tests/PHPStan/Type/VerbosityLevelTest.php | 61 +++++++++++++++++++ 3 files changed, 61 insertions(+), 66 deletions(-) delete mode 100644 tests/PHPStan/Rules/Methods/data/bug-11835.php create mode 100644 tests/PHPStan/Type/VerbosityLevelTest.php diff --git a/tests/PHPStan/Rules/Methods/ReturnTypeRuleTest.php b/tests/PHPStan/Rules/Methods/ReturnTypeRuleTest.php index c17cd2be088..6d374a6f1c9 100644 --- a/tests/PHPStan/Rules/Methods/ReturnTypeRuleTest.php +++ b/tests/PHPStan/Rules/Methods/ReturnTypeRuleTest.php @@ -1054,19 +1054,4 @@ public function testBug10715(): void $this->analyse([__DIR__ . '/data/bug-10715.php'], []); } - public function testBug11835(): void - { - $this->analyse([__DIR__ . '/data/bug-11835.php'], [ - [ - 'Method ProjectionCumulativeHeadersResolver::resolve() should return Collection but returns Collection.', - 37, - 'Template type TValue on class Collection is not covariant. Learn more: https://phpstan.org/blog/whats-up-with-template-covariant', - ], - [ - 'Method ProjectionCumulativeHeadersResolver::test() should return false but returns string.', - 47, - ], - ]); - } - } diff --git a/tests/PHPStan/Rules/Methods/data/bug-11835.php b/tests/PHPStan/Rules/Methods/data/bug-11835.php deleted file mode 100644 index f696b51b852..00000000000 --- a/tests/PHPStan/Rules/Methods/data/bug-11835.php +++ /dev/null @@ -1,51 +0,0 @@ - */ - public function chunk(int $size): self - { - return $this; - } - - /** - * @template TMapValue - * - * @param callable(TValue, TKey): TMapValue $callback - * @return self - */ - public function map(callable $callback): self - { - return $this; - } -} - -class DateHeader {} - -class ProjectionCumulativeHeadersResolver -{ - /** - * @param Collection $projectionMonthsHeaders - * @return Collection - */ - public function resolve(Collection $projectionMonthsHeaders): Collection - { - return $projectionMonthsHeaders->chunk(2) - ->map(fn ($_, $index) => $index * 2 + 2 . ' month'); - } - - /** - * @param lowercase-string $s - * @return false - */ - public function test(string $s) - { - return $s; - } -} diff --git a/tests/PHPStan/Type/VerbosityLevelTest.php b/tests/PHPStan/Type/VerbosityLevelTest.php new file mode 100644 index 00000000000..81a350119a0 --- /dev/null +++ b/tests/PHPStan/Type/VerbosityLevelTest.php @@ -0,0 +1,61 @@ +assertSame($expected->getLevelValue(), $level->getLevelValue()); + } + +} From 0d1298ff429a07c999fb0b7cd678b67b3c7178f8 Mon Sep 17 00:00:00 2001 From: Vincent Langlet Date: Sat, 12 Oct 2024 16:37:40 +0200 Subject: [PATCH 4/4] Fix --- tests/PHPStan/Type/VerbosityLevelTest.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/PHPStan/Type/VerbosityLevelTest.php b/tests/PHPStan/Type/VerbosityLevelTest.php index 81a350119a0..0ff71d50257 100644 --- a/tests/PHPStan/Type/VerbosityLevelTest.php +++ b/tests/PHPStan/Type/VerbosityLevelTest.php @@ -32,7 +32,7 @@ public function dataGetRecommendedLevelByType(): iterable ], null, null, - [TemplateTypeVariance::createInvariant(), TemplateTypeVariance::createInvariant()] + [TemplateTypeVariance::createInvariant(), TemplateTypeVariance::createInvariant()], ), new GenericObjectType( 'ArrayAccess', @@ -42,7 +42,7 @@ public function dataGetRecommendedLevelByType(): iterable ], null, null, - [TemplateTypeVariance::createInvariant(), TemplateTypeVariance::createInvariant()] + [TemplateTypeVariance::createInvariant(), TemplateTypeVariance::createInvariant()], ), VerbosityLevel::precise(), ];