Skip to content
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
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"keywords": ["module", "xp"],
"require" : {
"xp-framework/core": "^11.0 | ^10.0",
"xp-framework/ast": "^8.0",
"xp-framework/ast": "^8.1",
"php" : ">=7.0.0"
},
"require-dev" : {
Expand Down
14 changes: 9 additions & 5 deletions src/main/php/lang/ast/emit/PHP.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -383,7 +383,7 @@ protected function emitEnum($result, $enum) {
protected function emitClass($result, $class) {
array_unshift($result->type, $class);
array_unshift($result->meta, []);
$result->locals= [[], [], []];
$result->locals ?: $result->locals= [[], [], []];

$class->comment && $this->emitOne($result, $class->comment);
$class->annotations && $this->emitOne($result, $class->annotations);
Expand All @@ -399,21 +399,24 @@ protected function emitClass($result, $class) {
if ($result->locals[2]) {
$result->out->write('private $__virtual= [');
foreach ($result->locals[2] as $name => $access) {
$result->out->write("'{$name}' => null,");
$name && $result->out->write("'{$name}' => null,");
}
$result->out->write('];');

$result->out->write('public function __get($name) { switch ($name) {');
foreach ($result->locals[2] as $name => $access) {
$result->out->write('case "'.$name.'":');
$result->out->write($name ? 'case "'.$name.'":' : 'default:');
$this->emitOne($result, $access[0]);
$result->out->write('break;');
}
$result->out->write('default: trigger_error("Undefined property ".__CLASS__."::".$name, E_USER_WARNING); }}');
isset($result->locals[2][null]) || $result->out->write(
'default: trigger_error("Undefined property ".__CLASS__."::".$name, E_USER_WARNING);'
);
$result->out->write('}}');

$result->out->write('public function __set($name, $value) { switch ($name) {');
foreach ($result->locals[2] as $name => $access) {
$result->out->write('case "'.$name.'":');
$result->out->write($name ? 'case "'.$name.'":' : 'default:');
$this->emitOne($result, $access[1]);
$result->out->write('break;');
}
Expand All @@ -433,6 +436,7 @@ protected function emitClass($result, $class) {
$this->emitMeta($result, $class->name, $class->annotations, $class->comment);
$result->out->write('}} '.$class->name.'::__init();');
array_shift($result->type);
$result->locals= [];
}

protected function emitMeta($result, $name, $annotations, $comment) {
Expand Down
1 change: 1 addition & 0 deletions src/main/php/lang/ast/emit/PHP70.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ class PHP70 extends PHP {
OmitArgumentNames,
OmitConstModifiers,
OmitPropertyTypes,
ReadonlyClasses,
ReadonlyProperties,
RewriteClassOnObjects,
RewriteEnums,
Expand Down
1 change: 1 addition & 0 deletions src/main/php/lang/ast/emit/PHP71.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ class PHP71 extends PHP {
OmitArgumentNames,
OmitPropertyTypes,
ReadonlyProperties,
ReadonlyClasses,
RewriteClassOnObjects,
RewriteEnums,
RewriteExplicitOctals,
Expand Down
1 change: 1 addition & 0 deletions src/main/php/lang/ast/emit/PHP72.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ class PHP72 extends PHP {
OmitArgumentNames,
OmitPropertyTypes,
ReadonlyProperties,
ReadonlyClasses,
RewriteClassOnObjects,
RewriteEnums,
RewriteExplicitOctals,
Expand Down
1 change: 1 addition & 0 deletions src/main/php/lang/ast/emit/PHP74.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ class PHP74 extends PHP {
NonCapturingCatchVariables,
NullsafeAsTernaries,
OmitArgumentNames,
ReadonlyClasses,
ReadonlyProperties,
RewriteBlockLambdaExpressions,
RewriteClassOnObjects,
Expand Down
3 changes: 2 additions & 1 deletion src/main/php/lang/ast/emit/PHP80.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
* @see https://wiki.php.net/rfc#php_80
*/
class PHP80 extends PHP {
use RewriteBlockLambdaExpressions, RewriteExplicitOctals, RewriteEnums, ReadonlyProperties, CallablesAsClosures, ArrayUnpackUsingMerge;
use RewriteBlockLambdaExpressions, RewriteExplicitOctals, RewriteEnums;
use ReadonlyClasses, ReadonlyProperties, CallablesAsClosures, ArrayUnpackUsingMerge;

/** Sets up type => literal mappings */
public function __construct() {
Expand Down
2 changes: 1 addition & 1 deletion src/main/php/lang/ast/emit/PHP81.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
* @see https://wiki.php.net/rfc#php_81
*/
class PHP81 extends PHP {
use RewriteBlockLambdaExpressions;
use RewriteBlockLambdaExpressions, ReadonlyClasses;

/** Sets up type => literal mappings */
public function __construct() {
Expand Down
2 changes: 1 addition & 1 deletion src/main/php/lang/ast/emit/PHP82.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
* @see https://wiki.php.net/rfc#php_82
*/
class PHP82 extends PHP {
use RewriteBlockLambdaExpressions;
use RewriteBlockLambdaExpressions, ReadonlyClasses;

/** Sets up type => literal mappings */
public function __construct() {
Expand Down
36 changes: 36 additions & 0 deletions src/main/php/lang/ast/emit/ReadonlyClasses.class.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php namespace lang\ast\emit;

use lang\ast\Code;

/**
* Implements readonly properties by removing the `readonly` modifier from
* the class and inheriting it to all properties (and promoted constructor
* arguments).
*
* @see https://wiki.php.net/rfc/readonly_classes
*/
trait ReadonlyClasses {

protected function emitClass($result, $class) {
if (false !== ($p= array_search('readonly', $class->modifiers))) {
unset($class->modifiers[$p]);

// Inherit
foreach ($class->body as $member) {
if ($member->is('property')) {
$member->modifiers[]= 'readonly';
} else if ($member->is('method')) {
foreach ($member->signature->parameters as $param) {
$param->promote && $param->promote.= ' readonly';
}
}
}

// Prevent dynamic members
$throw= new Code('throw new \\Error("Cannot create dynamic property ".__CLASS__."::".$name);');
$result->locals= [[], [], [null => [$throw, $throw]]];
}

return parent::emitClass($result, $class);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@
use unittest\{Assert, Expect, Test};

/**
* Readonly properties
* Readonly classes and properties
*
* @see https://wiki.php.net/rfc/readonly_properties_v2
* @see https://wiki.php.net/rfc/readonly_classes
*/
class ReadonlyPropertiesTest extends EmittingTest {
class ReadonlyTest extends EmittingTest {

/** @return iterable */
private function modifiers() {
Expand All @@ -20,7 +21,19 @@ private function modifiers() {
}

#[Test]
public function declaration() {
public function class_declaration() {
$t= $this->type('readonly class <T> {
public int $fixture;
}');

Assert::equals(
sprintf('public readonly int %s::$fixture', $t->getName()),
$t->getField('fixture')->toString()
);
}

#[Test]
public function property_declaration() {
$t= $this->type('class <T> {
public readonly int $fixture;
}');
Expand All @@ -32,11 +45,28 @@ public function declaration() {
}

#[Test]
public function with_constructor_argument_promotion() {
public function class_with_constructor_argument_promotion() {
$t= $this->type('readonly class <T> {
public function __construct(public string $fixture) { }
}');

Assert::equals(
sprintf('public readonly string %s::$fixture', $t->getName()),
$t->getField('fixture')->toString()
);
Assert::equals('Test', $t->newInstance('Test')->fixture);
}

#[Test]
public function property_defined_with_constructor_argument_promotion() {
$t= $this->type('class <T> {
public function __construct(public readonly string $fixture) { }
}');

Assert::equals(
sprintf('public readonly string %s::$fixture', $t->getName()),
$t->getField('fixture')->toString()
);
Assert::equals('Test', $t->newInstance('Test')->fixture);
}

Expand Down Expand Up @@ -143,4 +173,23 @@ public function cannot_have_an_initial_value() {
public readonly string $fixture= "Test";
}');
}

#[Test, Expect(class: Error::class, withMessage: '/Cannot create dynamic property .+fixture/')]
public function cannot_read_dynamic_members_from_readonly_classes() {
$t= $this->type('readonly class <T> { }');
$t->newInstance()->fixture;
}

#[Test, Expect(class: Error::class, withMessage: '/Cannot create dynamic property .+fixture/')]
public function cannot_write_dynamic_members_from_readonly_classes() {
$t= $this->type('readonly class <T> { }');
$t->newInstance()->fixture= true;
}

#[Test, Ignore('Until proper error handling facilities exist')]
public function readonly_classes_cannot_have_static_members() {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This proper error handling means any way of raising an exception from within the emitting phase and having it turned into a lang.ast.Error with file and line set.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe these also need to go into the parser - where, instead of returning lang.ast.Node instances directly, we'd do something like:

// Before
return new PropertyNode(...);

// After
return $this->ast->property(...);

...and have two separate ast implementations: checked and unchecked. This would be part of a greater refactoring, thus keeping it for a separate pull request.

$this->type('readonly class <T> {
public static $test;
}');
}
}