Skip to content

Commit 8d530b5

Browse files
ondrejmirtesclaude
andcommitted
Declare PhpFileCleaner's and SymbolFinderInFiles' properties and signatures
PhpFileCleaner declared none of its twin's five private properties — the generator could not express the `string $contents = ''` default, so no declareProperties() was generated — and SymbolFinderInFiles neither declared nor kept the promoted $cleaner. Both registered their one working method by hand, without its return type. reg::Class learns typed properties with a string default (typedStringProperty()), which the generator now emits, so both classes declare their properties by the generated declarations; the SymbolFinderInFiles constructor stores $cleaner, and clean() and findSymbols() register by their sig:: declarations. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_018Fdg8n3eEi4ZoXa4dA2k4A
1 parent 26413ac commit 8d530b5

6 files changed

Lines changed: 57 additions & 13 deletions

File tree

‎build/PHPStan/Build/TurboDeclarationGenerator.php‎

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,10 +56,10 @@
5656
* Shared by turbo-ext/bin/generate-declarations.php and the drift check in
5757
* turbo-ext/bin/side-by-side.php.
5858
*
59-
* A property whose declaration reg::Class cannot express (a string or
60-
* non-empty array default, an intersection type, a defaulted union of
61-
* classes) leaves its class without declareProperties(); the class keeps
62-
* declaring its properties by hand.
59+
* A property whose declaration reg::Class cannot express (a non-empty
60+
* array default, a string default of a nullable or union type, an
61+
* intersection type, a defaulted union of classes) leaves its class without
62+
* declareProperties(); the class keeps declaring its properties by hand.
6363
*/
6464
final class TurboDeclarationGenerator
6565
{
@@ -722,6 +722,9 @@ private function renderProperty(ReflectionProperty $property): string
722722
if ($default === false) {
723723
return sprintf('cls.property(%s, %s, reg::PropertyKind::TypedFalse, %s%s);', $name, $flags, $mask, $classArg);
724724
}
725+
if (is_string($default) && $classes === [] && $masks === ['MAY_BE_STRING']) {
726+
return sprintf('cls.typedStringProperty(%s, %s, %s);', $name, $flags, $this->cString($default));
727+
}
725728

726729
throw new RuntimeException('a typed default reg::Class cannot declare');
727730
}

‎turbo-ext/src/PhpFileCleaner.cpp‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,16 @@ void pt_register_php_file_cleaner()
2727
{
2828
reg::Class cls("PHPStan\\Reflection\\BetterReflection\\SourceLocator\\PhpFileCleaner");
2929
ptdecl::PhpFileCleaner::declareClass(cls);
30+
/* the twin's scanner state; the native clean() keeps its own in the C++
31+
* scanner, so the properties stay at their defaults ($rejectChars,
32+
* which the twin's constructor computes, uninitialized) */
33+
ptdecl::PhpFileCleaner::declareProperties(cls);
3034

3135
cls.method(sigs::__construct, [](INTERNAL_FUNCTION_PARAMETERS) {
3236
ZEND_PARSE_PARAMETERS_NONE();
3337
});
3438

35-
cls.method("clean", reg::Public, 2, { reg::stringArg("contents"), reg::longArg("maxMatches") }, [](INTERNAL_FUNCTION_PARAMETERS) {
39+
cls.method(sigs::clean, [](INTERNAL_FUNCTION_PARAMETERS) {
3640
zend_string *contents;
3741
zend_long maxMatches;
3842
if (!zp::parse<zp::Str, zp::Long>(execute_data, contents, maxMatches)) RETURN_THROWS();

‎turbo-ext/src/SymbolFinderInFiles.cpp‎

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include "support.h"
2222
#include "generated/SymbolFinderInFiles.h"
2323

24+
namespace slots = ptdecl::SymbolFinderInFiles::slot;
2425
namespace sigs = ptdecl::SymbolFinderInFiles::sig;
2526
#include "zv.h"
2627
#include "SymbolScan.h"
@@ -171,22 +172,28 @@ zv::Val SymbolFinderInFiles::findSymbols(HashTable *files, bool supportsEnums)
171172

172173
#include "reg.h"
173174

174-
#define CLEANER_CLASS "PHPStan\\Reflection\\BetterReflection\\SourceLocator\\PhpFileCleaner"
175-
176175
void pt_register_symbol_finder_in_files()
177176
{
178177
reg::Class cls("PHPStan\\Reflection\\BetterReflection\\SourceLocator\\SymbolFinderInFiles");
179178
ptdecl::SymbolFinderInFiles::declareClass(cls);
179+
ptdecl::SymbolFinderInFiles::declareProperties(cls);
180180

181181
/* the arginfo has to keep the real parameter class name: Nette reflects
182-
* this constructor while compiling the container (rule 6) */
182+
* this constructor while compiling the container (rule 6). The promoted
183+
* $cleaner is kept like the twin keeps it; findSymbols() cleans with
184+
* the native scanner (PhpFileCleaner is final and shadowed too) */
183185
cls.method(sigs::__construct, [](INTERNAL_FUNCTION_PARAMETERS) {
184186
zval *cleaner;
185187
if (!zp::parse<zp::Obj>(execute_data, cleaner)) RETURN_THROWS();
186-
(void) cleaner;
188+
zval *slot = OBJ_PROP_NUM(Z_OBJ_P(ZEND_THIS), slots::cleaner);
189+
zval previous;
190+
ZVAL_COPY_VALUE(&previous, slot);
191+
ZVAL_COPY(slot, cleaner);
192+
Z_PROP_FLAG_P(slot) = 0;
193+
zval_ptr_dtor(&previous);
187194
});
188195

189-
cls.method("findSymbols", reg::Public, 2, { reg::arrayArg("files"), reg::boolArg("supportsEnums") }, [](INTERNAL_FUNCTION_PARAMETERS) {
196+
cls.method(sigs::findSymbols, [](INTERNAL_FUNCTION_PARAMETERS) {
190197
HashTable *files;
191198
bool supportsEnums;
192199
if (!zp::parse<zp::Ht, zp::Bool>(execute_data, files, supportsEnums)) RETURN_THROWS();

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

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,15 @@ inline void declareClass(reg::Class &cls)
2222
cls.final();
2323
}
2424

25-
/* no declareProperties(): $contents: a typed default reg::Class cannot declare */
25+
/* the properties the class declares itself, in declaration order (a used trait's come from its registrar) */
26+
inline void declareProperties(reg::Class &cls)
27+
{
28+
cls.property("typeConfig", ZEND_ACC_PRIVATE, reg::PropertyKind::TypedEmptyArray, MAY_BE_ARRAY);
29+
cls.property("rejectChars", ZEND_ACC_PRIVATE, reg::PropertyKind::Typed, MAY_BE_STRING);
30+
cls.typedStringProperty("contents", ZEND_ACC_PRIVATE, "");
31+
cls.property("len", ZEND_ACC_PRIVATE, reg::PropertyKind::TypedLong, 0);
32+
cls.property("index", ZEND_ACC_PRIVATE, reg::PropertyKind::TypedLong, 0);
33+
}
2634

2735
/* the string and parameter tables the signatures below index into (see reg::Sig) */
2836
namespace sigtab {

‎turbo-ext/src/reg.h‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -427,6 +427,7 @@ enum class PropertyKind
427427
TypedBool, /* a `private bool $x = false` typed property with a bool default; defaultValue carries the default (0/1), the type is bool */
428428
TypedLong, /* a `private int $x = 0` typed property with an int default; defaultValue carries the default, the type is int */
429429
TypedFalse, /* a typed property defaulting to false (`private string|false|null $x = false`, `private Foo|false|null $x = false`); defaultValue carries the MAY_BE_* mask (the scalar members next to a class name), className as for Typed */
430+
TypedString, /* a `private string $x = '...'` typed property with a string default; stringValue carries the default, the type is string */
430431
};
431432

432433
struct Property
@@ -436,6 +437,7 @@ struct Property
436437
uint32_t visibility;
437438
zend_long defaultValue;
438439
const char *className = nullptr; /* persistent literal: the class of a class-typed property (Typed* kinds), combined with a MAY_BE_NULL bit in defaultValue for `?Foo` */
440+
const char *stringValue = nullptr; /* persistent literal: the default of a TypedString property */
439441
};
440442

441443
struct Constant
@@ -570,6 +572,19 @@ inline void declareMembers(zend_class_entry *ce, const std::vector<Property> &pr
570572
zend_string_release(nameStr);
571573
break;
572574
}
575+
case PropertyKind::TypedString: {
576+
/* the default interned like a compiled literal: a user class's
577+
* defaults are released with the class, an internal class's
578+
* must persist */
579+
bool persistent = ce->type == ZEND_INTERNAL_CLASS;
580+
zend_string *nameStr = zend_string_init(property.name, len, persistent);
581+
zval defaultValue;
582+
ZVAL_STR(&defaultValue, zend_string_init_interned(property.stringValue, strlen(property.stringValue), persistent));
583+
zend_type type = (zend_type) ZEND_TYPE_INIT_MASK(MAY_BE_STRING);
584+
zend_declare_typed_property(ce, nameStr, &defaultValue, property.visibility, NULL, type);
585+
zend_string_release(nameStr);
586+
break;
587+
}
573588
case PropertyKind::TypedLong: {
574589
bool persistent = ce->type == ZEND_INTERNAL_CLASS;
575590
zend_string *nameStr = zend_string_init(property.name, len, persistent);
@@ -1215,6 +1230,15 @@ class Class
12151230
return *this;
12161231
}
12171232

1233+
/* a `private string $x = '...'` typed property with a string default of
1234+
* any visibility (visibility as for property()); defaultValue is a
1235+
* persistent literal */
1236+
Class &typedStringProperty(const char *propertyName, uint32_t visibility, const char *defaultValue)
1237+
{
1238+
properties.push_back({ propertyName, PropertyKind::TypedString, visibility, 0, nullptr, defaultValue });
1239+
return *this;
1240+
}
1241+
12181242
Class &classConstantValue(const char *constantName, void (*buildValue)(zval *out))
12191243
{
12201244
constants.push_back({ constantName, 0, ZEND_ACC_PUBLIC, nullptr, buildValue });

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

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,6 @@
3737
'PhpParser\\NodeTraverser' => 'pending',
3838
'PHPStan\\Analyser\\ExpressionResultStorage' => 'pending',
3939
'PHPStan\\Analyser\\VariableLivenessResolver' => 'pending',
40-
'PHPStan\\Reflection\\BetterReflection\\SourceLocator\\PhpFileCleaner' => 'pending',
41-
'PHPStan\\Reflection\\BetterReflection\\SourceLocator\\SymbolFinderInFiles' => 'pending',
4240
];
4341

4442
/**

0 commit comments

Comments
 (0)