Skip to content

Commit fc56de1

Browse files
authored
Add support for generating readonly properties via stubs (#7297)
1 parent 8a26cbe commit fc56de1

File tree

4 files changed

+17
-2
lines changed

4 files changed

+17
-2
lines changed

Zend/tests/type_declarations/typed_properties_095.phpt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,8 @@ object(_ZendTestClass)#1 (3) {
7070
}
7171
["classUnionProp"]=>
7272
NULL
73+
["readonlyProp"]=>
74+
uninitialized(int)
7375
}
7476
int(123)
7577
Cannot assign string to property _ZendTestClass::$intProp of type int
@@ -82,6 +84,8 @@ object(Test)#4 (3) {
8284
}
8385
["classUnionProp"]=>
8486
NULL
87+
["readonlyProp"]=>
88+
uninitialized(int)
8589
}
8690
int(123)
8791
Cannot assign string to property _ZendTestClass::$staticIntProp of type int

build/gen_stub.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1178,6 +1178,10 @@ private function getFlagsAsString(): string
11781178
$flags .= "|ZEND_ACC_STATIC";
11791179
}
11801180

1181+
if ($this->flags & Class_::MODIFIER_READONLY) {
1182+
$flags .= "|ZEND_ACC_READONLY";
1183+
}
1184+
11811185
return $flags;
11821186
}
11831187
}
@@ -1806,7 +1810,7 @@ function handleStatements(FileInfo $fileInfo, array $stmts, PrettyPrinterAbstrac
18061810
}
18071811

18081812
function parseStubFile(string $code): FileInfo {
1809-
$lexer = new PhpParser\Lexer();
1813+
$lexer = new PhpParser\Lexer\Emulative();
18101814
$parser = new PhpParser\Parser\Php7($lexer);
18111815
$nodeTraverser = new PhpParser\NodeTraverser;
18121816
$nodeTraverser->addVisitor(new PhpParser\NodeVisitor\NameResolver);

ext/zend_test/test.stub.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ class _ZendTestClass implements _ZendTestInterface {
1717
public int $intProp = 123;
1818
public ?stdClass $classProp = null;
1919
public stdClass|Iterator|null $classUnionProp = null;
20+
public readonly int $readonlyProp;
2021

2122
public static function is_object(): int {}
2223

ext/zend_test/test_arginfo.h

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/* This is a generated file, edit the .stub.php file instead.
2-
* Stub hash: 6efccee97845bb4176d25226cadaedbfc6de961d */
2+
* Stub hash: 2a1f8ff8205507259ba19bd379a07b390bc525cd */
33

44
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_zend_test_array_return, 0, 0, IS_ARRAY, 0)
55
ZEND_END_ARG_INFO()
@@ -227,6 +227,12 @@ static zend_class_entry *register_class__ZendTestClass(zend_class_entry *class_e
227227
zend_declare_typed_property(class_entry, property_classUnionProp_name, &property_classUnionProp_default_value, ZEND_ACC_PUBLIC, NULL, property_classUnionProp_type);
228228
zend_string_release(property_classUnionProp_name);
229229

230+
zval property_readonlyProp_default_value;
231+
ZVAL_UNDEF(&property_readonlyProp_default_value);
232+
zend_string *property_readonlyProp_name = zend_string_init("readonlyProp", sizeof("readonlyProp") - 1, 1);
233+
zend_declare_typed_property(class_entry, property_readonlyProp_name, &property_readonlyProp_default_value, ZEND_ACC_PUBLIC|ZEND_ACC_READONLY, NULL, (zend_type) ZEND_TYPE_INIT_MASK(MAY_BE_LONG));
234+
zend_string_release(property_readonlyProp_name);
235+
230236
return class_entry;
231237
}
232238

0 commit comments

Comments
 (0)