Skip to content

Add support for generating readonly properties via stubs #7297

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jul 22, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions Zend/tests/type_declarations/typed_properties_095.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@ object(_ZendTestClass)#1 (3) {
}
["classUnionProp"]=>
NULL
["readonlyProp"]=>
uninitialized(int)
}
int(123)
Cannot assign string to property _ZendTestClass::$intProp of type int
Expand All @@ -82,6 +84,8 @@ object(Test)#4 (3) {
}
["classUnionProp"]=>
NULL
["readonlyProp"]=>
uninitialized(int)
}
int(123)
Cannot assign string to property _ZendTestClass::$staticIntProp of type int
Expand Down
8 changes: 6 additions & 2 deletions build/gen_stub.php
Original file line number Diff line number Diff line change
Expand Up @@ -1178,6 +1178,10 @@ private function getFlagsAsString(): string
$flags .= "|ZEND_ACC_STATIC";
}

if ($this->flags & Class_::MODIFIER_READONLY) {
$flags .= "|ZEND_ACC_READONLY";
}

return $flags;
}
}
Expand Down Expand Up @@ -1806,7 +1810,7 @@ function handleStatements(FileInfo $fileInfo, array $stmts, PrettyPrinterAbstrac
}

function parseStubFile(string $code): FileInfo {
$lexer = new PhpParser\Lexer();
$lexer = new PhpParser\Lexer\Emulative();
$parser = new PhpParser\Parser\Php7($lexer);
$nodeTraverser = new PhpParser\NodeTraverser;
$nodeTraverser->addVisitor(new PhpParser\NodeVisitor\NameResolver);
Expand Down Expand Up @@ -2303,7 +2307,7 @@ function initPhpParser() {
}

$isInitialized = true;
$version = "4.9.0";
$version = "4.12.0";
$phpParserDir = __DIR__ . "/PHP-Parser-$version";
if (!is_dir($phpParserDir)) {
installPhpParser($version, $phpParserDir);
Expand Down
1 change: 1 addition & 0 deletions ext/zend_test/test.stub.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ class _ZendTestClass implements _ZendTestInterface {
public int $intProp = 123;
public ?stdClass $classProp = null;
public stdClass|Iterator|null $classUnionProp = null;
public readonly int $readonlyProp;

public static function is_object(): int {}

Expand Down
8 changes: 7 additions & 1 deletion ext/zend_test/test_arginfo.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* This is a generated file, edit the .stub.php file instead.
* Stub hash: 6efccee97845bb4176d25226cadaedbfc6de961d */
* Stub hash: 2a1f8ff8205507259ba19bd379a07b390bc525cd */

ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_zend_test_array_return, 0, 0, IS_ARRAY, 0)
ZEND_END_ARG_INFO()
Expand Down Expand Up @@ -227,6 +227,12 @@ static zend_class_entry *register_class__ZendTestClass(zend_class_entry *class_e
zend_declare_typed_property(class_entry, property_classUnionProp_name, &property_classUnionProp_default_value, ZEND_ACC_PUBLIC, NULL, property_classUnionProp_type);
zend_string_release(property_classUnionProp_name);

zval property_readonlyProp_default_value;
ZVAL_UNDEF(&property_readonlyProp_default_value);
zend_string *property_readonlyProp_name = zend_string_init("readonlyProp", sizeof("readonlyProp") - 1, 1);
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));
zend_string_release(property_readonlyProp_name);

return class_entry;
}

Expand Down