Skip to content

Add support for asymmetric visibility for properties - #43

Merged
thekid merged 6 commits into
mainfrom
feature/asymmetric-visibility
Aug 26, 2024
Merged

Add support for asymmetric visibility for properties#43
thekid merged 6 commits into
mainfrom
feature/asymmetric-visibility

Conversation

@thekid

@thekidthekid commented Aug 24, 2024

Copy link
Copy Markdown
Member

Example

Given the following:

uselang\Reflection;
class Person {
publicprivate(set)string$name= 'Test';
}
$property= Reflection::type(Person::class)->property('name');

The modifiers class includes private(set) and protected(set) support:

$property->modifiers(); // lang.reflect.Modifiers<public private(set)>

The isPublic(), isProtected() and isPrivate() methods optionally support "get" or "set":

$property->modifiers()->isPublic('get'); // true$property->modifiers()->isPrivate('set'); // true

The modifiers() method can be used with "get" or "set" to produce views on the respective visibility:

$property->modifiers('get')->isPublic(); // true$property->modifiers('set')->isPrivate(); // true

Tests

The following configurations have run all unit tests successfully:

See also

@thekidthekid added the enhancement New feature or request label Aug 24, 2024
thekid added a commit to xp-framework/compiler that referenced this pull request Aug 25, 2024
@thekid

thekid commented Aug 25, 2024

Copy link
Copy Markdown
MemberAuthor

Here's the modifiers as returned by ReflectionProperty::getModifiers() in PHP 8.4 compiled from php/php-src#15063:

DeclarationModifiersComment
class T { public int $prop; }0x00011️⃣
class T { public public(set) int $prop; }0x00012️⃣
class T { protected protected(set) int $prop; }0x00022️⃣
class T { private private(set) int $prop; }0x00042️⃣
class T { public protected(set) int $prop; }0x0801
class T { public private(set) int $prop; }0x1001
class T { proteced private(set) int $prop; }0x1002

1️⃣ - For comparison with the next line
2️⃣ - Note that if get and set visibilities are the same, the set bit is emitted

private static $ASYMMETRIC;

static function __static() {
self::$ASYMMETRIC= method_exists(\ReflectionProperty::class, 'isPrivateSet');

Copy link
Copy Markdown
MemberAuthor

Choose a reason for hiding this comment

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

This can be replaced by #[Runtime(version: '>=8.4')] once the PR is merged.

@thekid
thekid merged commit fe96140 into mainAug 26, 2024
@thekid
thekid deleted the feature/asymmetric-visibility branch August 26, 2024 19:23
@thekid

Copy link
Copy Markdown
MemberAuthor

thekid added a commit that referenced this pull request Aug 27, 2024
@thekid

Copy link
Copy Markdown
MemberAuthor

Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancementNew feature or request

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

1 participant

@thekid