Skip to content

Commit 0b49b0b

Browse files
ondrejmirtesclaude
andcommitted
Declare the twins' private constants on the analyser natives
AssignHandler, OutputBufferHelper, ExpressionResult, IssetabilityDescriptor, IssetabilityLinkInfo, MutatingScope, ScopeOps, ForeachHandler and TypeSpecifier did not declare their twins' private constants, so reflection (and a closure bound to the class) saw classes without them. They are declared now; the array-valued ones are persistent lists built once at module startup by pt_persistent_string_list(), which VolatileExpressionHelper's private list builder becomes. ScopeOps' keyMayHideSubExpressions() and MutatingScope's serialization check read the same tables the constants are built from. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_018Fdg8n3eEi4ZoXa4dA2k4A
1 parent 94a54a1 commit 0b49b0b

13 files changed

Lines changed: 124 additions & 45 deletions

‎turbo-ext/src/AssignHandler.cpp‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4711,6 +4711,9 @@ void pt_register_assign_handler()
47114711
reg::Class cls("PHPStan\\Analyser\\ExprHandler\\AssignHandler");
47124712
ptdecl::AssignHandler::declareClass(cls);
47134713
ptdecl::AssignHandler::declareProperties(cls);
4714+
cls.privateClassConstantLong("TERNARY_ARM_EXCLUDED_VALUES_LIMIT", PT_AH_TERNARY_ARM_EXCLUDED_VALUES_LIMIT);
4715+
cls.privateClassConstantLong("DERIVED_CONDITIONAL_EXPRESSIONS_LIMIT", PT_AH_DERIVED_CONDITIONAL_EXPRESSIONS_LIMIT);
4716+
cls.privateClassConstantLong("ARRAY_DIM_FETCH_WRITE_DEPTH_LIMIT", PT_AH_ARRAY_DIM_FETCH_WRITE_DEPTH_LIMIT);
47144717

47154718
/* the real parameter class names: the DI container autowires the
47164719
* service by reflecting the constructor */

‎turbo-ext/src/ExpressionResult.cpp‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1495,6 +1495,7 @@ void pt_register_expression_result()
14951495

14961496
reg::Class cls("PHPStan\\Analyser\\ExpressionResult");
14971497
ptdecl::ExpressionResult::declareClass(cls);
1498+
cls.privateClassConstantString("READ_VARIABLE_NAMES_ATTRIBUTE", "readVariableNames");
14981499
/* the twin's properties in declaration order (the OBJ_PROP_NUM slots) */
14991500
cls.privateNullProperty("typeCallback");
15001501
cls.privateNullProperty("specifyTypesCallback");

‎turbo-ext/src/ForeachHandler.cpp‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1968,6 +1968,8 @@ void pt_register_foreach_handler()
19681968
reg::Class cls("PHPStan\\Analyser\\StmtHandler\\ForeachHandler");
19691969
ptdecl::ForeachHandler::declareClass(cls);
19701970
ptdecl::ForeachHandler::declareProperties(cls);
1971+
cls.privateClassConstantLong("FOREACH_UNROLL_LIMIT", PT_FEH_FOREACH_UNROLL_LIMIT);
1972+
cls.privateClassConstantLong("FOREACH_UNROLL_NESTED_LIMIT", PT_FEH_FOREACH_UNROLL_NESTED_LIMIT);
19711973

19721974
/* the real parameter class names and #[AutowiredParameter] names: the DI
19731975
* container autowires the service by reflecting the constructor */

‎turbo-ext/src/IssetabilityDescriptor.cpp‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -420,6 +420,9 @@ void pt_register_issetability_descriptor()
420420
reg::Class cls("PHPStan\\Analyser\\IssetabilityDescriptor");
421421
ptdecl::IssetabilityDescriptor::declareClass(cls);
422422
ptdecl::IssetabilityDescriptor::declareProperties(cls);
423+
cls.privateClassConstantString("KIND_VARIABLE", "variable");
424+
cls.privateClassConstantString("KIND_OFFSET", "offset");
425+
cls.privateClassConstantString("KIND_PROPERTY", "property");
423426

424427
cls.method(sigs::__construct, [](INTERNAL_FUNCTION_PARAMETERS) {
425428
zend_string *kind;

‎turbo-ext/src/IssetabilityLinkInfo.cpp‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,10 @@ void pt_register_issetability_link_info()
289289
reg::Class cls("PHPStan\\Analyser\\IssetabilityLinkInfo");
290290
ptdecl::IssetabilityLinkInfo::declareClass(cls);
291291
ptdecl::IssetabilityLinkInfo::declareProperties(cls);
292+
cls.privateClassConstantString("KIND_VARIABLE", "variable");
293+
cls.privateClassConstantString("KIND_OFFSET", "offset");
294+
cls.privateClassConstantString("KIND_PROPERTY", "property");
295+
cls.privateClassConstantString("KIND_LEAF", "leaf");
292296

293297
cls.method(sigs::__construct, [](INTERNAL_FUNCTION_PARAMETERS) {
294298
zend_string *kind;

‎turbo-ext/src/MutatingScope.cpp‎

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -341,6 +341,14 @@ enum : uint32_t
341341
/* private const GLOBAL_CONSTANT_FETCH_KEYS_LIMIT */
342342
#define PT_MS_GLOBAL_CONSTANT_FETCH_KEYS_LIMIT 8192
343343

344+
/* the twin's private CUSTOM_SERIALIZATION_METHODS (the serialization check
345+
* walks the same names), a persistent list built once at module startup */
346+
static const pt_superglobal_name pt_ms_custom_serialization_methods[] = {
347+
{ PT_LC("__sleep") },
348+
{ PT_LC("__serialize") },
349+
{ PT_LC("__unserialize") },
350+
};
351+
344352
/* PHPStan\Analyser\ConstantResolver::PHP_MIN_ANALYZABLE_VERSION_ID and
345353
* PHPStan\Php\PhpVersionFactory::MAX_PHP_VERSION — the one place the native
346354
* code reads them from (getPhpVersion()) */
@@ -1313,11 +1321,9 @@ class MutatingScope
13131321
return false;
13141322
}
13151323
zend_object *reflection = Z_OBJ_P(classReflection.raw());
1316-
/* self::CUSTOM_SERIALIZATION_METHODS */
1317-
static const char *const methodNames[] = { "__sleep", "__serialize", "__unserialize" };
1318-
for (const char *methodName : methodNames) {
1324+
for (const pt_superglobal_name &methodName : pt_ms_custom_serialization_methods) {
13191325
bool has;
1320-
if (UNEXPECTED(!hasNativeMethod(reflection, methodName, has))) return false;
1326+
if (UNEXPECTED(!hasNativeMethod(reflection, methodName.name, has))) return false;
13211327
if (has) {
13221328
out = true;
13231329
return true;
@@ -12273,6 +12279,13 @@ zv::Val pt_mutating_scope_after_open_ssl_call(zend_object *scope, zend_string *o
1227312279

1227412280
/* }}} */
1227512281

12282+
static HashTable *pt_ms_custom_serialization_methods_list = nullptr;
12283+
12284+
static void pt_ms_custom_serialization_methods_constant(zval *out)
12285+
{
12286+
pt_persistent_list_into(out, pt_ms_custom_serialization_methods_list);
12287+
}
12288+
1227612289
void pt_register_mutating_scope()
1227712290
{
1227812291
using namespace pt_ms;
@@ -12330,6 +12343,10 @@ void pt_register_mutating_scope()
1233012343
* static takes no OBJ_PROP_NUM slot, and keeping it out of the run above
1233112344
* keeps that run a literal transcript of the PT_MS_PROP_* enum */
1233212345
cls.privateStaticTypedArrayPropertyDefaultEmpty("globalConstantFetchKeys");
12346+
cls.privateClassConstantLong("COMPLEX_UNION_TYPE_MEMBER_LIMIT", PT_MS_COMPLEX_UNION_TYPE_MEMBER_LIMIT);
12347+
cls.privateClassConstantLong("GLOBAL_CONSTANT_FETCH_KEYS_LIMIT", PT_MS_GLOBAL_CONSTANT_FETCH_KEYS_LIMIT);
12348+
pt_ms_custom_serialization_methods_list = pt_persistent_string_list(pt_ms_custom_serialization_methods, sizeof(pt_ms_custom_serialization_methods) / sizeof(pt_ms_custom_serialization_methods[0]));
12349+
cls.privateClassConstantValue("CUSTOM_SERIALIZATION_METHODS", pt_ms_custom_serialization_methods_constant);
1233312350
/* }}} */
1233412351

1233512352
cls.method("__construct", reg::Public, 15, {

‎turbo-ext/src/OutputBufferHelper.cpp‎

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,11 +201,40 @@ zv::Val pt_output_buffer_helper_apply_level_delta(zval *helper, zval *nodeScopeR
201201

202202
#include "reg.h"
203203

204+
/* the twin's private LEVEL_INCREMENTING_FUNCTIONS and
205+
* LEVEL_DECREMENTING_FUNCTIONS (getLevelDelta() compares the same names),
206+
* as persistent lists built once at module startup */
207+
static const pt_superglobal_name pt_obh_level_incrementing_functions[] = {
208+
{ PT_LC("ob_start") },
209+
};
210+
static const pt_superglobal_name pt_obh_level_decrementing_functions[] = {
211+
{ PT_LC("ob_get_clean") },
212+
{ PT_LC("ob_get_flush") },
213+
{ PT_LC("ob_end_clean") },
214+
{ PT_LC("ob_end_flush") },
215+
};
216+
static HashTable *pt_obh_level_incrementing_functions_list = nullptr;
217+
static HashTable *pt_obh_level_decrementing_functions_list = nullptr;
218+
219+
static void pt_obh_level_incrementing_functions_constant(zval *out)
220+
{
221+
pt_persistent_list_into(out, pt_obh_level_incrementing_functions_list);
222+
}
223+
224+
static void pt_obh_level_decrementing_functions_constant(zval *out)
225+
{
226+
pt_persistent_list_into(out, pt_obh_level_decrementing_functions_list);
227+
}
228+
204229
void pt_register_output_buffer_helper()
205230
{
206231
reg::Class cls("PHPStan\\Analyser\\ExprHandler\\Helper\\OutputBufferHelper");
207232
ptdecl::OutputBufferHelper::declareClass(cls);
208233
ptdecl::OutputBufferHelper::declareProperties(cls);
234+
pt_obh_level_incrementing_functions_list = pt_persistent_string_list(pt_obh_level_incrementing_functions, sizeof(pt_obh_level_incrementing_functions) / sizeof(pt_obh_level_incrementing_functions[0]));
235+
pt_obh_level_decrementing_functions_list = pt_persistent_string_list(pt_obh_level_decrementing_functions, sizeof(pt_obh_level_decrementing_functions) / sizeof(pt_obh_level_decrementing_functions[0]));
236+
cls.privateClassConstantValue("LEVEL_INCREMENTING_FUNCTIONS", pt_obh_level_incrementing_functions_constant);
237+
cls.privateClassConstantValue("LEVEL_DECREMENTING_FUNCTIONS", pt_obh_level_decrementing_functions_constant);
209238

210239
/* the real parameter class name: the DI container autowires the
211240
* service by reflecting the constructor */

‎turbo-ext/src/ScopeOps.cpp‎

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,17 @@ namespace sigs = ptdecl::ScopeOps::sig;
2323

2424
static zend_class_entry *pt_ce_scope_ops;
2525

26+
/* the twin's private COMPOSITIONAL_VIRTUAL_KEY_PREFIXES (the list
27+
* keyMayHideSubExpressions() walks), a persistent list built once at module
28+
* startup */
29+
static const pt_superglobal_name pt_so_compositional_virtual_key_prefixes[] = {
30+
{ PT_LC("__phpstanForeachValueByRef(") },
31+
{ PT_LC("__phpstanIntertwinedVariableByReference(") },
32+
{ PT_LC("__phpstanPossiblyImpure(") },
33+
{ PT_LC("__phpstanPropertyInitialization(") },
34+
{ PT_LC("__phpstanRemembered(") },
35+
};
36+
2637
/* {{{ scopeWith's cached property layout (owned by the request lifecycle) */
2738

2839
typedef struct {
@@ -1420,26 +1431,14 @@ class ScopeOps
14201431
*/
14211432
static bool keyMayHideSubExpressions(zend_string *key)
14221433
{
1423-
/* Mirror of ScopeOps::COMPOSITIONAL_VIRTUAL_KEY_PREFIXES - a prefix may
1424-
* be listed only when the printer emits every getSubNodeNames() sub-node
1425-
* verbatim (or the node walks no sub-nodes at all); the foreach/parameter
1426-
* original-value markers hide a synthesized Variable child on purpose. */
1427-
static const struct { const char *prefix; size_t len; } compositionalPrefixes[] = {
1428-
{ "__phpstanForeachValueByRef(", sizeof("__phpstanForeachValueByRef(") - 1 },
1429-
{ "__phpstanIntertwinedVariableByReference(", sizeof("__phpstanIntertwinedVariableByReference(") - 1 },
1430-
{ "__phpstanPossiblyImpure(", sizeof("__phpstanPossiblyImpure(") - 1 },
1431-
{ "__phpstanPropertyInitialization(", sizeof("__phpstanPropertyInitialization(") - 1 },
1432-
{ "__phpstanRemembered(", sizeof("__phpstanRemembered(") - 1 },
1433-
};
1434-
14351434
const char *pos = ZSTR_VAL(key);
14361435
const char *end = pos + ZSTR_LEN(key);
14371436
for (;;) {
14381437
const char *found = zend_memnstr(pos, "__phpstan", sizeof("__phpstan") - 1, end);
14391438
if (found == NULL) return false;
14401439
bool isCompositional = false;
1441-
for (const auto &candidate : compositionalPrefixes) {
1442-
if ((size_t) (end - found) >= candidate.len && memcmp(found, candidate.prefix, candidate.len) == 0) {
1440+
for (const pt_superglobal_name &candidate : pt_so_compositional_virtual_key_prefixes) {
1441+
if ((size_t) (end - found) >= candidate.len && memcmp(found, candidate.name, candidate.len) == 0) {
14431442
pos = found + candidate.len;
14441443
isCompositional = true;
14451444
break;
@@ -1813,11 +1812,21 @@ void pt_scope_ops_rshutdown()
18131812

18141813
/* }}} */
18151814

1815+
static HashTable *pt_so_compositional_virtual_key_prefixes_list = nullptr;
1816+
1817+
static void pt_so_compositional_virtual_key_prefixes_constant(zval *out)
1818+
{
1819+
pt_persistent_list_into(out, pt_so_compositional_virtual_key_prefixes_list);
1820+
}
1821+
18161822
void pt_register_scope_ops()
18171823
{
18181824
reg::Class cls("PHPStan\\Analyser\\ScopeOps");
18191825
ptdecl::ScopeOps::declareClass(cls);
18201826
ptdecl::ScopeOps::declareProperties(cls);
1827+
cls.privateClassConstantString("CONTAINS_SUPER_GLOBAL_ATTRIBUTE_NAME", "containsSuperGlobal");
1828+
pt_so_compositional_virtual_key_prefixes_list = pt_persistent_string_list(pt_so_compositional_virtual_key_prefixes, sizeof(pt_so_compositional_virtual_key_prefixes) / sizeof(pt_so_compositional_virtual_key_prefixes[0]));
1829+
cls.privateClassConstantValue("COMPOSITIONAL_VIRTUAL_KEY_PREFIXES", pt_so_compositional_virtual_key_prefixes_constant);
18211830

18221831
cls.method(sigs::mergeVariableHolders, [](INTERNAL_FUNCTION_PARAMETERS) {
18231832
HashTable *ours, *theirs;

‎turbo-ext/src/TypeSpecifier.cpp‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1028,6 +1028,7 @@ void pt_register_type_specifier()
10281028
reg::Class cls("PHPStan\\Analyser\\TypeSpecifier");
10291029
ptdecl::TypeSpecifier::declareClass(cls);
10301030
ptdecl::TypeSpecifier::declareProperties(cls);
1031+
cls.privateClassConstantString("CONTAINS_CALL_ATTRIBUTE_NAME", "containsCall");
10311032

10321033
/* the DI service's constructor: the generated arginfo names the twin's
10331034
* parameter classes exactly (README rule 6) */

‎turbo-ext/src/VolatileExpressionHelper.cpp‎

Lines changed: 9 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -38,32 +38,19 @@ const pt_superglobal_name pt_veh_existence_check_function_names[] = {
3838
{ PT_LC("function_exists") },
3939
};
4040

41-
/* the same lists as the twin's constants: persistent immutable arrays the
42-
* engine references for the process lifetime */
43-
HashTable *pt_veh_persistent_list(const pt_superglobal_name *names, size_t count)
44-
{
45-
HashTable *list = (HashTable *) pemalloc(sizeof(HashTable), 1);
46-
zend_hash_init(list, (uint32_t) count, NULL, NULL, 1);
47-
for (size_t i = 0; i < count; i++) {
48-
zval value;
49-
ZVAL_INTERNED_STR(&value, zend_string_init_interned(names[i].name, names[i].len, 1));
50-
zend_hash_next_index_insert(list, &value);
51-
}
52-
GC_ADD_FLAGS(list, IS_ARRAY_IMMUTABLE);
53-
GC_SET_REFCOUNT(list, 2);
54-
return list;
55-
}
41+
/* the same lists as the twin's constants (pt_persistent_string_list()),
42+
* built once at module startup */
43+
HashTable *pt_veh_volatile_function_names_list = nullptr;
44+
HashTable *pt_veh_existence_check_function_names_list = nullptr;
5645

5746
void pt_veh_volatile_function_names_constant(zval *out)
5847
{
59-
ZVAL_ARR(out, pt_veh_persistent_list(pt_veh_volatile_function_names, sizeof(pt_veh_volatile_function_names) / sizeof(pt_veh_volatile_function_names[0])));
60-
Z_TYPE_INFO_P(out) = IS_ARRAY;
48+
pt_persistent_list_into(out, pt_veh_volatile_function_names_list);
6149
}
6250

6351
void pt_veh_existence_check_function_names_constant(zval *out)
6452
{
65-
ZVAL_ARR(out, pt_veh_persistent_list(pt_veh_existence_check_function_names, sizeof(pt_veh_existence_check_function_names) / sizeof(pt_veh_existence_check_function_names[0])));
66-
Z_TYPE_INFO_P(out) = IS_ARRAY;
53+
pt_persistent_list_into(out, pt_veh_existence_check_function_names_list);
6754
}
6855

6956
/* unset($table[$key]) on the caller's array: separates a shared table
@@ -354,6 +341,9 @@ zv::Val pt_volatile_expression_helper_invalidate_negative_existence_checks(zval
354341

355342
void pt_register_volatile_expression_helper()
356343
{
344+
pt_veh_volatile_function_names_list = pt_persistent_string_list(pt_veh_volatile_function_names, sizeof(pt_veh_volatile_function_names) / sizeof(pt_veh_volatile_function_names[0]));
345+
pt_veh_existence_check_function_names_list = pt_persistent_string_list(pt_veh_existence_check_function_names, sizeof(pt_veh_existence_check_function_names) / sizeof(pt_veh_existence_check_function_names[0]));
346+
357347
reg::Class cls("PHPStan\\Analyser\\VolatileExpressionHelper");
358348
ptdecl::VolatileExpressionHelper::declareClass(cls);
359349
ptdecl::VolatileExpressionHelper::declareProperties(cls);

0 commit comments

Comments
 (0)