Skip to content

Commit

Permalink
Mask out all other modifiers when modifiers([get|set]) is called
Browse files Browse the repository at this point in the history
  • Loading branch information
thekid committed Aug 25, 2024
1 parent 5813384 commit b0122e4
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 2 deletions.
3 changes: 3 additions & 0 deletions src/main/php/lang/reflection/Modifiers.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ class Modifiers implements Value {
const IS_PRIVATE_SET = 0x1000;
const IS_NATIVE = 0x10000;

const GET_MASK = 0x0007; // PUBLIC | PROTECTED | PRIVATE
const SET_MASK = 0x1c00; // PUBLIC_SET | PROTECTED_SET | PRIVATE_SET

private static $names= [
'public' => self::IS_PUBLIC,
'protected' => self::IS_PROTECTED,
Expand Down
4 changes: 2 additions & 2 deletions src/main/php/lang/reflection/Property.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@ public function modifiers($hook= null) {
$bits= $this->reflect->getModifiers();
switch ($hook) {
case null: return new Modifiers($bits);
case 'get': return new Modifiers($bits & ~0x1c00);
case 'set': return new Modifiers($set[$bits & 0x1c00] ?? $bits);
case 'get': return new Modifiers(($bits & ~Modifiers::SET_MASK) & Modifiers::GET_MASK);
case 'set': return new Modifiers($set[$bits & Modifiers::SET_MASK] ?? $bits & Modifiers::GET_MASK);
default: throw new IllegalArgumentException('Unknown hook '.$hook);
}
}
Expand Down
16 changes: 16 additions & 0 deletions src/test/php/lang/reflection/unittest/PropertiesTest.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,22 @@ public function set_modifiers($modifier) {
);
}

#[Test]
public function get_modifiers_erases_static() {
Assert::equals(
new Modifiers('public'),
$this->declare('{ public static int $fixture; }')->property('fixture')->modifiers('get')
);
}

#[Test]
public function set_modifiers_erases_static() {
Assert::equals(
new Modifiers('public'),
$this->declare('{ public static int $fixture; }')->property('fixture')->modifiers('set')
);
}

#[Test, Expect(IllegalArgumentException::class)]
public function modifiers_unknown_hook() {
$this->declare('{ private $fixture; }')->property('fixture')->modifiers('@unknown');
Expand Down

0 comments on commit b0122e4

Please sign in to comment.