Skip to content
Open
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 phpunit/code/inheritance_error_prop_readonly.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ class A

class B extends A
{
public readonly int $x = 2;
public readonly int $x;
}

function main() {}
5 changes: 5 additions & 0 deletions phpunit/code/readonly_class_extends.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?php
readonly class A {}
class B extends A {}

function main() {}
5 changes: 5 additions & 0 deletions phpunit/code/readonly_class_extends_rev.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?php
class A {}
readonly class B extends A {}

function main() {}
5 changes: 5 additions & 0 deletions phpunit/code/readonly_class_extends_valid.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?php
readonly class A { public function __construct(public int $x) {} }
readonly class B extends A {}

function main() {}
4 changes: 4 additions & 0 deletions phpunit/code/readonly_rule_class_static.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?php
readonly class Cfg { public static int $port; }

function main() {}
4 changes: 4 additions & 0 deletions phpunit/code/readonly_rule_class_untyped.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?php
readonly class Cfg { public $port; }

function main() {}
4 changes: 4 additions & 0 deletions phpunit/code/readonly_rule_default.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?php
class Cfg { public readonly int $port = 80; }

function main() {}
4 changes: 4 additions & 0 deletions phpunit/code/readonly_rule_promoted_untyped.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?php
class Cfg { public function __construct(public readonly $port) {} }

function main() {}
4 changes: 4 additions & 0 deletions phpunit/code/readonly_rule_static.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?php
class Cfg { public static readonly int $port; }

function main() {}
4 changes: 4 additions & 0 deletions phpunit/code/readonly_rule_untyped.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?php
class Cfg { public readonly $port; }

function main() {}
4 changes: 4 additions & 0 deletions phpunit/code/readonly_rule_valid.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?php
readonly class Cfg { public int $port; public function __construct(public readonly string $host = "a") { $this->port = 80; } }

function main() {}
30 changes: 30 additions & 0 deletions phpunit/src/ClassKindInheritanceTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

/**
* Readonly-ness is part of the inheritance contract in both directions, and
* an interface can only extend other interfaces.
*/
class ClassKindInheritanceTest extends BaseTest
{
public function testNonReadonlyCannotExtendReadonly(): void
{
$this->exec(
'Non-readonly class `B` cannot extend readonly class `A`',
'readonly_class_extends.php'
);
}

public function testReadonlyCannotExtendNonReadonly(): void
{
$this->exec(
'Readonly class `B` cannot extend non-readonly class `A`',
'readonly_class_extends_rev.php'
);
}


public function testReadonlyExtendsReadonlyIsValid(): void
{
$this->compile('readonly_class_extends_valid.php');
}
}
46 changes: 46 additions & 0 deletions phpunit/src/ReadonlyDeclarationRulesTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php

/**
* Zend readonly property declaration rules for ZendVM-backed classes:
* no defaults, mandatory type, no static readonly, and the readonly
* class modifier applying the same rules to every property.
*/
class ReadonlyDeclarationRulesTest extends BaseTest
{
public function testReadonlyPropertyCannotHaveDefault(): void
{
$this->exec('Readonly property `Cfg::$port` cannot have default value', 'readonly_rule_default.php');
}

public function testReadonlyPropertyMustHaveType(): void
{
$this->exec('Readonly property `Cfg::$port` must have type', 'readonly_rule_untyped.php');
}

public function testStaticPropertyCannotBeReadonly(): void
{
$this->exec('Static property `Cfg::$port` cannot be readonly', 'readonly_rule_static.php');
}

public function testPromotedReadonlyParamMustHaveType(): void
{
$this->exec('Readonly property `Cfg::$port` must have type', 'readonly_rule_promoted_untyped.php');
}

public function testReadonlyClassPropertyMustHaveType(): void
{
$this->exec('Readonly property `Cfg::$port` must have type', 'readonly_rule_class_untyped.php');
}

public function testReadonlyClassCannotDeclareStaticProperty(): void
{
$this->exec('Static property `Cfg::$port` cannot be readonly', 'readonly_rule_class_static.php');
}

public function testWellFormedReadonlyDeclarationsStillCompile(): void
{
// Promoted readonly params may keep a parameter default: it belongs
// to the constructor argument, not to the property.
$this->compile('readonly_rule_valid.php');
}
}
15 changes: 15 additions & 0 deletions src/Preprocessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -1674,6 +1674,21 @@ protected function addClassProperty(string $name, int $flags, ?NodeAbstract $typ
);
}
$flags = $this->parseModifiers($flags);
// A `readonly class` marks every property readonly, so the class-level
// flag participates in the same Zend declaration rules as an explicit
// per-property `readonly` modifier.
if (($flags | $this->classDef->flags) & Modifiers::READONLY) {
$className = $this->classDef->getNamespacedName(false);
if ($flags & Modifiers::STATIC) {
$this->fatalError($errorNode, "Static property `{$className}::\${$name}` cannot be readonly");
}
if ($typeNode === null) {
$this->fatalError($errorNode, "Readonly property `{$className}::\${$name}` must have type");
}
if ($defaultNode !== null) {
$this->fatalError($errorNode, "Readonly property `{$className}::\${$name}` cannot have default value");
}
}
$this->validateAsymmetricPropertyDeclaration($name, $flags, $typeNode, $errorNode);
[$type, $class] = $this->resolveTypeDecl($typeNode, self::DECL_TYPE_OF_PROPERTY);
$this->assertSupportedNativeObjectTypeNode($typeNode, self::DECL_TYPE_OF_PROPERTY, $errorNode);
Expand Down
10 changes: 10 additions & 0 deletions src/Translator.php
Original file line number Diff line number Diff line change
Expand Up @@ -4027,6 +4027,16 @@ protected function parseClass(Node\Stmt\Class_|Node\Stmt\Trait_|Node\Stmt\Enum_
if ($parent->flags & Modifiers::FINAL) {
$this->fatalError($class, "Class `{$this->class}` cannot extend final class `{$parentClass}`");
}
// Readonly-ness is part of the inheritance contract in both
// directions (Zend: a readonly class seals its property
// semantics for the whole hierarchy).
$childReadonly = (bool) ($this->classDef->flags & Modifiers::READONLY);
$parentReadonly = (bool) ($parent->flags & Modifiers::READONLY);
if ($childReadonly !== $parentReadonly) {
$this->fatalError($class, $parentReadonly
? "Non-readonly class `{$this->class}` cannot extend readonly class `{$parentClass}`"
: "Readonly class `{$this->class}` cannot extend non-readonly class `{$parentClass}`");
}
} else {
$this->fatalError($class, "Class `{$this->class}` inherits from a non-existent class `{$parentClass}`");
}
Expand Down
Loading