Skip to content

Commit 1e02e9d

Browse files
committed
Fix constant array description verbosity
1 parent 9a46f7f commit 1e02e9d

File tree

3 files changed

+44
-4
lines changed

3 files changed

+44
-4
lines changed

src/Type/VerbosityLevel.php

+15-4
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace PHPStan\Type;
44

5+
use PHPStan\Type\Constant\ConstantArrayType;
6+
57
class VerbosityLevel
68
{
79

@@ -49,11 +51,20 @@ public static function cache(): self
4951

5052
public static function getRecommendedLevelByType(Type $type): self
5153
{
52-
if (TypeUtils::containsCallable($type) || count(TypeUtils::getConstantArrays($type)) > 0) {
53-
return self::value();
54-
}
54+
$moreVerbose = false;
55+
TypeTraverser::map($type, function (Type $type, callable $traverse) use (&$moreVerbose): Type {
56+
if ($type->isCallable()->yes()) {
57+
$moreVerbose = true;
58+
return $type;
59+
}
60+
if ($type instanceof ConstantType && !$type instanceof NullType) {
61+
$moreVerbose = true;
62+
return $type;
63+
}
64+
return $traverse($type);
65+
});
5566

56-
return self::typeOnly();
67+
return $moreVerbose ? VerbosityLevel::value() : VerbosityLevel::typeOnly();
5768
}
5869

5970
/**

tests/PHPStan/Rules/Methods/CallMethodsRuleTest.php

+8
Original file line numberDiff line numberDiff line change
@@ -456,6 +456,10 @@ public function testCallMethods(): void
456456
'Parameter #1 $a of method Test\CallableWithMixedArray::doBar() expects callable(array<string>): array<string>, Closure(array): array|null given.',
457457
1533,
458458
],
459+
[
460+
'Parameter #1 $members of method Test\ParameterTypeCheckVerbosity::doBar() expects array<array(\'id\' => string, \'code\' => string)>, array<array(\'code\' => string)> given.',
461+
1589,
462+
],
459463
]);
460464
}
461465

@@ -713,6 +717,10 @@ public function testCallMethodsOnThisOnly(): void
713717
'Parameter #1 $a of method Test\CallableWithMixedArray::doBar() expects callable(array<string>): array<string>, Closure(array): array|null given.',
714718
1533,
715719
],
720+
[
721+
'Parameter #1 $members of method Test\ParameterTypeCheckVerbosity::doBar() expects array<array(\'id\' => string, \'code\' => string)>, array<array(\'code\' => string)> given.',
722+
1589,
723+
],
716724
]);
717725
}
718726

tests/PHPStan/Rules/Methods/data/call-methods.php

+21
Original file line numberDiff line numberDiff line change
@@ -1577,3 +1577,24 @@ public function doFoo(Foo $foo)
15771577
}
15781578

15791579
}
1580+
1581+
class ParameterTypeCheckVerbosity
1582+
{
1583+
1584+
/**
1585+
* @param array{code: string}[] $members
1586+
*/
1587+
public function doFoo(array $members)
1588+
{
1589+
$this->doBar($members);
1590+
}
1591+
1592+
/**
1593+
* @param array{id: string, code: string}[] $members
1594+
*/
1595+
public function doBar(array $members)
1596+
{
1597+
1598+
}
1599+
1600+
}

0 commit comments

Comments
 (0)