From c70b2f7aa2cec198acd6991c92ada9851ba9cfb5 Mon Sep 17 00:00:00 2001 From: Timm Friebe Date: Sun, 25 Aug 2024 11:05:39 +0200 Subject: [PATCH] Readonly implies `protected(set)` --- src/main/php/lang/reflection/Modifiers.class.php | 4 ++-- src/main/php/lang/reflection/Property.class.php | 7 ++++++- .../reflection/unittest/VirtualPropertiesTest.class.php | 4 ++-- 3 files changed, 10 insertions(+), 5 deletions(-) diff --git a/src/main/php/lang/reflection/Modifiers.class.php b/src/main/php/lang/reflection/Modifiers.class.php index e80a3e7..92389c8 100755 --- a/src/main/php/lang/reflection/Modifiers.class.php +++ b/src/main/php/lang/reflection/Modifiers.class.php @@ -54,7 +54,7 @@ public function __construct($arg= 0, $visibility= true) { } if ($visibility && 0 === ($this->bits & (self::IS_PROTECTED | self::IS_PRIVATE))) { - $this->bits |= self::IS_PUBLIC; + $this->bits|= self::IS_PUBLIC; } } @@ -67,7 +67,7 @@ public function __construct($arg= 0, $visibility= true) { private static function parse($names) { $bits= 0; foreach ($names as $name) { - $bits |= self::$names[$name]; + $bits|= self::$names[$name]; } return $bits; } diff --git a/src/main/php/lang/reflection/Property.class.php b/src/main/php/lang/reflection/Property.class.php index a6aa01f..7f5cdf4 100755 --- a/src/main/php/lang/reflection/Property.class.php +++ b/src/main/php/lang/reflection/Property.class.php @@ -52,7 +52,10 @@ public function modifiers($hook= null) { Modifiers::IS_PRIVATE_SET => Modifiers::IS_PRIVATE, ]; + // Readonly implies protected(set) $bits= $this->reflect->getModifiers(); + $bits & Modifiers::IS_READONLY && $bits|= Modifiers::IS_PROTECTED_SET; + switch ($hook) { case null: return new Modifiers($bits); case 'get': return new Modifiers(($bits & ~Modifiers::SET_MASK) & Modifiers::GET_MASK); @@ -117,7 +120,9 @@ public function toString() { $name= $t->getName(); } - return Modifiers::namesOf($this->reflect->getModifiers()).' '.$name.' $'.$this->reflect->getName(); + $bits= $this->reflect->getModifiers(); + $bits & Modifiers::IS_READONLY && $bits|= Modifiers::IS_PROTECTED_SET; + return Modifiers::namesOf($bits).' '.$name.' $'.$this->reflect->getName(); } /** diff --git a/src/test/php/lang/reflection/unittest/VirtualPropertiesTest.class.php b/src/test/php/lang/reflection/unittest/VirtualPropertiesTest.class.php index 234c251..c8ab772 100755 --- a/src/test/php/lang/reflection/unittest/VirtualPropertiesTest.class.php +++ b/src/test/php/lang/reflection/unittest/VirtualPropertiesTest.class.php @@ -25,13 +25,13 @@ private function fixtures() { #[Test, Values(from: 'fixtures')] public function readonly_modifier_shown_in_string_representation($type) { - Assert::equals('public readonly string $fixture', $type->property('fixture')->toString()); + Assert::equals('public readonly protected(set) string $fixture', $type->property('fixture')->toString()); } #[Test, Values(from: 'fixtures')] public function virtual_property_included_in_list($type) { Assert::equals( - ['fixture' => 'public readonly'], + ['fixture' => 'public readonly protected(set)'], array_map(fn($p) => $p->modifiers()->names(), iterator_to_array($type->properties())) ); }