Skip to content

Commit 81ed009

Browse files
phpstan-botclaude
andcommitted
Do not fire UsedAsStringNode for arguments passed to PHPDoc-only string parameters
A parameter typed only via @PARAM (no native typehint) has a native type of mixed. The previous "simplify" used !isString()->no() as the slot discriminator, which treats mixed as a string slot, so an argument passed to such a parameter fired UsedAsStringNode. Restore the "native type contains a string member" discriminator (flatten + isString()->yes()) so mixed is excluded while plain string and string|int still fire. Apply the same check to the property-assignment slot, which had the same latent mixed-fires issue. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
1 parent edb4503 commit 81ed009

3 files changed

Lines changed: 36 additions & 2 deletions

File tree

‎src/Analyser/ExprHandler/AssignHandler.php‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -258,7 +258,13 @@ private function isAssignToStringSlot(MutatingScope $scope, Expr $var, Expr $ass
258258
return false;
259259
}
260260

261-
return !$slotType->isString()->no();
261+
foreach (TypeUtils::flattenTypes($slotType) as $innerType) {
262+
if ($innerType->isString()->yes()) {
263+
return true;
264+
}
265+
}
266+
267+
return false;
262268
}
263269

264270
private function getAssignTargetPropertyNativeType(MutatingScope $scope, Expr $var): ?Type

‎src/Analyser/NodeScopeResolver.php‎

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3300,6 +3300,22 @@ private function processParamNode(
33003300
$this->callNodeCallback($nodeCallback, new UsedAsStringNode($param->default), $scope, $storage);
33013301
}
33023302

3303+
/**
3304+
* Whether the native type is a string slot: a plain `string` or a union that
3305+
* contains a `string` member (e.g. `string|int`). Plain `mixed` is not a string
3306+
* slot, so arguments to untyped or PHPDoc-only `@param string` parameters do not fire.
3307+
*/
3308+
private function isStringSlotType(Type $type): bool
3309+
{
3310+
foreach (TypeUtils::flattenTypes($type) as $slotMemberType) {
3311+
if ($slotMemberType->isString()->yes()) {
3312+
return true;
3313+
}
3314+
}
3315+
3316+
return false;
3317+
}
3318+
33033319
/**
33043320
* @param AttributeGroup[] $attrGroups
33053321
* @param callable(Node $node, Scope $scope): void $nodeCallback
@@ -3743,7 +3759,7 @@ public function processArgs(
37433759
$argStringSlotType = $parameterNativeType ?? ($parameter instanceof NativeParameterReflection ? $parameter->getType() : null);
37443760
if (
37453761
$argStringSlotType !== null
3746-
&& !$argStringSlotType->isString()->no()
3762+
&& $this->isStringSlotType($argStringSlotType)
37473763
&& !ExprUsedAsStringVisitor::isAlreadyUsedAsStringSite($arg->value)
37483764
) {
37493765
$this->callNodeCallback($nodeCallback, new UsedAsStringNode($arg->value), $scopeToPass, $storage);

‎tests/PHPStan/Node/data/used-as-string.php‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,3 +142,15 @@ function arrayAssignment(): void
142142
{
143143
$bar = array('Bar1', 'Bar2', 'Bar3');
144144
}
145+
146+
/**
147+
* @param string $s
148+
*/
149+
function takesPhpDocString($s): void
150+
{
151+
}
152+
153+
function passPhpDocStringArgument(): void
154+
{
155+
takesPhpDocString('phpdoc string argument');
156+
}

0 commit comments

Comments
 (0)