Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
5 changes: 3 additions & 2 deletions src/Rules/FunctionCallParametersCheck.php
Original file line number Diff line number Diff line change
Expand Up @@ -505,12 +505,13 @@ public function check(
}

if (!$accepts->result) {
$verbosityLevel = VerbosityLevel::getRecommendedLevelByType($parameterType, $argumentValueType);
$givenType = $accepts->witnessType ?? $argumentValueType;
$verbosityLevel = VerbosityLevel::getRecommendedLevelByType($parameterType, $givenType);
$errors[] = RuleErrorBuilder::message(sprintf(
$wrongArgumentTypeMessage,
$this->describeParameter($parameter, $argumentName ?? $i + 1),
$parameterType->describe($verbosityLevel),
$argumentValueType->describe($verbosityLevel),
$givenType->describe($verbosityLevel),
))
->identifier('argument.type')
->line($argumentLine)
Expand Down
5 changes: 3 additions & 2 deletions src/Rules/FunctionReturnTypeCheck.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,13 +92,14 @@ public function checkReturnType(

$accepts = $this->ruleLevelHelper->accepts($returnType, $returnValueType, $scope->isDeclareStrictTypes());
if (!$accepts->result) {
$verbosityLevel = VerbosityLevel::getRecommendedLevelByType($returnType, $returnValueType);
$givenType = $accepts->witnessType ?? $returnValueType;
$verbosityLevel = VerbosityLevel::getRecommendedLevelByType($returnType, $givenType);

return [
RuleErrorBuilder::message(sprintf(
$typeMismatchMessage,
$returnType->describe($verbosityLevel),
$returnValueType->describe($verbosityLevel),
$givenType->describe($verbosityLevel),
))
->line($returnNode->getStartLine())
->identifier('return.type')
Expand Down
5 changes: 3 additions & 2 deletions src/Rules/Functions/ReturnTypeAfterFinallyRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,12 +90,13 @@ public function processNode(Node $node, Scope $scope): array
continue;
}

$verbosityLevel = VerbosityLevel::getRecommendedLevelByType($returnType, $typeAfterFinally);
$givenType = $accepts->witnessType ?? $typeAfterFinally;
$verbosityLevel = VerbosityLevel::getRecommendedLevelByType($returnType, $givenType);
$errors[] = RuleErrorBuilder::message(sprintf(
'%s should return %s but returns %s because the finally block modifies the value returned by reference.',
$description,
$returnType->describe($verbosityLevel),
$typeAfterFinally->describe($verbosityLevel),
$givenType->describe($verbosityLevel),
))
->line($returnNode->getStartLine())
->identifier('return.byRefFinally')
Expand Down
10 changes: 6 additions & 4 deletions src/Rules/Generators/YieldFromTypeRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,11 +84,12 @@ public function processNode(Node $node, Scope $scope): array
$messages = [];
$acceptsKey = $this->ruleLevelHelper->accepts($returnType->getIterableKeyType(), $exprType->getIterableKeyType(), $scope->isDeclareStrictTypes());
if (!$acceptsKey->result) {
$verbosityLevel = VerbosityLevel::getRecommendedLevelByType($returnType->getIterableKeyType(), $exprType->getIterableKeyType());
$givenKeyType = $acceptsKey->witnessType ?? $exprType->getIterableKeyType();
$verbosityLevel = VerbosityLevel::getRecommendedLevelByType($returnType->getIterableKeyType(), $givenKeyType);
$messages[] = RuleErrorBuilder::message(sprintf(
'Generator expects key type %s, %s given.',
$returnType->getIterableKeyType()->describe($verbosityLevel),
$exprType->getIterableKeyType()->describe($verbosityLevel),
$givenKeyType->describe($verbosityLevel),
))
->line($node->expr->getStartLine())
->identifier('generator.keyType')
Expand All @@ -98,11 +99,12 @@ public function processNode(Node $node, Scope $scope): array

$acceptsValue = $this->ruleLevelHelper->accepts($returnType->getIterableValueType(), $exprType->getIterableValueType(), $scope->isDeclareStrictTypes());
if (!$acceptsValue->result) {
$verbosityLevel = VerbosityLevel::getRecommendedLevelByType($returnType->getIterableValueType(), $exprType->getIterableValueType());
$givenValueType = $acceptsValue->witnessType ?? $exprType->getIterableValueType();
$verbosityLevel = VerbosityLevel::getRecommendedLevelByType($returnType->getIterableValueType(), $givenValueType);
$messages[] = RuleErrorBuilder::message(sprintf(
'Generator expects value type %s, %s given.',
$returnType->getIterableValueType()->describe($verbosityLevel),
$exprType->getIterableValueType()->describe($verbosityLevel),
$givenValueType->describe($verbosityLevel),
))
->line($node->expr->getStartLine())
->identifier('generator.valueType')
Expand Down
10 changes: 6 additions & 4 deletions src/Rules/Generators/YieldTypeRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,12 @@ public function processNode(Node $node, Scope $scope): array
$messages = [];
$acceptsKey = $this->ruleLevelHelper->accepts($returnType->getIterableKeyType(), $keyType, $scope->isDeclareStrictTypes());
if (!$acceptsKey->result) {
$verbosityLevel = VerbosityLevel::getRecommendedLevelByType($returnType->getIterableKeyType(), $keyType);
$givenKeyType = $acceptsKey->witnessType ?? $keyType;
$verbosityLevel = VerbosityLevel::getRecommendedLevelByType($returnType->getIterableKeyType(), $givenKeyType);
$messages[] = RuleErrorBuilder::message(sprintf(
'Generator expects key type %s, %s given.',
$returnType->getIterableKeyType()->describe($verbosityLevel),
$keyType->describe($verbosityLevel),
$givenKeyType->describe($verbosityLevel),
))
->acceptsReasonsTip($acceptsKey->reasons)
->identifier('generator.keyType')
Expand All @@ -76,11 +77,12 @@ public function processNode(Node $node, Scope $scope): array

$acceptsValue = $this->ruleLevelHelper->accepts($returnType->getIterableValueType(), $valueType, $scope->isDeclareStrictTypes());
if (!$acceptsValue->result) {
$verbosityLevel = VerbosityLevel::getRecommendedLevelByType($returnType->getIterableValueType(), $valueType);
$givenValueType = $acceptsValue->witnessType ?? $valueType;
$verbosityLevel = VerbosityLevel::getRecommendedLevelByType($returnType->getIterableValueType(), $givenValueType);
$messages[] = RuleErrorBuilder::message(sprintf(
'Generator expects value type %s, %s given.',
$returnType->getIterableValueType()->describe($verbosityLevel),
$valueType->describe($verbosityLevel),
$givenValueType->describe($verbosityLevel),
))
->acceptsReasonsTip($acceptsValue->reasons)
->identifier('generator.valueType')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@ public function processNode(Node $node, Scope $scope): array
return [];
}

$verbosityLevel = VerbosityLevel::getRecommendedLevelByType($propertyType, $defaultValueType);
$givenType = $accepts->witnessType ?? $defaultValueType;
$verbosityLevel = VerbosityLevel::getRecommendedLevelByType($propertyType, $givenType);

return [
RuleErrorBuilder::message(sprintf(
Expand All @@ -59,7 +60,7 @@ public function processNode(Node $node, Scope $scope): array
$classReflection->getDisplayName(),
$node->getName(),
$propertyType->describe($verbosityLevel),
$defaultValueType->describe($verbosityLevel),
$givenType->describe($verbosityLevel),
))
->identifier('property.defaultValue')
->acceptsReasonsTip($accepts->reasons)
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 @@ -90,14 +90,15 @@ private function processSingleProperty(
$accepts = $this->ruleLevelHelper->accepts($propertyType, $assignedValueType, $scope->isDeclareStrictTypes());
if (!$accepts->result) {
$propertyDescription = $this->describePropertyByName($propertyReflection, $propertyReflection->getName());
$verbosityLevel = VerbosityLevel::getRecommendedLevelByType($propertyType, $assignedValueType);
$givenType = $accepts->witnessType ?? $assignedValueType;
$verbosityLevel = VerbosityLevel::getRecommendedLevelByType($propertyType, $givenType);

return [
RuleErrorBuilder::message(sprintf(
'%s (%s) does not accept %s.',
$propertyDescription,
$propertyType->describe($verbosityLevel),
$assignedValueType->describe($verbosityLevel),
$givenType->describe($verbosityLevel),
))
->line($assignedExpr->getStartLine())
->identifier('assign.propertyType')
Expand Down
34 changes: 14 additions & 20 deletions src/Rules/RuleLevelHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
use PHPStan\Type\TypeTraverser;
use PHPStan\Type\TypeUtils;
use PHPStan\Type\UnionType;
use PHPStan\Type\VerbosityLevel;
use function count;
use function sprintf;

Expand Down Expand Up @@ -248,20 +247,17 @@ private function transformAcceptedType(Type $acceptingType, Type $acceptedType):
/** @api */
public function accepts(Type $acceptingType, Type $acceptedType, bool $strictTypes): RuleLevelHelperAcceptsResult
{
$witnessResult = $this->acceptsWitnessType($acceptingType, $acceptedType, $strictTypes);
if ($witnessResult !== null) {
return $witnessResult;
}

[$acceptedType, $checkForUnion] = $this->transformAcceptedType($acceptingType, $acceptedType);
$acceptingType = $this->transformCommonType($acceptingType);
[$transformedAcceptedType, $checkForUnion] = $this->transformAcceptedType($acceptingType, $acceptedType);
$transformedAcceptingType = $this->transformCommonType($acceptingType);

$accepts = $acceptingType->accepts($acceptedType, $strictTypes);
$accepts = $transformedAcceptingType->accepts($transformedAcceptedType, $strictTypes);
$result = $checkForUnion ? $accepts->yes() : !$accepts->no();
if (!$result) {
return new RuleLevelHelperAcceptsResult(false, $accepts->reasons);
}

return new RuleLevelHelperAcceptsResult(
$checkForUnion ? $accepts->yes() : !$accepts->no(),
$accepts->reasons,
);
return $this->acceptsWitnessType($acceptingType, $acceptedType, $strictTypes)
?? new RuleLevelHelperAcceptsResult(true, $accepts->reasons);
}

/**
Expand All @@ -279,22 +275,20 @@ private function acceptsWitnessType(Type $acceptingType, Type $acceptedType, boo
return null;
}

$checkedWitnessType = $witnessType;
if (!$this->checkNullables && !TypeCombinator::containsNull($acceptingType)) {
$witnessType = TypeCombinator::removeNull($witnessType);
if ($witnessType instanceof NeverType) {
$checkedWitnessType = TypeCombinator::removeNull($witnessType);
if ($checkedWitnessType instanceof NeverType) {
return null;
}
}

$accepts = $this->accepts($acceptingType, $witnessType, $strictTypes);
$accepts = $this->accepts($acceptingType, $checkedWitnessType, $strictTypes);
if ($accepts->result) {
return null;
}

return new RuleLevelHelperAcceptsResult(false, [
sprintf('Type %s is %s on some paths.', $acceptedType->describe(VerbosityLevel::typeOnly()), $witnessType->describe(VerbosityLevel::value())),
...$accepts->reasons,
]);
return new RuleLevelHelperAcceptsResult(false, $accepts->reasons, $witnessType);
}

/**
Expand Down
6 changes: 5 additions & 1 deletion src/Rules/RuleLevelHelperAcceptsResult.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace PHPStan\Rules;

use PHPStan\Turbo\ReferencedByTurboExtension;
use PHPStan\Type\Type;
use function array_merge;

/**
Expand All @@ -14,10 +15,12 @@ final class RuleLevelHelperAcceptsResult

/**
* @param list<string> $reasons
* @param Type|null $witnessType Set when the accepted type is a mixed accepted on its own, but its witness type, what some path is known to produce, is not. Rules report it as the given type.
*/
public function __construct(
public readonly bool $result,
public readonly array $reasons,
public readonly ?Type $witnessType = null,
)
{
}
Expand All @@ -27,6 +30,7 @@ public function and(self $other): self
return new self(
$this->result && $other->result,
array_merge($this->reasons, $other->reasons),
$this->witnessType ?? $other->witnessType,
);
}

Expand All @@ -40,7 +44,7 @@ public function decorateReasons(callable $cb): self
$reasons[] = $cb($reason);
}

return new self($this->result, $reasons);
return new self($this->result, $reasons, $this->witnessType);
}

}
2 changes: 1 addition & 1 deletion tests/PHPStan/Analyser/AnalyserIntegrationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -378,7 +378,7 @@ public function testBug11649(): void
// crash
$errors = $this->runAnalyse(__DIR__ . '/data/bug-11649.php');
$this->assertCount(1, $errors);
$this->assertSame('Anonymous function should return string but returns mixed.', $errors[0]->getMessage());
$this->assertSame('Anonymous function should return string but returns string|null.', $errors[0]->getMessage());
$this->assertSame(27, $errors[0]->getLine());
}

Expand Down
1 change: 1 addition & 0 deletions tests/PHPStan/Levels/LevelsIntegrationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ public static function dataTopics(): array
['missingTypes'],
['arrayOffsetAccess'],
['arraySum'],
['mixedWitness'],
];
if (PHP_VERSION_ID >= 80300) {
$topics[] = ['constantAccesses83'];
Expand Down
47 changes: 47 additions & 0 deletions tests/PHPStan/Levels/data/mixedWitness-10-missing.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
[
{
"message": "Method Levels\\MixedWitness\\Foo::returnExplicit() should return string but returns int.",
"line": 12,
"ignorable": true
},
{
"message": "Method Levels\\MixedWitness\\Foo::returnImplicit() should return string but returns int.",
"line": 17,
"ignorable": true
},
{
"message": "Method Levels\\MixedWitness\\Foo::returnImplicitOrUnion() should return string but returns int|null.",
"line": 32,
"ignorable": true
},
{
"message": "Parameter #1 $s of method Levels\\MixedWitness\\Foo::doString() expects string, int given.",
"line": 37,
"ignorable": true
},
{
"message": "Parameter #1 $s of method Levels\\MixedWitness\\Foo::doString() expects string, int given.",
"line": 42,
"ignorable": true
},
{
"message": "Property Levels\\MixedWitness\\Foo::$string (string) does not accept int.",
"line": 47,
"ignorable": true
},
{
"message": "Property Levels\\MixedWitness\\Foo::$string (string) does not accept int.",
"line": 52,
"ignorable": true
},
{
"message": "Method Levels\\MixedWitness\\Foo::returnExplicitOrNull() should return string but returns null.",
"line": 22,
"ignorable": true
},
{
"message": "Method Levels\\MixedWitness\\Foo::returnExplicitOrNullableString() should return string but returns string|null.",
"line": 27,
"ignorable": true
}
]
22 changes: 22 additions & 0 deletions tests/PHPStan/Levels/data/mixedWitness-10.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
[
{
"message": "Method Levels\\MixedWitness\\Foo::returnImplicit() should return string but returns mixed.",
"line": 17,
"ignorable": true
},
{
"message": "Method Levels\\MixedWitness\\Foo::returnImplicitOrUnion() should return string but returns mixed.",
"line": 32,
"ignorable": true
},
{
"message": "Parameter #1 $s of method Levels\\MixedWitness\\Foo::doString() expects string, mixed given.",
"line": 42,
"ignorable": true
},
{
"message": "Property Levels\\MixedWitness\\Foo::$string (string) does not accept mixed.",
"line": 52,
"ignorable": true
}
]
22 changes: 22 additions & 0 deletions tests/PHPStan/Levels/data/mixedWitness-6.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
[
{
"message": "Method Levels\\MixedWitness\\Foo::returnImplicit() has parameter $implicit with no type specified.",
"line": 15,
"ignorable": true
},
{
"message": "Method Levels\\MixedWitness\\Foo::returnImplicitOrUnion() has parameter $implicit with no type specified.",
"line": 30,
"ignorable": true
},
{
"message": "Method Levels\\MixedWitness\\Foo::argumentImplicit() has parameter $implicit with no type specified.",
"line": 40,
"ignorable": true
},
{
"message": "Method Levels\\MixedWitness\\Foo::propertyImplicit() has parameter $implicit with no type specified.",
"line": 50,
"ignorable": true
}
]
Loading
Loading