Skip to content

Commit 8799bee

Browse files
ondrejmirtesclaude
andcommitted
Register the private methods the mirroring natives were missing
Natives that declare their twins' private methods declared all but a few: InitializerExprTypeResolver's getIntegerBounds(), getMaxModuloMagnitude(), shiftLeftOverflows() and toIntBound(), InitializerExprContext's parseNamespace(), IntegerRangeType's isSubTypeOfUnionWithReason() and the private static create() of the TypeSpecifierContext, PassedByReference and TemplateTypeVariance flyweights. Each is registered now over the C++ member that already implements it, and the differential tests call them in the class scope on both sides. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_018Fdg8n3eEi4ZoXa4dA2k4A
1 parent 28eabbe commit 8799bee

10 files changed

Lines changed: 132 additions & 6 deletions

‎turbo-ext/src/InitializerExprContext.cpp‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -828,6 +828,14 @@ void pt_register_initializer_expr_context()
828828
PT_IEC_THIS.construct(&args[0], &args[1], &args[2], &args[3], &args[4], &args[5], &args[6]);
829829
});
830830

831+
cls.method(sigs::parseNamespace, [](INTERNAL_FUNCTION_PARAMETERS) {
832+
zend_string *name;
833+
if (!zp::parse<zp::Str>(execute_data, name)) RETURN_THROWS();
834+
zv::Val namespace_;
835+
if (UNEXPECTED(!InitializerExprContext::parseNamespace(name, namespace_))) RETURN_THROWS();
836+
namespace_.intoReturnValue(return_value);
837+
});
838+
831839
cls.method(sigs::fromScope, [](INTERNAL_FUNCTION_PARAMETERS) {
832840
zval *scope;
833841
ZEND_PARSE_PARAMETERS_START(1, 1)

‎turbo-ext/src/InitializerExprTypeResolver.cpp‎

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1836,6 +1836,30 @@ class InitializerExprTypeResolver
18361836
return true;
18371837
}
18381838

1839+
/* getIntegerBounds() as the private method returns it: the [min, max]
1840+
* pair or null; UNDEF = pending exception */
1841+
zv::Val getIntegerBoundsValue(zval *type) const
1842+
{
1843+
bool has = false;
1844+
NullableLong min = NullableLong::null();
1845+
NullableLong max = NullableLong::null();
1846+
if (UNEXPECTED(!getIntegerBounds(type, has, min, max))) return zv::Val();
1847+
if (!has) return zv::Val::null();
1848+
zv::Arr pair = zv::Arr::create(2);
1849+
pair.push(min.toVal());
1850+
pair.push(max.toVal());
1851+
return zv::Val(std::move(pair));
1852+
}
1853+
1854+
/* shiftLeftOverflows() for the private method's glue; false = pending
1855+
* exception */
1856+
[[nodiscard]] static bool shiftLeftOverflowsValue(zend_long value, zend_long shift, bool &out)
1857+
{
1858+
zval shiftZv;
1859+
ZVAL_LONG(&shiftZv, shift);
1860+
return shiftLeftOverflows(value, &shiftZv, out);
1861+
}
1862+
18391863
/* Mirrors getPlusType(). */
18401864
zv::Val getPlusType(zval *left, zval *right, const pt_ietr_get_type &getTypeCallback) const
18411865
{
@@ -5727,6 +5751,40 @@ void pt_register_initializer_expr_type_resolver()
57275751
PT_RETURN_VAL(InitializerExprTypeResolver::getNonNegativeIntegerBoundsValue(type));
57285752
});
57295753

5754+
cls.method(sigs::getMaxModuloMagnitude, [](INTERNAL_FUNCTION_PARAMETERS) {
5755+
zend_long divisorMin, divisorMax;
5756+
bool divisorMinIsNull, divisorMaxIsNull;
5757+
ZEND_PARSE_PARAMETERS_START(2, 2)
5758+
Z_PARAM_LONG_OR_NULL(divisorMin, divisorMinIsNull)
5759+
Z_PARAM_LONG_OR_NULL(divisorMax, divisorMaxIsNull)
5760+
ZEND_PARSE_PARAMETERS_END();
5761+
NullableLong magnitude = InitializerExprTypeResolver::getMaxModuloMagnitude(
5762+
divisorMinIsNull ? NullableLong::null() : NullableLong::of(divisorMin),
5763+
divisorMaxIsNull ? NullableLong::null() : NullableLong::of(divisorMax));
5764+
PT_RETURN_VAL(magnitude.toVal());
5765+
});
5766+
5767+
cls.method(sigs::getIntegerBounds, [](INTERNAL_FUNCTION_PARAMETERS) {
5768+
zval *type;
5769+
if (!zp::parse<zp::Obj>(execute_data, type)) RETURN_THROWS();
5770+
if (UNEXPECTED(!ietrArgType(type, 1))) RETURN_THROWS();
5771+
PT_RETURN_VAL(IETR_THIS.getIntegerBoundsValue(type));
5772+
});
5773+
5774+
cls.method(sigs::shiftLeftOverflows, [](INTERNAL_FUNCTION_PARAMETERS) {
5775+
zend_long value, shift;
5776+
if (!zp::parse<zp::Long, zp::Long>(execute_data, value, shift)) RETURN_THROWS();
5777+
bool out = false;
5778+
if (UNEXPECTED(!InitializerExprTypeResolver::shiftLeftOverflowsValue(value, shift, out))) RETURN_THROWS();
5779+
RETURN_BOOL(out);
5780+
});
5781+
5782+
cls.method(sigs::toIntBound, [](INTERNAL_FUNCTION_PARAMETERS) {
5783+
double value;
5784+
if (!zp::parse<zp::Double>(execute_data, value)) RETURN_THROWS();
5785+
InitializerExprTypeResolver::toIntBound(value, return_value);
5786+
});
5787+
57305788
cls.method(sigs::computeBitwiseAndRange, [](INTERNAL_FUNCTION_PARAMETERS) {
57315789
zval *leftNumberType, *rightNumberType;
57325790
if (!zp::parse<zp::Obj, zp::Obj>(execute_data, leftNumberType, rightNumberType)) RETURN_THROWS();

‎turbo-ext/src/IntegerRangeType.cpp‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1425,6 +1425,7 @@ void pt_register_integer_range_type()
14251425
cls.op<PT_OP_IS_SUPER_TYPE_OF, &IntegerRangeType::isSuperTypeOf>();
14261426

14271427
cls.method<&IntegerRangeType::isSubTypeOf, zp::Obj>(sigs::isSubTypeOf);
1428+
cls.method<&IntegerRangeType::isSubTypeOfUnionWithReason, zp::Obj>(sigs::isSubTypeOfUnionWithReason);
14281429
cls.op<PT_OP_IS_SUB_TYPE_OF, &IntegerRangeType::isSubTypeOf>();
14291430

14301431
cls.method(sigs::isAcceptedBy, [](INTERNAL_FUNCTION_PARAMETERS) {

‎turbo-ext/src/PassedByReference.cpp‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,12 @@ void pt_register_passed_by_reference()
237237
PT_PBR_THIS.construct(value);
238238
});
239239

240+
cls.method(sigs::create, [](INTERNAL_FUNCTION_PARAMETERS) {
241+
zend_long value;
242+
if (!zp::parse<zp::Long>(execute_data, value)) RETURN_THROWS();
243+
pt_pbr_return_object(return_value, PassedByReference::create(value));
244+
});
245+
240246
cls.method(sigs::createNo, [](INTERNAL_FUNCTION_PARAMETERS) {
241247
ZEND_PARSE_PARAMETERS_NONE();
242248
pt_pbr_return_object(return_value, PassedByReference::create(PassedByReference::NO));

‎turbo-ext/src/TemplateTypeVariance.cpp‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -541,6 +541,12 @@ void pt_register_template_type_variance()
541541
PT_TTV_THIS.construct(value);
542542
});
543543

544+
cls.method(sigs::create, [](INTERNAL_FUNCTION_PARAMETERS) {
545+
zend_long value;
546+
if (!zp::parse<zp::Long>(execute_data, value)) RETURN_THROWS();
547+
pt_ttv_return_singleton(return_value, TemplateTypeVariance::create(value));
548+
});
549+
544550
cls.method(sigs::createInvariant, [](INTERNAL_FUNCTION_PARAMETERS) {
545551
ZEND_PARSE_PARAMETERS_NONE();
546552
pt_ttv_return_singleton(return_value, TemplateTypeVariance::createInvariant());

‎turbo-ext/src/TypeSpecifierContext.cpp‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,15 @@ void pt_register_type_specifier_context()
245245
PT_TSC_THIS.construct(isNull ? PT_TSC_NULL : value);
246246
});
247247

248+
cls.method(sigs::create, [](INTERNAL_FUNCTION_PARAMETERS) {
249+
zend_long value;
250+
bool isNull;
251+
ZEND_PARSE_PARAMETERS_START(1, 1)
252+
Z_PARAM_LONG_OR_NULL(value, isNull)
253+
ZEND_PARSE_PARAMETERS_END();
254+
pt_tsc_return_object(return_value, TypeSpecifierContext::create(isNull ? PT_TSC_NULL : value));
255+
});
256+
248257
cls.method(sigs::createTrue, [](INTERNAL_FUNCTION_PARAMETERS) {
249258
ZEND_PARSE_PARAMETERS_NONE();
250259
pt_tsc_return_object(return_value, TypeSpecifierContext::createTrue());

‎turbo-ext/tests/analyser-values.php‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1063,6 +1063,7 @@ public function toString(): string
10631063
$r['scope ' . $i] = $avCatch(static fn () => $view($C::fromScope($scope)));
10641064
}
10651065
foreach (['Foo', 'A\B\C', '\Foo', 'Foo\\', '', '\\', 'A\\\\B'] as $name) {
1066+
$r['parseNamespace ' . $name] = $avCatch(static fn () => \Closure::bind(static fn () => $C::parseNamespace($name), null, $C)());
10661067
$r['fromClass ' . $name] = [
10671068
$avCatch(static fn () => $view($C::fromClass($name, null))),
10681069
$avCatch(static fn () => $view($C::fromClass($name, '/tmp/file.php'))),

‎turbo-ext/tests/signature-parity.php‎

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@
3636
$knownDrift = [
3737
'PhpParser\\NodeTraverser' => 'pending',
3838
'PHPStan\\Analyser\\ExpressionResultStorage' => 'pending',
39-
'PHPStan\\Analyser\\TypeSpecifierContext' => 'pending',
4039
'PHPStan\\Analyser\\VariableLivenessResolver' => 'pending',
4140
'PHPStan\\Cache\\ArenaCache' => 'pending',
4241
'PHPStan\\Internal\\CombinationsHelper' => 'pending',
@@ -66,13 +65,8 @@
6665
'PHPStan\\Parser\\TypeTraverserInstanceofVisitor' => 'pending',
6766
'PHPStan\\Reflection\\BetterReflection\\SourceLocator\\PhpFileCleaner' => 'pending',
6867
'PHPStan\\Reflection\\BetterReflection\\SourceLocator\\SymbolFinderInFiles' => 'pending',
69-
'PHPStan\\Reflection\\InitializerExprContext' => 'pending',
70-
'PHPStan\\Reflection\\InitializerExprTypeResolver' => 'pending',
71-
'PHPStan\\Reflection\\PassedByReference' => 'pending',
7268
'PHPStan\\Reflection\\Php\\PhpClassReflectionExtension' => 'pending',
7369
'PHPStan\\Type\\Constant\\ConstantArrayTypeBuilder' => 'pending',
74-
'PHPStan\\Type\\Generic\\TemplateTypeVariance' => 'pending',
75-
'PHPStan\\Type\\IntegerRangeType' => 'pending',
7670
'PHPStan\\Type\\ObjectType' => 'pending',
7771
'PHPStan\\Type\\TypeCombinatorCache' => 'pending',
7872
];

‎turbo-ext/tests/smoke.php‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1568,6 +1568,23 @@ public function traverse(\PHPStan\Type\Type $type, callable $traverse): \PHPStan
15681568
check($ttvPhp[$ttvLeft]->validPosition($ttvPhp[$ttvRight]) === $ttvNative[$ttvLeft]->validPosition($ttvNative[$ttvRight]), "TemplateTypeVariance $ttvLeft validPosition $ttvRight");
15691569
}
15701570
}
1571+
1572+
// the flyweights' private create(): the singleton the public factory hands
1573+
// out, on both sides
1574+
$flyweightCreate = static fn (string $class, ?int $value): object => \Closure::bind(static fn () => $class::create($value), null, $class)();
1575+
$flyweights = [
1576+
'TemplateTypeVariance' => [\PHPStan\Type\Generic\TemplateTypeVariance::class, \PHPStanTurbo\TemplateTypeVariance::class, [1 => 'createInvariant', 2 => 'createCovariant', 3 => 'createContravariant', 4 => 'createStatic', 5 => 'createBivariant']],
1577+
'PassedByReference' => [\PHPStan\Reflection\PassedByReference::class, \PHPStanTurbo\PassedByReference::class, [1 => 'createNo', 2 => 'createReadsArgument', 3 => 'createCreatesNewVariable']],
1578+
'TypeSpecifierContext' => [\PHPStan\Analyser\TypeSpecifierContext::class, \PHPStanTurbo\TypeSpecifierContext::class, [0b0001 => 'createTrue', 0b0011 => 'createTruthy', 0b0100 => 'createFalse', 0b1100 => 'createFalsey', -1 => 'createNull']],
1579+
'TrinaryLogic' => [\PHPStan\TrinaryLogic::class, \PHPStanTurbo\TrinaryLogic::class, [3 => 'createYes', 1 => 'createMaybe', 0 => 'createNo']],
1580+
];
1581+
foreach ($flyweights as $flyweightName => [$flyweightPhp, $flyweightNative, $flyweightFactories]) {
1582+
foreach ($flyweightFactories as $flyweightValue => $flyweightFactory) {
1583+
$flyweightValue = $flyweightValue === -1 ? null : $flyweightValue;
1584+
check($flyweightCreate($flyweightPhp, $flyweightValue) === $flyweightPhp::$flyweightFactory(), "$flyweightName::create() is $flyweightFactory()'s singleton in PHP");
1585+
check($flyweightCreate($flyweightNative, $flyweightValue) === $flyweightNative::$flyweightFactory(), "$flyweightName::create() is $flyweightFactory()'s singleton natively");
1586+
}
1587+
}
15711588
$ttvErrors = static function (string $class): array {
15721589
$errors = [];
15731590
try {

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

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,17 @@
251251
foreach ($subjects as $name => $subject) {
252252
$r["$name class"] = $view($subject);
253253
$r["$name instanceof"] = [$subject instanceof \PHPStan\Type\Type, $subject instanceof $intClass, $subject instanceof \PHPStan\Type\ConstantScalarType, $subject instanceof \PHPStan\Type\CompoundType];
254+
if ($subject instanceof $rangeClass) {
255+
// the private helper isSubTypeOf() hands a union to
256+
$unions = [
257+
'consts0-10' => new \PHPStan\Type\UnionType(array_map(static fn (int $i) => new $constIntClass($i), range(0, 10))),
258+
'consts3-4|string' => new \PHPStan\Type\UnionType([new $constIntClass(3), new $constIntClass(4), new \PHPStan\Type\StringType()]),
259+
'range0-5|string' => new \PHPStan\Type\UnionType([$rangeClass::fromInterval(0, 5), new \PHPStan\Type\StringType()]),
260+
];
261+
foreach ($unions as $unionName => $union) {
262+
$r["$name isSubTypeOfUnionWithReason $unionName"] = $view((fn () => $this->isSubTypeOfUnionWithReason($union))->call($subject));
263+
}
264+
}
254265
foreach (['typeOnly' => \PHPStan\Type\VerbosityLevel::typeOnly(), 'value' => \PHPStan\Type\VerbosityLevel::value(), 'precise' => \PHPStan\Type\VerbosityLevel::precise(), 'cache' => \PHPStan\Type\VerbosityLevel::cache()] as $levelName => $level) {
255266
$r["$name describe $levelName"] = $subject->describe($level);
256267
}
@@ -7717,6 +7728,21 @@ public function getResolvedPhpDoc(): ?\PHPStan\PhpDoc\ResolvedPhpDocBlock { retu
77177728
foreach ([0, 1, 5, 200, 1000, PHP_INT_MAX] as $value) {
77187729
$r["allBitsMask $value"] = $catching(static fn () => $staticPrivate('allBitsMask', $value));
77197730
}
7731+
foreach ($operands as $name => $operand) {
7732+
$r["getIntegerBounds $name"] = $catching(static fn () => $private('getIntegerBounds', $operand));
7733+
}
7734+
foreach ($integerOperands as $name => $operand) {
7735+
$r["getIntegerBounds $name"] = $catching(static fn () => $private('getIntegerBounds', $operand));
7736+
}
7737+
foreach ([[null, 5], [-7, 3], [PHP_INT_MIN, 1], [PHP_INT_MIN + 1, 1], [0, 0], [3, null], [-2, -9]] as [$divisorMin, $divisorMax]) {
7738+
$r['getMaxModuloMagnitude ' . var_export($divisorMin, true) . ' ' . var_export($divisorMax, true)] = $catching(static fn () => $staticPrivate('getMaxModuloMagnitude', $divisorMin, $divisorMax));
7739+
}
7740+
foreach ([[1, 1], [1, 62], [1, 63], [PHP_INT_MAX, 1], [-1, 63], [-5, 2], [5, -1], [5, 64]] as [$value, $shift]) {
7741+
$r["shiftLeftOverflows $value $shift"] = $catching(static fn () => $staticPrivate('shiftLeftOverflows', $value, $shift));
7742+
}
7743+
foreach ([1.5, -1.5, INF, -INF, NAN, 1e30, -1e30, (float) PHP_INT_MAX, (float) PHP_INT_MIN, 0.0] as $i => $value) {
7744+
$r["toIntBound $i"] = $catching(static fn () => $staticPrivate('toIntBound', $value));
7745+
}
77207746
foreach ([null, true, 1, 1.5, 'x'] as $i => $value) {
77217747
$r["getTypeFromValue $i"] = $catching(static fn () => $private('getTypeFromValue', $value));
77227748
}

0 commit comments

Comments
 (0)