Skip to content

Commit

Permalink
More rules use the recommended type description verbosity level
Browse files Browse the repository at this point in the history
  • Loading branch information
ondrejmirtes committed May 3, 2020
1 parent f6a37a0 commit d4f0907
Show file tree
Hide file tree
Showing 7 changed files with 20 additions and 16 deletions.
2 changes: 1 addition & 1 deletion src/Rules/Arrays/AppendedArrayItemTypeRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ public function processNode(\PhpParser\Node $node, Scope $scope): array

$itemType = $assignedToType->getItemType();
if (!$this->ruleLevelHelper->accepts($itemType, $assignedValueType, $scope->isDeclareStrictTypes())) {
$verbosityLevel = $itemType->isCallable()->and($assignedValueType->isCallable())->yes() ? VerbosityLevel::value() : VerbosityLevel::typeOnly();
$verbosityLevel = VerbosityLevel::getRecommendedLevelByType($itemType);
return [
RuleErrorBuilder::message(sprintf(
'Array (%s) does not accept %s.',
Expand Down
5 changes: 1 addition & 4 deletions src/Rules/FunctionReturnTypeCheck.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
use PHPStan\Analyser\Scope;
use PHPStan\Type\GenericTypeVariableResolver;
use PHPStan\Type\Type;
use PHPStan\Type\TypeUtils;
use PHPStan\Type\TypeWithClassName;
use PHPStan\Type\VerbosityLevel;
use PHPStan\Type\VoidType;
Expand Down Expand Up @@ -58,9 +57,7 @@ public function checkReturnType(
}

$isVoidSuperType = (new VoidType())->isSuperTypeOf($returnType);
$verbosityLevel = $returnType->isCallable()->yes() || count(TypeUtils::getConstantArrays($returnType)) > 0
? VerbosityLevel::value()
: VerbosityLevel::typeOnly();
$verbosityLevel = VerbosityLevel::getRecommendedLevelByType($returnType);
if ($returnValue === null) {
if (!$isVoidSuperType->no()) {
return [];
Expand Down
6 changes: 4 additions & 2 deletions src/Rules/Functions/IncompatibleDefaultParameterTypeRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,13 +64,15 @@ public function processNode(Node $node, Scope $scope): array
continue;
}

$verbosityLevel = VerbosityLevel::getRecommendedLevelByType($parameterType);

$errors[] = RuleErrorBuilder::message(sprintf(
'Default value of the parameter #%d $%s (%s) of function %s() is incompatible with type %s.',
$paramI + 1,
$param->var->name,
$defaultValueType->describe(VerbosityLevel::value()),
$defaultValueType->describe($verbosityLevel),
$function->getName(),
$parameterType->describe(VerbosityLevel::value())
$parameterType->describe($verbosityLevel)
))->line($param->getLine())->build();
}

Expand Down
6 changes: 4 additions & 2 deletions src/Rules/Methods/IncompatibleDefaultParameterTypeRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,14 +50,16 @@ public function processNode(Node $node, Scope $scope): array
continue;
}

$verbosityLevel = VerbosityLevel::getRecommendedLevelByType($parameterType);

$errors[] = RuleErrorBuilder::message(sprintf(
'Default value of the parameter #%d $%s (%s) of method %s::%s() is incompatible with type %s.',
$paramI + 1,
$param->var->name,
$defaultValueType->describe(VerbosityLevel::value()),
$defaultValueType->describe($verbosityLevel),
$method->getDeclaringClass()->getDisplayName(),
$method->getName(),
$parameterType->describe(VerbosityLevel::value())
$parameterType->describe($verbosityLevel)
))->line($param->getLine())->build();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,15 @@ public function processNode(Node $node, Scope $scope): array
continue;
}

$verbosityLevel = VerbosityLevel::getRecommendedLevelByType($propertyType);

$errors[] = RuleErrorBuilder::message(sprintf(
'%s %s::$%s (%s) does not accept default value of type %s.',
$node->isStatic() ? 'Static property' : 'Property',
$classReflection->getDisplayName(),
$property->name->name,
$propertyType->describe(VerbosityLevel::typeOnly()),
$defaultValueType->describe(VerbosityLevel::typeOnly())
$propertyType->describe($verbosityLevel),
$defaultValueType->describe($verbosityLevel)
))->build();
}

Expand Down
5 changes: 3 additions & 2 deletions src/Rules/Properties/TypesAssignedToPropertiesRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,13 +71,14 @@ public function processNode(Node $node, Scope $scope): array
}
if (!$this->ruleLevelHelper->accepts($propertyType, $assignedValueType, $scope->isDeclareStrictTypes())) {
$propertyDescription = $this->propertyDescriptor->describeProperty($propertyReflection, $propertyFetch);
$verbosityLevel = VerbosityLevel::getRecommendedLevelByType($propertyType);

return [
RuleErrorBuilder::message(sprintf(
'%s (%s) does not accept %s.',
$propertyDescription,
$propertyType->describe(VerbosityLevel::typeOnly()),
$assignedValueType->describe(VerbosityLevel::typeOnly())
$propertyType->describe($verbosityLevel),
$assignedValueType->describe($verbosityLevel)
))->build(),
];
}
Expand Down
6 changes: 3 additions & 3 deletions tests/PHPStan/Rules/Arrays/AppendedArrayItemTypeRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,15 @@ public function testAppendedArrayItemType(): void
18,
],
[
'Array (array<callable>) does not accept array<int, int>.',
'Array (array<callable(): mixed>) does not accept array(1, 2, 3).',
20,
],
[
'Array (array<callable>) does not accept array<int, string>.',
'Array (array<callable(): mixed>) does not accept array(\'AppendedArrayItem\\\\Foo\', \'classMethod\').',
23,
],
[
'Array (array<callable>) does not accept array<int, string>.',
'Array (array<callable(): mixed>) does not accept array(\'Foo\', \'Hello world\').',
25,
],
[
Expand Down

0 comments on commit d4f0907

Please sign in to comment.