Skip to content

Commit 0d717ee

Browse files
ondrejmirtesclaude
andcommitted
Declare ArrayType's and ObjectType's private class constants natively
The twins declare `private const TRUNCATE_ACCESSORIES_LIMIT` (ArrayType) and `private const EXTRA_OFFSET_CLASSES` (ObjectType); the native classes only had a #define and a local copy of the list, so reflection saw fewer constants on them than on the twins. Both are registered now with the twins' visibility and values, and ObjectType's isExtraOffsetAccessibleClass() walks the one list the constant is built from. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_018Fdg8n3eEi4ZoXa4dA2k4A
1 parent 39efdc6 commit 0d717ee

3 files changed

Lines changed: 54 additions & 15 deletions

File tree

‎turbo-ext/src/ArrayType.cpp‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2062,6 +2062,7 @@ void pt_register_array_type()
20622062
ptdecl::ArrayType::declareClass(cls);
20632063
/* the slots PT_AT_PROP_*: the three class-body properties first, the
20642064
* promoted $itemType after them */
2065+
cls.privateClassConstantLong("TRUNCATE_ACCESSORIES_LIMIT", PT_AT_TRUNCATE_ACCESSORIES_LIMIT);
20652066
cls.privateTypedClassProperty("keyType", ptcls::type, false);
20662067
cls.privateTypedClassPropertyDefaultNull("cachedIterableKeyType", ptcls::type);
20672068
cls.privateTypedClassPropertyDefaultNull("isList", ptcls::trinaryLogic);

‎turbo-ext/src/ObjectType.cpp‎

Lines changed: 37 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,40 @@ zend_class_entry *pt_ce_object_type = nullptr;
5454
/* private const DESCRIPTION_CACHE_LIMIT */
5555
#define PT_OT_DESCRIPTION_CACHE_LIMIT 1024
5656

57+
/* private const EXTRA_OFFSET_CLASSES: the class constant is built from this
58+
* list, and isExtraOffsetAccessibleClass() walks it */
59+
static const pt_superglobal_name pt_ot_extra_offset_classes[] = {
60+
{"DOMNamedNodeMap", sizeof("DOMNamedNodeMap") - 1}, // Only read and existence
61+
{"Dom\\NamedNodeMap", sizeof("Dom\\NamedNodeMap") - 1}, // Only read and existence
62+
{"DOMNodeList", sizeof("DOMNodeList") - 1}, // Only read and existence
63+
{"Dom\\NodeList", sizeof("Dom\\NodeList") - 1}, // Only read and existence
64+
{"Dom\\HTMLCollection", sizeof("Dom\\HTMLCollection") - 1}, // Only read and existence
65+
{"Dom\\DtdNamedNodeMap", sizeof("Dom\\DtdNamedNodeMap") - 1}, // Only read and existence
66+
{"PDORow", sizeof("PDORow") - 1}, // Only read and existence
67+
{"ResourceBundle", sizeof("ResourceBundle") - 1}, // Only read
68+
{"FFI\\CData", sizeof("FFI\\CData") - 1}, // Very funky and weird
69+
{"SimpleXMLElement", sizeof("SimpleXMLElement") - 1},
70+
{"Threaded", sizeof("Threaded") - 1},
71+
};
72+
73+
/* the constant's value: a persistent immutable list of interned strings, as
74+
* the engine references a class constant for the process lifetime */
75+
static void pt_ot_extra_offset_classes_constant(zval *out)
76+
{
77+
size_t count = sizeof(pt_ot_extra_offset_classes) / sizeof(pt_ot_extra_offset_classes[0]);
78+
HashTable *list = (HashTable *) pemalloc(sizeof(HashTable), 1);
79+
zend_hash_init(list, (uint32_t) count, NULL, NULL, 1);
80+
for (size_t i = 0; i < count; i++) {
81+
zval value;
82+
ZVAL_INTERNED_STR(&value, zend_string_init_interned(pt_ot_extra_offset_classes[i].name, pt_ot_extra_offset_classes[i].len, 1));
83+
zend_hash_next_index_insert(list, &value);
84+
}
85+
GC_ADD_FLAGS(list, IS_ARRAY_IMMUTABLE);
86+
GC_SET_REFCOUNT(list, 2);
87+
ZVAL_ARR(out, list);
88+
Z_TYPE_INFO_P(out) = IS_ARRAY;
89+
}
90+
5791
/* new IsSuperTypeOfResult($trinary, $reasons) — AcceptsResult.cpp */
5892
bool pt_result_object_create(zval *out, zend_class_entry *ce, zval *trinary, zval *reasons, zval *lazyReasons);
5993

@@ -1846,22 +1880,9 @@ class ObjectType
18461880
if (classReflection.isNull()) return PT_TRI_MAYBE;
18471881
zend_object *reflection = Z_OBJ_P(classReflection.raw());
18481882

1849-
static const char *const extraOffsetClasses[] = {
1850-
"DOMNamedNodeMap", // Only read and existence
1851-
"Dom\\NamedNodeMap", // Only read and existence
1852-
"DOMNodeList", // Only read and existence
1853-
"Dom\\NodeList", // Only read and existence
1854-
"Dom\\HTMLCollection", // Only read and existence
1855-
"Dom\\DtdNamedNodeMap", // Only read and existence
1856-
"PDORow", // Only read and existence
1857-
"ResourceBundle", // Only read
1858-
"FFI\\CData", // Very funky and weird
1859-
"SimpleXMLElement",
1860-
"Threaded",
1861-
};
1862-
for (const char *extraOffsetClass : extraOffsetClasses) {
1883+
for (const pt_superglobal_name &extraOffsetClass : pt_ot_extra_offset_classes) {
18631884
bool is;
1864-
if (UNEXPECTED(!reflectionIs(reflection, extraOffsetClass, strlen(extraOffsetClass), is))) return -1;
1885+
if (UNEXPECTED(!reflectionIs(reflection, extraOffsetClass.name, extraOffsetClass.len, is))) return -1;
18651886
if (is) return PT_TRI_YES;
18661887
}
18671888

@@ -3586,6 +3607,7 @@ void pt_register_object_type()
35863607
ptdecl::ObjectType::declareClass(cls);
35873608
/* the twin's declaration order defines the PT_OT_PROP_* slots */
35883609
cls.privateClassConstantLong("DESCRIPTION_CACHE_LIMIT", PT_OT_DESCRIPTION_CACHE_LIMIT);
3610+
cls.privateClassConstantValue("EXTRA_OFFSET_CLASSES", pt_ot_extra_offset_classes_constant);
35893611
cls.privateTypedClassProperty("subtractedType", "PHPStan\\Type\\Type", true);
35903612
/* the static caches, in the twin's declaration order too */
35913613
cls.privateStaticTypedArrayPropertyDefaultEmpty("superTypes");

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8444,6 +8444,22 @@ public function getTypes(): array
84448444
$constantArrayUnion = static fn () => new \PHPStan\Type\UnionType([new \PHPStan\Type\Constant\ConstantArrayType([$cs('a')], [$ci(1)]), new \PHPStan\Type\Constant\ConstantArrayType([$cs('a')], [$ci(2)])]);
84458445
$r['intersection getFiniteTypes constant arrays'] = $view((new \PHPStan\Type\IntersectionType([$constantArrayUnion(), $constantArrayUnion()]))->getFiniteTypes());
84468446

8447+
// the class constants the Type classes declare, private ones included
8448+
foreach (array_keys($manifest) as $shadowedClass) {
8449+
if (!str_starts_with($shadowedClass, 'PHPStan\\Type\\')) {
8450+
continue;
8451+
}
8452+
$constants = [];
8453+
foreach ((new \ReflectionClass($shadowedClass))->getReflectionConstants() as $constant) {
8454+
$constants[$constant->getName()] = [$constant->isPrivate() ? 'private' : ($constant->isProtected() ? 'protected' : 'public'), $constant->getValue(), $constant->isFinal(), $constant->getDeclaringClass()->getName()];
8455+
}
8456+
$r["class constants $shadowedClass"] = $constants;
8457+
}
8458+
// ObjectType's EXTRA_OFFSET_CLASSES, walked by isOffsetAccessible()
8459+
foreach ([\SimpleXMLElement::class, 'Dom\\NodeList', \DOMNodeList::class, \PDORow::class, \stdClass::class, \ArrayObject::class, \Exception::class] as $offsetClass) {
8460+
$r["extra offset class $offsetClass"] = $misuse(static fn () => (new \PHPStan\Type\ObjectType($offsetClass))->isOffsetAccessible());
8461+
}
8462+
84478463
// countConstantArrayValueTypes() hands each element to TypeTraverser::map(Type $type, ...)
84488464
foreach (['string' => 'x', 'object' => new \stdClass()] as $elementName => $element) {
84498465
$r["combinator countConstantArrayValueTypes $elementName"] = $misuse(static fn () => \PHPStan\Type\TypeCombinator::countConstantArrayValueTypes([new \PHPStan\Type\IntegerType(), $element]));

0 commit comments

Comments
 (0)