Skip to content

Commit dc73664

Browse files
committed
Keep the analysed PHP version as the floor of a PHP_VERSION_ID < X guard
When a single PHP version is analysed (a fixed `phpVersion`, or no version range from the config or composer.json), PHP_VERSION_ID is int<50207, max> and Scope::getPhpVersion() treats that as no information and answers with the analysed version. A guard like `if (PHP_VERSION_ID < 80400)` narrows the constant to int<50207, 80399>, and getPhpVersion() returned that range as is, so the version dropped to PHP 5.2.7 inside the branch. Since the "supported only on PHP X" rules read the version from the scope, a class with promoted properties in such a branch got a non-ignorable property.promotedNotSupported error with `phpVersion: 80500`. Only the upper bound of that range comes from the guard. Raise the lower bound to the analysed version, or to the guard's upper bound when the guard excludes the analysed version. Guards with a lower bound (`PHP_VERSION_ID >= 80300`) and configured version ranges are unchanged. The turbo-ext mirror gets the same change. Closes phpstan/phpstan#15425
1 parent e647591 commit dc73664

6 files changed

Lines changed: 200 additions & 75 deletions

File tree

‎src/Analyser/MutatingScope.php‎

Lines changed: 43 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,7 @@
136136
use function is_string;
137137
use function ltrim;
138138
use function md5;
139+
use function min;
139140
use function preg_match;
140141
use function preg_match_all;
141142
use function spl_object_id;
@@ -6020,25 +6021,56 @@ public function getPhpVersion(): PhpVersions
60206021
{
60216022
$constType = $this->getGlobalConstantType(new Name('PHP_VERSION_ID'));
60226023
if ($constType !== null && !$this->isOverallPhpVersionRange($constType)) {
6024+
if (
6025+
$constType instanceof IntegerRangeType
6026+
&& $constType->getMin() === ConstantResolver::PHP_MIN_ANALYZABLE_VERSION_ID
6027+
&& $constType->getMax() !== null
6028+
&& $this->getConfiguredPhpVersionRange() === null
6029+
) {
6030+
// A guard like `PHP_VERSION_ID < 80400` only gives an upper bound. Without
6031+
// a configured version range, the lower bound is merely the lowest version
6032+
// PHPStan knows, while outside the guard the single analysed PHP version
6033+
// applies. Keep the lower bound at that version, or as close to it as
6034+
// the guard allows.
6035+
$maxVersionId = $constType->getMax();
6036+
return new PhpVersions(IntegerRangeType::fromInterval(
6037+
min($this->phpVersion->getVersionId(), $maxVersionId),
6038+
$maxVersionId,
6039+
));
6040+
}
6041+
60236042
return new PhpVersions($constType);
60246043
}
60256044

6026-
// The analysed PHP version range comes either from the NEON phpVersion min/max
6027-
// config or from the composer.json "require.php" constraint - the very same
6028-
// source ConstantResolver narrows PHP_VERSION_ID with, so that
6029-
// Scope::getPhpVersion() never contradicts the PHP_VERSION_ID constant.
6045+
$configuredRange = $this->getConfiguredPhpVersionRange();
6046+
if ($configuredRange !== null) {
6047+
return new PhpVersions($configuredRange);
6048+
}
6049+
6050+
return new PhpVersions(new ConstantIntegerType($this->phpVersion->getVersionId()));
6051+
}
6052+
6053+
/**
6054+
* The analysed PHP version range comes either from the NEON phpVersion min/max
6055+
* config or from the composer.json "require.php" constraint - the very same
6056+
* source ConstantResolver narrows PHP_VERSION_ID with, so that
6057+
* Scope::getPhpVersion() never contradicts the PHP_VERSION_ID constant.
6058+
* Null when neither is set and a single PHP version is analysed.
6059+
*/
6060+
private function getConfiguredPhpVersionRange(): ?Type
6061+
{
60306062
[$minPhpVersion, $maxPhpVersion] = $this->configuredPhpVersionRangeHelper->getVersionRange();
60316063
if (
6032-
$minPhpVersion !== null
6033-
|| ($maxPhpVersion !== null && $maxPhpVersion->getVersionId() !== PhpVersionFactory::MAX_PHP_VERSION)
6064+
$minPhpVersion === null
6065+
&& ($maxPhpVersion === null || $maxPhpVersion->getVersionId() === PhpVersionFactory::MAX_PHP_VERSION)
60346066
) {
6035-
return new PhpVersions(IntegerRangeType::fromInterval(
6036-
$minPhpVersion !== null ? $minPhpVersion->getVersionId() : ConstantResolver::PHP_MIN_ANALYZABLE_VERSION_ID,
6037-
$maxPhpVersion !== null ? $maxPhpVersion->getVersionId() : null,
6038-
));
6067+
return null;
60396068
}
60406069

6041-
return new PhpVersions(new ConstantIntegerType($this->phpVersion->getVersionId()));
6070+
return IntegerRangeType::fromInterval(
6071+
$minPhpVersion !== null ? $minPhpVersion->getVersionId() : ConstantResolver::PHP_MIN_ANALYZABLE_VERSION_ID,
6072+
$maxPhpVersion !== null ? $maxPhpVersion->getVersionId() : null,
6073+
);
60426074
}
60436075

60446076
/**

‎tests/PHPStan/Rules/Classes/InvalidPromotedPropertiesRuleTest.php‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,4 +141,10 @@ public function testConditionallyDeclaredClass(): void
141141
$this->analyse([__DIR__ . '/data/promoted-properties-php-versions.php'], $errors);
142142
}
143143

144+
#[RequiresPhp('>= 8.0.0')]
145+
public function testBug15425(): void
146+
{
147+
$this->analyse([__DIR__ . '/data/bug-15425.php'], []);
148+
}
149+
144150
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php // lint >= 8.0
2+
3+
declare(strict_types = 1);
4+
5+
namespace Bug15425;
6+
7+
if (PHP_VERSION_ID < 80400) {
8+
\PHPStan\dumpType(PHP_VERSION_ID);
9+
10+
final class Foo
11+
{
12+
public function __construct(public ?string $message = null)
13+
{
14+
}
15+
}
16+
}

‎turbo-ext/src/MutatingScope.cpp‎

Lines changed: 71 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -3908,24 +3908,71 @@ class MutatingScope
39083908
if (UNEXPECTED(constType.isUndef())) return zv::Val();
39093909

39103910
bool isOverallPhpVersionRange = false;
3911+
bool hasMinAnalyzableVersion = false;
3912+
zv::Val max;
39113913
if (constType.ref().isObject() && constType.ref().instanceOf(pt_ce_integer_range_type)) {
39123914
zv::Val min = pt_type_call(constType.ref().asObject(), PT_LC("getmin"), 0, NULL);
39133915
if (UNEXPECTED(min.isUndef())) return zv::Val();
39143916
if (min.ref().isLong() && min.ref().asLong() == PT_MS_PHP_MIN_ANALYZABLE_VERSION_ID) {
3915-
zv::Val max = pt_type_call(constType.ref().asObject(), PT_LC("getmax"), 0, NULL);
3917+
hasMinAnalyzableVersion = true;
3918+
max = pt_type_call(constType.ref().asObject(), PT_LC("getmax"), 0, NULL);
39163919
if (UNEXPECTED(max.isUndef())) return zv::Val();
39173920
if (max.isNull() || (max.ref().isLong() && max.ref().asLong() == PT_MS_MAX_PHP_VERSION)) {
39183921
isOverallPhpVersionRange = true;
39193922
}
39203923
}
39213924
}
39223925

3923-
if (!constType.isNull() && !isOverallPhpVersionRange) return pt_type_new(PT_CLASS_PHP_VERSIONS, 1, constType.raw());
3926+
if (!constType.isNull() && !isOverallPhpVersionRange) {
3927+
if (hasMinAnalyzableVersion && max.ref().isLong()) {
3928+
zv::Val configuredRange = getConfiguredPhpVersionRange();
3929+
if (UNEXPECTED(configuredRange.isUndef())) return zv::Val();
3930+
if (configuredRange.isNull()) {
3931+
// A guard like `PHP_VERSION_ID < 80400` only gives an upper bound. Without
3932+
// a configured version range, the lower bound is merely the lowest version
3933+
// PHPStan knows, while outside the guard the single analysed PHP version
3934+
// applies. Keep the lower bound at that version, or as close to it as
3935+
// the guard allows.
3936+
zv::Ref phpVersion = slot(PT_MS_PROP_PHP_VERSION);
3937+
if (UNEXPECTED(!phpVersion.isObject())) return uninitializedProperty("phpVersion");
3938+
zv::Val versionId = pt_php_version_get_version_id(phpVersion.raw());
3939+
if (UNEXPECTED(versionId.isUndef())) return zv::Val();
3940+
zend_long maxVersionId = max.ref().asLong();
3941+
zend_long minVersionId = zval_get_long(versionId.raw());
3942+
zval interval[2];
3943+
ZVAL_LONG(&interval[0], minVersionId < maxVersionId ? minVersionId : maxVersionId);
3944+
ZVAL_LONG(&interval[1], maxVersionId);
3945+
zv::Val versionRange = pt_type_call_static_ce(pt_ce_integer_range_type, PT_LC("frominterval"), 2, interval);
3946+
if (UNEXPECTED(versionRange.isUndef())) return zv::Val();
3947+
return pt_type_new(PT_CLASS_PHP_VERSIONS, 1, versionRange.raw());
3948+
}
3949+
}
3950+
3951+
return pt_type_new(PT_CLASS_PHP_VERSIONS, 1, constType.raw());
3952+
}
3953+
3954+
zv::Val configuredRange = getConfiguredPhpVersionRange();
3955+
if (UNEXPECTED(configuredRange.isUndef())) return zv::Val();
3956+
if (!configuredRange.isNull()) return pt_type_new(PT_CLASS_PHP_VERSIONS, 1, configuredRange.raw());
3957+
3958+
zv::Ref phpVersion = slot(PT_MS_PROP_PHP_VERSION);
3959+
if (UNEXPECTED(!phpVersion.isObject())) return uninitializedProperty("phpVersion");
3960+
zv::Val versionId = pt_php_version_get_version_id(phpVersion.raw());
3961+
if (UNEXPECTED(versionId.isUndef())) return zv::Val();
3962+
zval constantInteger;
3963+
if (UNEXPECTED(!pt_constant_integer_type_new(&constantInteger, zval_get_long(versionId.raw())))) return zv::Val();
3964+
zv::Val constantIntegerType = zv::Val::adopt(constantInteger);
3965+
return pt_type_new(PT_CLASS_PHP_VERSIONS, 1, constantIntegerType.raw());
3966+
}
39243967

3925-
// The analysed PHP version range comes either from the NEON phpVersion min/max
3926-
// config or from the composer.json "require.php" constraint - the very same
3927-
// source ConstantResolver narrows PHP_VERSION_ID with, so that
3928-
// Scope::getPhpVersion() never contradicts the PHP_VERSION_ID constant.
3968+
/* The analysed PHP version range comes either from the NEON phpVersion min/max
3969+
* config or from the composer.json "require.php" constraint - the very same
3970+
* source ConstantResolver narrows PHP_VERSION_ID with, so that
3971+
* Scope::getPhpVersion() never contradicts the PHP_VERSION_ID constant.
3972+
* NULL when neither is set and a single PHP version is analysed; UNDEF =
3973+
* pending exception */
3974+
zv::Val getConfiguredPhpVersionRange()
3975+
{
39293976
if (UNEXPECTED(!requireSlot(PT_MS_PROP_CONFIGURED_PHP_VERSION_RANGE_HELPER, "configuredPhpVersionRangeHelper"))) return zv::Val();
39303977
zv::Ref rangeHelper = slot(PT_MS_PROP_CONFIGURED_PHP_VERSION_RANGE_HELPER);
39313978
zv::Val range = pt_type_call(rangeHelper.asObject(), PT_LC("getversionrange"), 0, NULL);
@@ -3952,36 +3999,25 @@ class MutatingScope
39523999
if (UNEXPECTED(maxVersionId.isUndef())) return zv::Val();
39534000
narrowed = !(maxVersionId.ref().isLong() && maxVersionId.ref().asLong() == PT_MS_MAX_PHP_VERSION);
39544001
}
3955-
if (narrowed) {
3956-
zval interval[2];
3957-
zv::Val minVersionId, maxVersionId;
3958-
if (minPhpVersion != NULL) {
3959-
minVersionId = pt_php_version_get_version_id(minPhpVersion);
3960-
if (UNEXPECTED(minVersionId.isUndef())) return zv::Val();
3961-
ZVAL_COPY_VALUE(&interval[0], minVersionId.raw());
3962-
} else {
3963-
ZVAL_LONG(&interval[0], PT_MS_PHP_MIN_ANALYZABLE_VERSION_ID);
3964-
}
3965-
if (maxPhpVersion != NULL) {
3966-
maxVersionId = pt_php_version_get_version_id(maxPhpVersion);
3967-
if (UNEXPECTED(maxVersionId.isUndef())) return zv::Val();
3968-
ZVAL_COPY_VALUE(&interval[1], maxVersionId.raw());
3969-
} else {
3970-
ZVAL_NULL(&interval[1]);
3971-
}
3972-
zv::Val versionRange = pt_type_call_static_ce(pt_ce_integer_range_type, PT_LC("frominterval"), 2, interval);
3973-
if (UNEXPECTED(versionRange.isUndef())) return zv::Val();
3974-
return pt_type_new(PT_CLASS_PHP_VERSIONS, 1, versionRange.raw());
3975-
}
4002+
if (!narrowed) return zv::Val::null();
39764003

3977-
zv::Ref phpVersion = slot(PT_MS_PROP_PHP_VERSION);
3978-
if (UNEXPECTED(!phpVersion.isObject())) return uninitializedProperty("phpVersion");
3979-
zv::Val versionId = pt_php_version_get_version_id(phpVersion.raw());
3980-
if (UNEXPECTED(versionId.isUndef())) return zv::Val();
3981-
zval constantInteger;
3982-
if (UNEXPECTED(!pt_constant_integer_type_new(&constantInteger, zval_get_long(versionId.raw())))) return zv::Val();
3983-
zv::Val constantIntegerType = zv::Val::adopt(constantInteger);
3984-
return pt_type_new(PT_CLASS_PHP_VERSIONS, 1, constantIntegerType.raw());
4004+
zval interval[2];
4005+
zv::Val minVersionId, maxVersionId;
4006+
if (minPhpVersion != NULL) {
4007+
minVersionId = pt_php_version_get_version_id(minPhpVersion);
4008+
if (UNEXPECTED(minVersionId.isUndef())) return zv::Val();
4009+
ZVAL_COPY_VALUE(&interval[0], minVersionId.raw());
4010+
} else {
4011+
ZVAL_LONG(&interval[0], PT_MS_PHP_MIN_ANALYZABLE_VERSION_ID);
4012+
}
4013+
if (maxPhpVersion != NULL) {
4014+
maxVersionId = pt_php_version_get_version_id(maxPhpVersion);
4015+
if (UNEXPECTED(maxVersionId.isUndef())) return zv::Val();
4016+
ZVAL_COPY_VALUE(&interval[1], maxVersionId.raw());
4017+
} else {
4018+
ZVAL_NULL(&interval[1]);
4019+
}
4020+
return pt_type_call_static_ce(pt_ce_integer_range_type, PT_LC("frominterval"), 2, interval);
39854021
}
39864022

39874023
/* $this->getPhpVersion()->supportsNamedArguments()->no() negated — the

‎turbo-ext/src/generated/MutatingScope.h‎

Lines changed: 32 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -528,21 +528,22 @@ inline constexpr char strings[] =
528528
"getIterableValueType\0" /* 9182 */
529529
"PHPStan\\Php\\PhpVersions\0" /* 9203 */
530530
"getPhpVersion\0" /* 9227 */
531-
"isOverallPhpVersionRange\0" /* 9241 */
532-
"PhpParser\\Node\0" /* 9266 */
533-
"invokeNodeCallback\0" /* 9281 */
534-
"collectorType\0" /* 9300 */
535-
"data\0" /* 9314 */
536-
"emitCollectedData\0" /* 9319 */
537-
"extensionClass\0" /* 9337 */
538-
"trackValueDependency\0" /* 9352 */
539-
"file\0" /* 9373 */
540-
"trackFileDependency\0" /* 9378 */
541-
"directory\0" /* 9398 */
542-
"pattern\0" /* 9408 */
543-
"'*'\0" /* 9416 */
544-
"trackDirectoryDependency\0" /* 9420 */
545-
"trackClassDependency"; /* 9445 */
531+
"getConfiguredPhpVersionRange\0" /* 9241 */
532+
"isOverallPhpVersionRange\0" /* 9270 */
533+
"PhpParser\\Node\0" /* 9295 */
534+
"invokeNodeCallback\0" /* 9310 */
535+
"collectorType\0" /* 9329 */
536+
"data\0" /* 9343 */
537+
"emitCollectedData\0" /* 9348 */
538+
"extensionClass\0" /* 9366 */
539+
"trackValueDependency\0" /* 9381 */
540+
"file\0" /* 9402 */
541+
"trackFileDependency\0" /* 9407 */
542+
"directory\0" /* 9427 */
543+
"pattern\0" /* 9437 */
544+
"'*'\0" /* 9445 */
545+
"trackDirectoryDependency\0" /* 9449 */
546+
"trackClassDependency"; /* 9474 */
546547
inline constexpr reg::PackedArg args[] = {
547548
reg::packed(0, 0, 10), /* __construct $container */
548549
reg::packed(48, 0, 61), /* __construct $scopeFactory */
@@ -1070,20 +1071,21 @@ inline constexpr reg::PackedArg args[] = {
10701071
reg::packed(5878, 0, 2162), /* getIterableValueType $iteratee */
10711072
reg::packed(1476, 0, 2162), /* getIterableValueType return */
10721073
reg::packed(1476, 0, 9203), /* getPhpVersion return */
1074+
reg::packed(1476, MAY_BE_NULL, 2162), /* getConfiguredPhpVersionRange return */
10731075
reg::packed(4614, 0, 2162), /* isOverallPhpVersionRange $type */
10741076
reg::packed(1476, MAY_BE_BOOL), /* isOverallPhpVersionRange return */
1075-
reg::packed(2500, 0, 9266), /* invokeNodeCallback $node */
1077+
reg::packed(2500, 0, 9295), /* invokeNodeCallback $node */
10761078
reg::packed(1476, MAY_BE_VOID), /* invokeNodeCallback return */
1077-
reg::packed(9300, MAY_BE_STRING), /* emitCollectedData $collectorType */
1078-
reg::packed(9314, MAY_BE_ANY), /* emitCollectedData $data */
1079+
reg::packed(9329, MAY_BE_STRING), /* emitCollectedData $collectorType */
1080+
reg::packed(9343, MAY_BE_ANY), /* emitCollectedData $data */
10791081
reg::packed(1476, MAY_BE_VOID), /* emitCollectedData return */
1080-
reg::packed(9337, MAY_BE_STRING), /* trackValueDependency $extensionClass */
1082+
reg::packed(9366, MAY_BE_STRING), /* trackValueDependency $extensionClass */
10811083
reg::packed(3280, MAY_BE_STRING), /* trackValueDependency $key */
10821084
reg::packed(1476, MAY_BE_VOID), /* trackValueDependency return */
1083-
reg::packed(9373, MAY_BE_STRING), /* trackFileDependency $file */
1085+
reg::packed(9402, MAY_BE_STRING), /* trackFileDependency $file */
10841086
reg::packed(1476, MAY_BE_VOID), /* trackFileDependency return */
1085-
reg::packed(9398, MAY_BE_STRING), /* trackDirectoryDependency $directory */
1086-
reg::packed(9408, MAY_BE_STRING, reg::NoString, false, false, 9416), /* trackDirectoryDependency $pattern */
1087+
reg::packed(9427, MAY_BE_STRING), /* trackDirectoryDependency $directory */
1088+
reg::packed(9437, MAY_BE_STRING, reg::NoString, false, false, 9445), /* trackDirectoryDependency $pattern */
10871089
reg::packed(1476, MAY_BE_VOID), /* trackDirectoryDependency return */
10881090
reg::packed(3717, MAY_BE_STRING), /* trackClassDependency $className */
10891091
reg::packed(1476, MAY_BE_VOID), /* trackClassDependency return */
@@ -1285,13 +1287,14 @@ inline constexpr sigtab::Sig getNativeConstantTypes = { { 9140 /* getNativeConst
12851287
inline constexpr sigtab::Sig getIterableKeyType = { { 9163 /* getIterableKeyType */, 1, 521, 1, 522, ZEND_ACC_PUBLIC } };
12861288
inline constexpr sigtab::Sig getIterableValueType = { { 9182 /* getIterableValueType */, 1, 523, 1, 524, ZEND_ACC_PUBLIC } };
12871289
inline constexpr sigtab::Sig getPhpVersion = { { 9227 /* getPhpVersion */, 0, 525, 0, 525, ZEND_ACC_PUBLIC } };
1288-
inline constexpr sigtab::Sig isOverallPhpVersionRange = { { 9241 /* isOverallPhpVersionRange */, 1, 526, 1, 527, ZEND_ACC_PRIVATE } };
1289-
inline constexpr sigtab::Sig invokeNodeCallback = { { 9281 /* invokeNodeCallback */, 1, 528, 1, 529, ZEND_ACC_PUBLIC } };
1290-
inline constexpr sigtab::Sig emitCollectedData = { { 9319 /* emitCollectedData */, 2, 530, 2, 532, ZEND_ACC_PUBLIC } };
1291-
inline constexpr sigtab::Sig trackValueDependency = { { 9352 /* trackValueDependency */, 2, 533, 2, 535, ZEND_ACC_PUBLIC } };
1292-
inline constexpr sigtab::Sig trackFileDependency = { { 9378 /* trackFileDependency */, 1, 536, 1, 537, ZEND_ACC_PUBLIC } };
1293-
inline constexpr sigtab::Sig trackDirectoryDependency = { { 9420 /* trackDirectoryDependency */, 1, 538, 2, 540, ZEND_ACC_PUBLIC } };
1294-
inline constexpr sigtab::Sig trackClassDependency = { { 9445 /* trackClassDependency */, 1, 541, 1, 542, ZEND_ACC_PUBLIC } };
1290+
inline constexpr sigtab::Sig getConfiguredPhpVersionRange = { { 9241 /* getConfiguredPhpVersionRange */, 0, 526, 0, 526, ZEND_ACC_PRIVATE } };
1291+
inline constexpr sigtab::Sig isOverallPhpVersionRange = { { 9270 /* isOverallPhpVersionRange */, 1, 527, 1, 528, ZEND_ACC_PRIVATE } };
1292+
inline constexpr sigtab::Sig invokeNodeCallback = { { 9310 /* invokeNodeCallback */, 1, 529, 1, 530, ZEND_ACC_PUBLIC } };
1293+
inline constexpr sigtab::Sig emitCollectedData = { { 9348 /* emitCollectedData */, 2, 531, 2, 533, ZEND_ACC_PUBLIC } };
1294+
inline constexpr sigtab::Sig trackValueDependency = { { 9381 /* trackValueDependency */, 2, 534, 2, 536, ZEND_ACC_PUBLIC } };
1295+
inline constexpr sigtab::Sig trackFileDependency = { { 9407 /* trackFileDependency */, 1, 537, 1, 538, ZEND_ACC_PUBLIC } };
1296+
inline constexpr sigtab::Sig trackDirectoryDependency = { { 9449 /* trackDirectoryDependency */, 1, 539, 2, 541, ZEND_ACC_PUBLIC } };
1297+
inline constexpr sigtab::Sig trackClassDependency = { { 9474 /* trackClassDependency */, 1, 542, 1, 543, ZEND_ACC_PUBLIC } };
12951298
} // namespace sig
12961299

12971300
} // namespace ptdecl::MutatingScope

‎turbo-ext/tests/scope-family.php‎

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -643,6 +643,35 @@ public function exprRef(Node\Expr $expr): mixed
643643

644644
return [$table, $table, []];
645645
};
646+
// PHP_VERSION_ID under a guard that only gives an upper bound (`PHP_VERSION_ID < X`),
647+
// with a single analysed version pinned to phpVersion: 80500 whatever PHP runs
648+
// this: getPhpVersion() raises the lower bound to the analysed version, or as
649+
// close to it as the guard allows - once with the analysed version above the
650+
// guard's bound and once below it
651+
$sfGuardedVersionScopes = [];
652+
foreach ([80399, 80599] as $sfGuardedMax) {
653+
$sfGuardedVersionFetch = new \PhpParser\Node\Expr\ConstFetch(new \PhpParser\Node\Name('PHP_VERSION_ID'));
654+
$sfGuardedVersionArgs = $sfHarness->constructorArgs(
655+
$sfScopeFactory->create(\PHPStan\Analyser\ScopeContext::create($sfFile))
656+
->assignExpression($sfGuardedVersionFetch, \PHPStan\Type\IntegerRangeType::fromInterval(\PHPStan\Analyser\ConstantResolver::PHP_MIN_ANALYZABLE_VERSION_ID, $sfGuardedMax), new \PHPStan\Type\IntegerType()),
657+
);
658+
$sfGuardedVersionArgs['phpVersion'] = new \PHPStan\Php\PhpVersion(80500);
659+
$sfGuardedVersionArgs['configuredPhpVersionRangeHelper'] = new \PHPStan\Php\ConfiguredPhpVersionRangeHelper(80500, $scContainer->getByType(\PHPStan\Php\ComposerPhpVersionFactory::class));
660+
$sfGuardedVersionScope = new \PHPStan\Analyser\MutatingScope(...array_values($sfGuardedVersionArgs));
661+
$sfScopes[spl_object_id($sfGuardedVersionScope)] = [$sfGuardedVersionScope, [$sfGuardedVersionFetch], null];
662+
$sfGuardedVersionScopes[] = [$sfGuardedVersionScope, static function (string $side) use ($sfHarness, $sfGuardedVersionFetch, $sfGuardedMax): array {
663+
$native = $side === 'native';
664+
$range = $native
665+
? \PHPStanTurbo\IntegerRangeType::fromInterval(\PHPStan\Analyser\ConstantResolver::PHP_MIN_ANALYZABLE_VERSION_ID, $sfGuardedMax)
666+
: \PHPStan\Type\IntegerRangeType::fromInterval(\PHPStan\Analyser\ConstantResolver::PHP_MIN_ANALYZABLE_VERSION_ID, $sfGuardedMax);
667+
$holder = $native
668+
? new \PHPStanTurbo\ExpressionTypeHolder($sfGuardedVersionFetch, $range, \PHPStanTurbo\TrinaryLogic::createYes())
669+
: new \PHPStan\Analyser\ExpressionTypeHolder($sfGuardedVersionFetch, $range, \PHPStan\TrinaryLogic::createYes());
670+
$table = [$sfHarness->key($sfGuardedVersionFetch) => $holder];
671+
672+
return [$table, $table, []];
673+
}];
674+
}
646675
foreach ([80500, 70400] as $sfVersionId) {
647676
$sfVersionScope = $sfScopeFactory->create(\PHPStan\Analyser\ScopeContext::create($sfFile)->enterClass($sfReflectionProvider->getClass(\ScopeFamilyFixture\Holder::class)))
648677
->assignExpression(new \PhpParser\Node\Expr\ConstFetch(new \PhpParser\Node\Name('PHP_VERSION_ID')), new \PHPStan\Type\Constant\ConstantIntegerType($sfVersionId), new \PHPStan\Type\IntegerType());
@@ -759,6 +788,9 @@ public function exprRef(Node\Expr $expr): mixed
759788
// the scopes whose tables are built per side (each side's own Type classes)
760789
$sfScopes[spl_object_id($sfSyntheticScope)][3] = $sfSyntheticTables;
761790
$sfScopes[spl_object_id($sfOverallVersionScope)][3] = $sfOverallVersionTables;
791+
foreach ($sfGuardedVersionScopes as [$sfGuardedVersionScope, $sfGuardedVersionTables]) {
792+
$sfScopes[spl_object_id($sfGuardedVersionScope)][3] = $sfGuardedVersionTables;
793+
}
762794
$sfPromotedArgs = $sfHarness->constructorArgs($sfSyntheticScope);
763795
$sfPromotedArgs['nativeTypesPromoted'] = true;
764796
$sfPromotedScope = new \PHPStan\Analyser\MutatingScope(...array_values($sfPromotedArgs));

0 commit comments

Comments
 (0)