Skip to content

Commit be6f4de

Browse files
ondrejmirtesclaude
andcommitted
Describe types with getRecommendedLevelByType in impossible haystack value errors
Instead of VerbosityLevel::precise(), which leaked internal notation like mixed~'installed' into the message. The resulting ambiguity (needle type "mixed") is resolved the same way StrictComparisonOfDifferentTypesRule does it: the reasons from resolveIdenticalType()/resolveEqualType() are attached via acceptsReasonsTip(). Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01N3Ak8RnnYpuCDFH9fFQ1um
1 parent a93789f commit be6f4de

3 files changed

Lines changed: 55 additions & 13 deletions

File tree

‎src/Rules/Comparison/ImpossibleInArrayHaystackFiniteTypesRule.php‎

Lines changed: 39 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@
1313
use PHPStan\Type\Type;
1414
use PHPStan\Type\VerbosityLevel;
1515
use function array_key_exists;
16+
use function array_merge;
17+
use function array_unique;
18+
use function array_values;
1619
use function count;
1720
use function max;
1821
use function sprintf;
@@ -79,11 +82,13 @@ public function processNode(Node $node, Scope $scope): array
7982
? ($this->treatPhpDocTypesAsCertain ? $scope->getType($args[2]->value) : $scope->getNativeType($args[2]->value))->isTrue()
8083
: TrinaryLogic::createNo();
8184

85+
/** @var array<string, array{Type, list<string>}> $deadValueTypes */
8286
$deadValueTypes = [];
8387
$anyPossibleMatch = false;
8488
foreach ($constantArrays as $constantArray) {
8589
foreach ($constantArray->getValueTypes() as $valueType) {
86-
if (!$this->canNeverMatch($needleType, $valueType, $isStrict)) {
90+
$canNeverMatchReasons = $this->findCanNeverMatchReasons($needleType, $valueType, $isStrict);
91+
if ($canNeverMatchReasons === null) {
8792
$anyPossibleMatch = true;
8893
continue;
8994
}
@@ -92,7 +97,7 @@ public function processNode(Node $node, Scope $scope): array
9297
continue;
9398
}
9499

95-
$deadValueTypes[$valueType->describe(VerbosityLevel::precise())] = $valueType;
100+
$deadValueTypes[$valueType->describe(VerbosityLevel::precise())] = [$valueType, $canNeverMatchReasons];
96101
}
97102
}
98103

@@ -106,36 +111,57 @@ public function processNode(Node $node, Scope $scope): array
106111
$verb = $isStrict->no() ? 'equal to' : 'identical to';
107112

108113
$errors = [];
109-
foreach ($deadValueTypes as $valueType) {
110-
$errors[] = $this->buildError($valueType, $needleType, $functionName, $verb);
114+
foreach ($deadValueTypes as [$valueType, $reasons]) {
115+
$errors[] = $this->buildError($valueType, $needleType, $functionName, $verb, $reasons);
111116
}
112117

113118
return $errors;
114119
}
115120

116-
private function canNeverMatch(Type $needleType, Type $valueType, TrinaryLogic $isStrict): bool
121+
/**
122+
* @return list<string>|null Reasons for the impossibility when the value can never match, null when it can match.
123+
*/
124+
private function findCanNeverMatchReasons(Type $needleType, Type $valueType, TrinaryLogic $isStrict): ?array
117125
{
118126
if ($isStrict->yes()) {
119-
return $this->initializerExprTypeResolver->resolveIdenticalType($needleType, $valueType)->type->isFalse()->yes();
127+
$result = $this->initializerExprTypeResolver->resolveIdenticalType($needleType, $valueType);
128+
129+
return $result->type->isFalse()->yes() ? $result->reasons : null;
120130
}
121131

122132
if ($isStrict->no()) {
123-
return $this->initializerExprTypeResolver->resolveEqualType($needleType, $valueType)->type->isFalse()->yes();
133+
$result = $this->initializerExprTypeResolver->resolveEqualType($needleType, $valueType);
134+
135+
return $result->type->isFalse()->yes() ? $result->reasons : null;
136+
}
137+
138+
$identicalResult = $this->initializerExprTypeResolver->resolveIdenticalType($needleType, $valueType);
139+
if (!$identicalResult->type->isFalse()->yes()) {
140+
return null;
141+
}
142+
143+
$equalResult = $this->initializerExprTypeResolver->resolveEqualType($needleType, $valueType);
144+
if (!$equalResult->type->isFalse()->yes()) {
145+
return null;
124146
}
125147

126-
return $this->initializerExprTypeResolver->resolveIdenticalType($needleType, $valueType)->type->isFalse()->yes()
127-
&& $this->initializerExprTypeResolver->resolveEqualType($needleType, $valueType)->type->isFalse()->yes();
148+
return array_values(array_unique(array_merge($identicalResult->reasons, $equalResult->reasons)));
128149
}
129150

130-
private function buildError(Type $valueType, Type $needleType, string $functionName, string $verb): IdentifierRuleError
151+
/**
152+
* @param list<string> $reasons
153+
*/
154+
private function buildError(Type $valueType, Type $needleType, string $functionName, string $verb, array $reasons): IdentifierRuleError
131155
{
132156
return RuleErrorBuilder::message(sprintf(
133157
'Value %s in the haystack passed to %s() can never be %s the needle type %s.',
134-
$valueType->describe(VerbosityLevel::precise()),
158+
$valueType->describe(VerbosityLevel::getRecommendedLevelByType($valueType)),
135159
$functionName,
136160
$verb,
137-
$needleType->describe(VerbosityLevel::precise()),
138-
))->identifier('function.impossibleHaystackValue')->build();
161+
$needleType->describe(VerbosityLevel::getRecommendedLevelByType($needleType)),
162+
))->identifier('function.impossibleHaystackValue')
163+
->acceptsReasonsTip($reasons)
164+
->build();
139165
}
140166

141167
}

‎tests/PHPStan/Rules/Comparison/ImpossibleInArrayHaystackFiniteTypesRuleTest.php‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,11 @@ public function testRule(): void
4545
'Value ImpossibleInArrayFiniteTypes\Foo::TWO in the haystack passed to in_array() can never be identical to the needle type ImpossibleInArrayFiniteTypes\Foo::ONE.',
4646
48,
4747
],
48+
[
49+
'Value \'installed\' in the haystack passed to in_array() can never be identical to the needle type mixed.',
50+
98,
51+
'Type \'installed\' has already been eliminated from mixed.',
52+
],
4853
]);
4954
}
5055

‎tests/PHPStan/Rules/Comparison/data/impossible-in-array-finite-types.php‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,3 +88,14 @@ function noErrorUnionNeedleMatches(int|string $i): void
8888
echo 'yes';
8989
}
9090
}
91+
92+
function reportedValueEliminatedFromNeedle(mixed $status): void
93+
{
94+
if ($status === 'installed') {
95+
return;
96+
}
97+
98+
if (in_array($status, ['installed', 'active'], true)) {
99+
echo 'yes';
100+
}
101+
}

0 commit comments

Comments
 (0)