Skip to content
Prev Previous commit
Next Next commit
Restore SubstrDynamicReturnTypeExtension behavior and baseline its fi…
…nding

The previous commit removed the empty-string branch under `is_bool($substr)`
as dead code, reasoning that `$substr` is a bool only where
`substrReturnFalseInsteadOfEmptyString()` is true. That is a real regression:
the branch is live when PHPStan itself runs on a runtime PHP whose native
`substr()` returns `false` (old runtime) while analysing a target version
where substr yields `''`. There the runtime `substr()` call produces a bool,
`substrReturnFalseInsteadOfEmptyString()` is false, and the else branch
simulates the modern result by returning ConstantStringType(''). PHPStan's own
inference models `substr()` per the analysed version and cannot see the old
runtime path, so it reports the guard as always-true - a finding that must be
baselined, not resolved by deleting the branch.

Restore the file to its exact pre-commit content and record the single
`if.alwaysTrue` finding in the baseline instead. The other seven dead-code
removals in the previous commit are genuinely dead and stay removed.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01CGhnJQpUWRRpg6H5LuWJkA
  • Loading branch information
ondrejmirtes and claude committed Sep 11, 2026
commit d3c0b9dde12bad71a0f6015d11a05435da465090
6 changes: 6 additions & 0 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -1566,6 +1566,12 @@ parameters:
count: 1
path: src/Type/Php/ReflectionMethodConstructorThrowTypeExtension.php

-
rawMessage: If condition is always true.
identifier: if.alwaysTrue
count: 1
path: src/Type/Php/SubstrDynamicReturnTypeExtension.php

-
rawMessage: 'Doing instanceof PHPStan\Type\ObjectType is error-prone and deprecated. Use Type::isObject() or Type::getObjectClassNames() instead.'
identifier: phpstanApi.instanceofType
Expand Down
7 changes: 6 additions & 1 deletion src/Type/Php/SubstrDynamicReturnTypeExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,12 @@ public function getTypeFromFunctionCall(
}

if (is_bool($substr)) {
$results[] = new ConstantBooleanType($substr);
if ($this->phpVersion->substrReturnFalseInsteadOfEmptyString()) {
$results[] = new ConstantBooleanType($substr);
} else {
// Simulate substr call on a recent PHP version if the runtime one is too old.
$results[] = new ConstantStringType('');
}
} else {
$results[] = new ConstantStringType($substr);
}
Expand Down