Skip to content

[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

Merged
merged 3 commits into from
Feb 14, 2019

Conversation

lisachenko
Copy link
Member

This PR introduces an ability to intercept access to private properties in classes. We use closure binding in generated proxy child class and bind it to the parent scope so we gain a control over private properties of the class as well.

Here is an example of generated constructor for the Demo\Example\PropertyDemo proxy class:

    public function __construct()
    {
        $accessor = function(array &$propertyStorage, object $target) {
            $propertyStorage = [
                'publicProperty' => &$target->publicProperty,
                'protectedProperty' => &$target->protectedProperty,
                'privateProperty' => &$target->privateProperty,
                'indirectModificationCheck' => &$target->indirectModificationCheck
            ];
            unset(
                $target->publicProperty,
                $target->protectedProperty,
                $target->privateProperty,
                $target->indirectModificationCheck
            );
        };
        ($accessor->bindTo($this, parent::class))($this->__properties, $this);
        parent::__construct();
    }

As we store references to properties from given original object instance, we can read and write them via aspects without additional code logic.

Feature was requested in #411 by @jakzal

@lisachenko lisachenko added this to the 3.x milestone Feb 8, 2019
$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
Copy link
Contributor

Choose a reason for hiding this comment

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

👍

return;
}
// For protected properties its ok to access from any subclass
if ($isProtected && is_subclass_of($accessor['class'], $propertyClass)) {
return;
}
Copy link

Choose a reason for hiding this comment

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

The exception message in line 137 below is probably no longer accurate: Cannot access protected property....

Copy link
Member Author

Choose a reason for hiding this comment

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

Thanks, somehow have forgotten about this line )

@jakzal
Copy link

jakzal commented Feb 8, 2019

This was quick! Thank you for your hard work! 🍺

With this PR, my code to set private's property value from an aspect works now:

            $setter = \Closure::bind(function ($object, $value) use ($field) {
                $object->$field = $value;
            }, null, \get_parent_class($fieldAccess->getThis()));
            $setter($fieldAccess->getThis(), 'some value');

Not sure if you expect the below code to work as well ('cause it doesn't):

            $fieldAccess->getField()->setAccessible(true);
            $fieldAccess->getField()->setValue($fieldAccess->getThis(), 'some value');

@scrutinizer-notifier
Copy link

The inspection completed: 1 updated code elements

@lisachenko lisachenko merged commit 1e35403 into master Feb 14, 2019
@lisachenko lisachenko deleted the feature/private-properties-interception branch February 14, 2019 15:34
@jakzal
Copy link

jakzal commented Oct 15, 2019

@lisachenko any plans to release this feature? :)

@lisachenko
Copy link
Member Author

3.0.0-RC1 is ready to test, if it is ok, then 3.0.0 will be released soon

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

Successfully merging this pull request may close these issues.

4 participants