-
-
Notifications
You must be signed in to change notification settings - Fork 162
[Feature] Add private properties interception #412
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -117,18 +117,24 @@ public function &getValueToSet() | |
*/ | ||
public function ensureScopeRule(int $stackLevel = 2): void | ||
{ | ||
$property = $this->reflectionProperty; | ||
|
||
if ($property->isProtected()) { | ||
$property = $this->reflectionProperty; | ||
$isProtected = $property->isProtected(); | ||
$isPrivate = $property->isPrivate(); | ||
if ($isProtected || $isPrivate) { | ||
$backTrace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, $stackLevel+1); | ||
$accessor = $backTrace[$stackLevel] ?? []; | ||
$propertyClass = $property->class; | ||
if (isset($accessor['class'])) { | ||
if ($accessor['class'] === $propertyClass || is_subclass_of($accessor['class'], $propertyClass)) { | ||
// For private and protected properties its ok to access from the same class | ||
if ($accessor['class'] === $propertyClass) { | ||
return; | ||
} | ||
// For protected properties its ok to access from any subclass | ||
if ($isProtected && is_subclass_of($accessor['class'], $propertyClass)) { | ||
return; | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The exception message in line 137 below is probably no longer accurate: There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks, somehow have forgotten about this line ) |
||
} | ||
throw new AspectException("Cannot access protected property {$propertyClass}::{$property->name}"); | ||
throw new AspectException("Cannot access property {$propertyClass}::{$property->name}"); | ||
} | ||
} | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍