diff --git a/src/Type/VerbosityLevel.php b/src/Type/VerbosityLevel.php index cef110be0f..7e351f30b2 100644 --- a/src/Type/VerbosityLevel.php +++ b/src/Type/VerbosityLevel.php @@ -2,6 +2,8 @@ namespace PHPStan\Type; +use PHPStan\Type\Constant\ConstantArrayType; + class VerbosityLevel { @@ -49,11 +51,20 @@ public static function cache(): self public static function getRecommendedLevelByType(Type $type): self { - if (TypeUtils::containsCallable($type) || count(TypeUtils::getConstantArrays($type)) > 0) { - return self::value(); - } + $moreVerbose = false; + TypeTraverser::map($type, function (Type $type, callable $traverse) use (&$moreVerbose): Type { + if ($type->isCallable()->yes()) { + $moreVerbose = true; + return $type; + } + if ($type instanceof ConstantType && !$type instanceof NullType) { + $moreVerbose = true; + return $type; + } + return $traverse($type); + }); - return self::typeOnly(); + return $moreVerbose ? VerbosityLevel::value() : VerbosityLevel::typeOnly(); } /** diff --git a/tests/PHPStan/Rules/Methods/CallMethodsRuleTest.php b/tests/PHPStan/Rules/Methods/CallMethodsRuleTest.php index ee80bd9eb9..1e76941f01 100644 --- a/tests/PHPStan/Rules/Methods/CallMethodsRuleTest.php +++ b/tests/PHPStan/Rules/Methods/CallMethodsRuleTest.php @@ -456,6 +456,10 @@ public function testCallMethods(): void 'Parameter #1 $a of method Test\CallableWithMixedArray::doBar() expects callable(array): array, Closure(array): array|null given.', 1533, ], + [ + 'Parameter #1 $members of method Test\ParameterTypeCheckVerbosity::doBar() expects array string, \'code\' => string)>, array string)> given.', + 1589, + ], ]); } @@ -713,6 +717,10 @@ public function testCallMethodsOnThisOnly(): void 'Parameter #1 $a of method Test\CallableWithMixedArray::doBar() expects callable(array): array, Closure(array): array|null given.', 1533, ], + [ + 'Parameter #1 $members of method Test\ParameterTypeCheckVerbosity::doBar() expects array string, \'code\' => string)>, array string)> given.', + 1589, + ], ]); } diff --git a/tests/PHPStan/Rules/Methods/data/call-methods.php b/tests/PHPStan/Rules/Methods/data/call-methods.php index 99968c903f..0b046e24e4 100644 --- a/tests/PHPStan/Rules/Methods/data/call-methods.php +++ b/tests/PHPStan/Rules/Methods/data/call-methods.php @@ -1577,3 +1577,24 @@ public function doFoo(Foo $foo) } } + +class ParameterTypeCheckVerbosity +{ + + /** + * @param array{code: string}[] $members + */ + public function doFoo(array $members) + { + $this->doBar($members); + } + + /** + * @param array{id: string, code: string}[] $members + */ + public function doBar(array $members) + { + + } + +}