Skip to content

Commit

Permalink
Simplify withScopeCheck()
Browse files Browse the repository at this point in the history
  • Loading branch information
thekid committed May 18, 2023
1 parent ed5003f commit 6031c35
Showing 1 changed file with 6 additions and 6 deletions.
12 changes: 6 additions & 6 deletions src/main/php/lang/ast/emit/PropertyHooks.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,24 +41,24 @@ protected function rewriteHook($node, $name, $virtual, $literal) {
return $node;
}

protected function withScopeCheck($modifiers, $name, $node) {
protected function withScopeCheck($modifiers, $node) {
if ($modifiers & MODIFIER_PRIVATE) {
$check= (
'$scope= debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 2)[1]["class"] ?? null;'.
'if (__CLASS__ !== $scope && \\lang\\VirtualProperty::class !== $scope)'.
'throw new \\Error("Cannot access private property ".__CLASS__."::\\$%1$s");'
'throw new \\Error("Cannot access private property ".__CLASS__."::".$name);'
);
} else if ($modifiers & MODIFIER_PROTECTED) {
$check= (
'$scope= debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 2)[1]["class"] ?? null;'.
'if (__CLASS__ !== $scope && !is_subclass_of($scope, __CLASS__) && \\lang\\VirtualProperty::class !== $scope)'.
'throw new \\Error("Cannot access protected property ".__CLASS__."::\\$%1$s");'
'throw new \\Error("Cannot access protected property ".__CLASS__."::".$name);'
);
} else {
return $node;
}

return new Block([new Code(sprintf($check, $name)), $node]);
return new Block([new Code($check), $node]);
}

protected function emitProperty($result, $property) {
Expand Down Expand Up @@ -109,7 +109,7 @@ protected function emitProperty($result, $property) {
)],
null // $hook->annotations
));
$get= $this->withScopeCheck($modifiers, $property->name, new ReturnStatement(new InvokeExpression(
$get= $this->withScopeCheck($modifiers, new ReturnStatement(new InvokeExpression(
new InstanceExpression(new Variable('this'), new Literal($method)),
[]
)));
Expand All @@ -126,7 +126,7 @@ protected function emitProperty($result, $property) {
)],
null // $hook->annotations
));
$set= $this->withScopeCheck($modifiers, $property->name, new InvokeExpression(
$set= $this->withScopeCheck($modifiers, new InvokeExpression(
new InstanceExpression(new Variable('this'), new Literal($method)),
[new Variable('value')]
));
Expand Down

0 comments on commit 6031c35

Please sign in to comment.