Skip to content

Commit

Permalink
Fix constant array description verbosity
Browse files Browse the repository at this point in the history
  • Loading branch information
ondrejmirtes committed May 3, 2020
1 parent 9a46f7f commit 1e02e9d
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 4 deletions.
19 changes: 15 additions & 4 deletions src/Type/VerbosityLevel.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace PHPStan\Type;

use PHPStan\Type\Constant\ConstantArrayType;

class VerbosityLevel
{

Expand Down Expand Up @@ -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();
}

/**
Expand Down
8 changes: 8 additions & 0 deletions tests/PHPStan/Rules/Methods/CallMethodsRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -456,6 +456,10 @@ public function testCallMethods(): void
'Parameter #1 $a of method Test\CallableWithMixedArray::doBar() expects callable(array<string>): array<string>, Closure(array): array|null given.',
1533,
],
[
'Parameter #1 $members of method Test\ParameterTypeCheckVerbosity::doBar() expects array<array(\'id\' => string, \'code\' => string)>, array<array(\'code\' => string)> given.',
1589,
],
]);
}

Expand Down Expand Up @@ -713,6 +717,10 @@ public function testCallMethodsOnThisOnly(): void
'Parameter #1 $a of method Test\CallableWithMixedArray::doBar() expects callable(array<string>): array<string>, Closure(array): array|null given.',
1533,
],
[
'Parameter #1 $members of method Test\ParameterTypeCheckVerbosity::doBar() expects array<array(\'id\' => string, \'code\' => string)>, array<array(\'code\' => string)> given.',
1589,
],
]);
}

Expand Down
21 changes: 21 additions & 0 deletions tests/PHPStan/Rules/Methods/data/call-methods.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{

}

}

0 comments on commit 1e02e9d

Please sign in to comment.