Skip to content

Commit

Permalink
Readonly implies protected(set)
Browse files Browse the repository at this point in the history
  • Loading branch information
thekid committed Aug 25, 2024
1 parent b0122e4 commit c70b2f7
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 5 deletions.
4 changes: 2 additions & 2 deletions src/main/php/lang/reflection/Modifiers.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}

Expand All @@ -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;
}
Expand Down
7 changes: 6 additions & 1 deletion src/main/php/lang/reflection/Property.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()))
);
}
Expand Down

0 comments on commit c70b2f7

Please sign in to comment.