Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions src/Type/VerbosityLevel.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -163,7 +163,7 @@ public static function getRecommendedLevelByType(Type $acceptingType, ?Type $acc
});

if (!$containsInvariantTemplateType) {
return self::typeOnly();
return $verbosity ?? self::typeOnly();
}

/** @var bool $moreVerbose */
Expand All @@ -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();
}

/**
Expand Down
15 changes: 15 additions & 0 deletions tests/PHPStan/Rules/Methods/ReturnTypeRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1054,4 +1054,19 @@ public function testBug10715(): void
$this->analyse([__DIR__ . '/data/bug-10715.php'], []);
}

public function testBug11835(): void
{
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

VerbosityLevel unit test would make more sense than this because this error will inevitably go away.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I updated the PR with another test. I hope it's what you wanted :)

$this->analyse([__DIR__ . '/data/bug-11835.php'], [
[
'Method ProjectionCumulativeHeadersResolver::resolve() should return Collection<int, non-falsy-string> but returns Collection<int, lowercase-string&non-falsy-string>.',
37,
'Template type TValue on class Collection is not covariant. Learn more: <fg=cyan>https://phpstan.org/blog/whats-up-with-template-covariant</>',
],
[
'Method ProjectionCumulativeHeadersResolver::test() should return false but returns string.',
47,
],
]);
}

}
49 changes: 49 additions & 0 deletions tests/PHPStan/Rules/Methods/data/bug-11835.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php declare(strict_types = 1);

/**
* @template TKey of array-key
* @template TValue
*/
final class Collection
{
/** @return self<int, static> */
public function chunk(int $size): self
{
return $this;
}

/**
* @template TMapValue
*
* @param callable(TValue, TKey): TMapValue $callback
* @return self<TKey, TMapValue>
*/
public function map(callable $callback): self
{
return $this;
}
}

class DateHeader {}

class ProjectionCumulativeHeadersResolver
{
/**
* @param Collection<int, DateHeader> $projectionMonthsHeaders
* @return Collection<int, non-falsy-string>
*/
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;
}
}
Loading