Skip to content

Commit ca0a7d6

Browse files
committed
Report the witness type of mixed as the given type
When mixed is accepted but its witness type is not, rules now describe the witness as the given type: `return $c ? $mixed : 1;` in a function returning string reports "should return string but returns int." from level 7 instead of "returns mixed" with a tip saying mixed is int on some paths. RuleLevelHelper::accepts() checks the mixed itself first, so a mixed that is rejected on its own - explicit mixed from level 9, implicit mixed on level 10 - is still reported as mixed. RuleLevelHelperAcceptsResult carries the rejected witness type for the rules describing the given type. The native extension mirrors it. Amp-Thread-ID: https://ampcode.com/threads/T-01a237b0-3e9f-77ea-bd9b-d9a06b69686b
1 parent bb4f3d7 commit ca0a7d6

21 files changed

Lines changed: 308 additions & 79 deletions

‎src/Rules/FunctionCallParametersCheck.php‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -505,12 +505,13 @@ public function check(
505505
}
506506

507507
if (!$accepts->result) {
508-
$verbosityLevel = VerbosityLevel::getRecommendedLevelByType($parameterType, $argumentValueType);
508+
$givenType = $accepts->witnessType ?? $argumentValueType;
509+
$verbosityLevel = VerbosityLevel::getRecommendedLevelByType($parameterType, $givenType);
509510
$errors[] = RuleErrorBuilder::message(sprintf(
510511
$wrongArgumentTypeMessage,
511512
$this->describeParameter($parameter, $argumentName ?? $i + 1),
512513
$parameterType->describe($verbosityLevel),
513-
$argumentValueType->describe($verbosityLevel),
514+
$givenType->describe($verbosityLevel),
514515
))
515516
->identifier('argument.type')
516517
->line($argumentLine)

‎src/Rules/FunctionReturnTypeCheck.php‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,13 +92,14 @@ public function checkReturnType(
9292

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

9798
return [
9899
RuleErrorBuilder::message(sprintf(
99100
$typeMismatchMessage,
100101
$returnType->describe($verbosityLevel),
101-
$returnValueType->describe($verbosityLevel),
102+
$givenType->describe($verbosityLevel),
102103
))
103104
->line($returnNode->getStartLine())
104105
->identifier('return.type')

‎src/Rules/Functions/ReturnTypeAfterFinallyRule.php‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,12 +90,13 @@ public function processNode(Node $node, Scope $scope): array
9090
continue;
9191
}
9292

93-
$verbosityLevel = VerbosityLevel::getRecommendedLevelByType($returnType, $typeAfterFinally);
93+
$givenType = $accepts->witnessType ?? $typeAfterFinally;
94+
$verbosityLevel = VerbosityLevel::getRecommendedLevelByType($returnType, $givenType);
9495
$errors[] = RuleErrorBuilder::message(sprintf(
9596
'%s should return %s but returns %s because the finally block modifies the value returned by reference.',
9697
$description,
9798
$returnType->describe($verbosityLevel),
98-
$typeAfterFinally->describe($verbosityLevel),
99+
$givenType->describe($verbosityLevel),
99100
))
100101
->line($returnNode->getStartLine())
101102
->identifier('return.byRefFinally')

‎src/Rules/Generators/YieldFromTypeRule.php‎

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -84,11 +84,12 @@ public function processNode(Node $node, Scope $scope): array
8484
$messages = [];
8585
$acceptsKey = $this->ruleLevelHelper->accepts($returnType->getIterableKeyType(), $exprType->getIterableKeyType(), $scope->isDeclareStrictTypes());
8686
if (!$acceptsKey->result) {
87-
$verbosityLevel = VerbosityLevel::getRecommendedLevelByType($returnType->getIterableKeyType(), $exprType->getIterableKeyType());
87+
$givenKeyType = $acceptsKey->witnessType ?? $exprType->getIterableKeyType();
88+
$verbosityLevel = VerbosityLevel::getRecommendedLevelByType($returnType->getIterableKeyType(), $givenKeyType);
8889
$messages[] = RuleErrorBuilder::message(sprintf(
8990
'Generator expects key type %s, %s given.',
9091
$returnType->getIterableKeyType()->describe($verbosityLevel),
91-
$exprType->getIterableKeyType()->describe($verbosityLevel),
92+
$givenKeyType->describe($verbosityLevel),
9293
))
9394
->line($node->expr->getStartLine())
9495
->identifier('generator.keyType')
@@ -98,11 +99,12 @@ public function processNode(Node $node, Scope $scope): array
9899

99100
$acceptsValue = $this->ruleLevelHelper->accepts($returnType->getIterableValueType(), $exprType->getIterableValueType(), $scope->isDeclareStrictTypes());
100101
if (!$acceptsValue->result) {
101-
$verbosityLevel = VerbosityLevel::getRecommendedLevelByType($returnType->getIterableValueType(), $exprType->getIterableValueType());
102+
$givenValueType = $acceptsValue->witnessType ?? $exprType->getIterableValueType();
103+
$verbosityLevel = VerbosityLevel::getRecommendedLevelByType($returnType->getIterableValueType(), $givenValueType);
102104
$messages[] = RuleErrorBuilder::message(sprintf(
103105
'Generator expects value type %s, %s given.',
104106
$returnType->getIterableValueType()->describe($verbosityLevel),
105-
$exprType->getIterableValueType()->describe($verbosityLevel),
107+
$givenValueType->describe($verbosityLevel),
106108
))
107109
->line($node->expr->getStartLine())
108110
->identifier('generator.valueType')

‎src/Rules/Generators/YieldTypeRule.php‎

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,11 +57,12 @@ public function processNode(Node $node, Scope $scope): array
5757
$messages = [];
5858
$acceptsKey = $this->ruleLevelHelper->accepts($returnType->getIterableKeyType(), $keyType, $scope->isDeclareStrictTypes());
5959
if (!$acceptsKey->result) {
60-
$verbosityLevel = VerbosityLevel::getRecommendedLevelByType($returnType->getIterableKeyType(), $keyType);
60+
$givenKeyType = $acceptsKey->witnessType ?? $keyType;
61+
$verbosityLevel = VerbosityLevel::getRecommendedLevelByType($returnType->getIterableKeyType(), $givenKeyType);
6162
$messages[] = RuleErrorBuilder::message(sprintf(
6263
'Generator expects key type %s, %s given.',
6364
$returnType->getIterableKeyType()->describe($verbosityLevel),
64-
$keyType->describe($verbosityLevel),
65+
$givenKeyType->describe($verbosityLevel),
6566
))
6667
->acceptsReasonsTip($acceptsKey->reasons)
6768
->identifier('generator.keyType')
@@ -76,11 +77,12 @@ public function processNode(Node $node, Scope $scope): array
7677

7778
$acceptsValue = $this->ruleLevelHelper->accepts($returnType->getIterableValueType(), $valueType, $scope->isDeclareStrictTypes());
7879
if (!$acceptsValue->result) {
79-
$verbosityLevel = VerbosityLevel::getRecommendedLevelByType($returnType->getIterableValueType(), $valueType);
80+
$givenValueType = $acceptsValue->witnessType ?? $valueType;
81+
$verbosityLevel = VerbosityLevel::getRecommendedLevelByType($returnType->getIterableValueType(), $givenValueType);
8082
$messages[] = RuleErrorBuilder::message(sprintf(
8183
'Generator expects value type %s, %s given.',
8284
$returnType->getIterableValueType()->describe($verbosityLevel),
83-
$valueType->describe($verbosityLevel),
85+
$givenValueType->describe($verbosityLevel),
8486
))
8587
->acceptsReasonsTip($acceptsValue->reasons)
8688
->identifier('generator.valueType')

‎src/Rules/Properties/DefaultValueTypesAssignedToPropertiesRule.php‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,8 @@ public function processNode(Node $node, Scope $scope): array
5050
return [];
5151
}
5252

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

5556
return [
5657
RuleErrorBuilder::message(sprintf(
@@ -59,7 +60,7 @@ public function processNode(Node $node, Scope $scope): array
5960
$classReflection->getDisplayName(),
6061
$node->getName(),
6162
$propertyType->describe($verbosityLevel),
62-
$defaultValueType->describe($verbosityLevel),
63+
$givenType->describe($verbosityLevel),
6364
))
6465
->identifier('property.defaultValue')
6566
->acceptsReasonsTip($accepts->reasons)

‎src/Rules/Properties/TypesAssignedToPropertiesRule.php‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,14 +90,15 @@ private function processSingleProperty(
9090
$accepts = $this->ruleLevelHelper->accepts($propertyType, $assignedValueType, $scope->isDeclareStrictTypes());
9191
if (!$accepts->result) {
9292
$propertyDescription = $this->describePropertyByName($propertyReflection, $propertyReflection->getName());
93-
$verbosityLevel = VerbosityLevel::getRecommendedLevelByType($propertyType, $assignedValueType);
93+
$givenType = $accepts->witnessType ?? $assignedValueType;
94+
$verbosityLevel = VerbosityLevel::getRecommendedLevelByType($propertyType, $givenType);
9495

9596
return [
9697
RuleErrorBuilder::message(sprintf(
9798
'%s (%s) does not accept %s.',
9899
$propertyDescription,
99100
$propertyType->describe($verbosityLevel),
100-
$assignedValueType->describe($verbosityLevel),
101+
$givenType->describe($verbosityLevel),
101102
))
102103
->line($assignedExpr->getStartLine())
103104
->identifier('assign.propertyType')

‎src/Rules/RuleLevelHelper.php‎

Lines changed: 14 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727
use PHPStan\Type\TypeTraverser;
2828
use PHPStan\Type\TypeUtils;
2929
use PHPStan\Type\UnionType;
30-
use PHPStan\Type\VerbosityLevel;
3130
use function count;
3231
use function sprintf;
3332

@@ -248,20 +247,17 @@ private function transformAcceptedType(Type $acceptingType, Type $acceptedType):
248247
/** @api */
249248
public function accepts(Type $acceptingType, Type $acceptedType, bool $strictTypes): RuleLevelHelperAcceptsResult
250249
{
251-
$witnessResult = $this->acceptsWitnessType($acceptingType, $acceptedType, $strictTypes);
252-
if ($witnessResult !== null) {
253-
return $witnessResult;
254-
}
255-
256-
[$acceptedType, $checkForUnion] = $this->transformAcceptedType($acceptingType, $acceptedType);
257-
$acceptingType = $this->transformCommonType($acceptingType);
250+
[$transformedAcceptedType, $checkForUnion] = $this->transformAcceptedType($acceptingType, $acceptedType);
251+
$transformedAcceptingType = $this->transformCommonType($acceptingType);
258252

259-
$accepts = $acceptingType->accepts($acceptedType, $strictTypes);
253+
$accepts = $transformedAcceptingType->accepts($transformedAcceptedType, $strictTypes);
254+
$result = $checkForUnion ? $accepts->yes() : !$accepts->no();
255+
if (!$result) {
256+
return new RuleLevelHelperAcceptsResult(false, $accepts->reasons);
257+
}
260258

261-
return new RuleLevelHelperAcceptsResult(
262-
$checkForUnion ? $accepts->yes() : !$accepts->no(),
263-
$accepts->reasons,
264-
);
259+
return $this->acceptsWitnessType($acceptingType, $acceptedType, $strictTypes)
260+
?? new RuleLevelHelperAcceptsResult(true, $accepts->reasons);
265261
}
266262

267263
/**
@@ -279,22 +275,20 @@ private function acceptsWitnessType(Type $acceptingType, Type $acceptedType, boo
279275
return null;
280276
}
281277

278+
$checkedWitnessType = $witnessType;
282279
if (!$this->checkNullables && !TypeCombinator::containsNull($acceptingType)) {
283-
$witnessType = TypeCombinator::removeNull($witnessType);
284-
if ($witnessType instanceof NeverType) {
280+
$checkedWitnessType = TypeCombinator::removeNull($witnessType);
281+
if ($checkedWitnessType instanceof NeverType) {
285282
return null;
286283
}
287284
}
288285

289-
$accepts = $this->accepts($acceptingType, $witnessType, $strictTypes);
286+
$accepts = $this->accepts($acceptingType, $checkedWitnessType, $strictTypes);
290287
if ($accepts->result) {
291288
return null;
292289
}
293290

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

300294
/**

‎src/Rules/RuleLevelHelperAcceptsResult.php‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace PHPStan\Rules;
44

55
use PHPStan\Turbo\ReferencedByTurboExtension;
6+
use PHPStan\Type\Type;
67
use function array_merge;
78

89
/**
@@ -14,10 +15,12 @@ final class RuleLevelHelperAcceptsResult
1415

1516
/**
1617
* @param list<string> $reasons
18+
* @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.
1719
*/
1820
public function __construct(
1921
public readonly bool $result,
2022
public readonly array $reasons,
23+
public readonly ?Type $witnessType = null,
2124
)
2225
{
2326
}
@@ -27,6 +30,7 @@ public function and(self $other): self
2730
return new self(
2831
$this->result && $other->result,
2932
array_merge($this->reasons, $other->reasons),
33+
$this->witnessType ?? $other->witnessType,
3034
);
3135
}
3236

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

43-
return new self($this->result, $reasons);
47+
return new self($this->result, $reasons, $this->witnessType);
4448
}
4549

4650
}

‎tests/PHPStan/Analyser/AnalyserIntegrationTest.php‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -378,7 +378,7 @@ public function testBug11649(): void
378378
// crash
379379
$errors = $this->runAnalyse(__DIR__ . '/data/bug-11649.php');
380380
$this->assertCount(1, $errors);
381-
$this->assertSame('Anonymous function should return string but returns mixed.', $errors[0]->getMessage());
381+
$this->assertSame('Anonymous function should return string but returns string|null.', $errors[0]->getMessage());
382382
$this->assertSame(27, $errors[0]->getLine());
383383
}
384384

0 commit comments

Comments
 (0)