Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

[PropertyAccess][PropertyInfo] customize behavior for property hooks on read and write #59246

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

Open
wants to merge 1 commit into
base: 7.3
Choose a base branch
Loading
from

Conversation

xavierleune
Copy link
Contributor

@xavierleune xavierleune commented Dec 18, 2024

Q A
Branch? 7.3
Bug fix? no
New feature? yes
Deprecations? no
Issues ¤
License MIT

This pull requests adds 2 new (tiny) features:

  • PropertyInfo: expose data around property hooks for readInfo and writeInfo
  • PropertyAccess: allow the end user to customize behavior around property hooks, to bypass them if needed

Have a nice day!

@xavierleune xavierleune requested a review from dunglas as a code owner December 18, 2024 10:10
@carsonbot carsonbot added this to the 7.3 milestone Dec 18, 2024
@carsonbot carsonbot changed the title [PropertyInfo] [PropertyAccess] Feature: customize behavior for property hooks on read and write [PropertyAccess][PropertyInfo] Feature: customize behavior for property hooks on read and write Dec 18, 2024
@xavierleune xavierleune force-pushed the feature/property-hooks branch from c4ef85d to 399efcf Compare December 18, 2024 10:11
public bool $virtualSetHookOnly { set => $value; }
public bool $virtualHook { get => true; set => $value; }
public bool $virtualSetHookOnly { set (bool $value) { } }
public bool $virtualHook { get => true; set (bool $value) { } }
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I updated this fixtures because PHP does not report these properties as Virtual, see reproducer

@xavierleune xavierleune force-pushed the feature/property-hooks branch 2 times, most recently from 0900ef7 to 379e96c Compare December 18, 2024 10:38
@xavierleune
Copy link
Contributor Author

Tests are failing on 8.2 / highest deps because of other components (Bridge\twig, Bundle\FrameworkBundle, Component\HttpKernel). It leaves us with psalm for which PHP 8.4 support can be tracked here: vimeo/psalm#11107. Not sure how to deal with it here.

@xavierleune xavierleune force-pushed the feature/property-hooks branch 3 times, most recently from a396856 to 73f2e3f Compare December 20, 2024 13:39
@xavierleune
Copy link
Contributor Author

@mtarld Thanks for your feedback, I think everything is covered now :)

@xavierleune xavierleune force-pushed the feature/property-hooks branch 2 times, most recently from bd1f4a1 to 0c5bb13 Compare December 20, 2024 16:10
@xavierleune xavierleune force-pushed the feature/property-hooks branch from 0c5bb13 to 0eaa1d4 Compare March 20, 2025 10:10
@xavierleune
Copy link
Contributor Author

Thanks @mtarld for your previous review, sorry I took so long to make the changes.

@OskarStark OskarStark changed the title [PropertyAccess][PropertyInfo] Feature: customize behavior for property hooks on read and write [PropertyAccess][PropertyInfo] customize behavior for property hooks on read and write Mar 21, 2025
@xavierleune xavierleune force-pushed the feature/property-hooks branch from 0eaa1d4 to 0d100a9 Compare March 24, 2025 09:02
@xavierleune
Copy link
Contributor Author

@OskarStark thanks for your feedback, I renamed the variables.

@xavierleune
Copy link
Contributor Author

Hi @mtarld do you think we might be good on this pull request ? Thanks ! 🙏

@OskarStark
Copy link
Contributor

OskarStark commented Apr 1, 2025

Would it be sufficient to split those PR into two separate feature PRs?

src/Symfony/Component/PropertyAccess/PropertyAccessor.php Outdated Show resolved Hide resolved
src/Symfony/Component/PropertyAccess/PropertyAccessor.php Outdated Show resolved Hide resolved
src/Symfony/Component/PropertyAccess/PropertyAccessor.php Outdated Show resolved Hide resolved
src/Symfony/Component/PropertyAccess/PropertyAccessor.php Outdated Show resolved Hide resolved
src/Symfony/Component/PropertyAccess/PropertyAccessor.php Outdated Show resolved Hide resolved
Comment on lines +395 to 401
if ($hasProperty && (($r = $reflClass->getProperty($property))->getModifiers() & $this->propertyReflectionFlags)) {
return new PropertyReadInfo(PropertyReadInfo::TYPE_PROPERTY, $property, $this->getReadVisibilityForProperty($r), $r->isStatic(), true, $this->propertyHasHook($r, 'get'), $this->propertyIsVirtual($r));
}

if ($hasProperty && (($r = $reflClass->getProperty($property))->getModifiers() & $this->propertyReflectionFlags)) {
return new PropertyReadInfo(PropertyReadInfo::TYPE_PROPERTY, $property, $this->getReadVisibilityForProperty($r), $r->isStatic(), true);
if ($allowMagicGet && $reflClass->hasMethod('__get') && (($r = $reflClass->getMethod('__get'))->getModifiers() & $this->methodReflectionFlags)) {
return new PropertyReadInfo(PropertyReadInfo::TYPE_PROPERTY, $property, PropertyReadInfo::VISIBILITY_PUBLIC, false, $r->returnsReference(), false, false);
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Why these ifs has been inverted?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

See that comment #59246 (comment)

@xavierleune xavierleune force-pushed the feature/property-hooks branch from 0d100a9 to 3990cad Compare April 9, 2025 13:23
@xavierleune
Copy link
Contributor Author

@mtarld thanks for the feedback

Copy link
Member

@stof stof left a comment

Choose a reason for hiding this comment

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

I'm not sure I like this new feature of PropertyAccess. Until now, PropertyAccess never accesses the internal state of the object through Reflection (Reflection is used only for instrospection of the class structure). All reads and writes are done through the public API of the class until now.

@@ -77,19 +82,24 @@ class PropertyAccessor implements PropertyAccessorInterface
* or self::DISALLOW_MAGIC_METHODS for none
* @param int $throw A bitwise combination of the THROW_* constants
* to specify when exceptions should be thrown
* @param int $bypassPropertyHooks A bitwise combination of the BYPASS_PROPERTY_HOOK_* constants
Copy link
Member

Choose a reason for hiding this comment

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

@param int-mask-of<self::BYPASS_PROPERTY_HOOK_*> $bypassPropertyHooks so that static analysis tool know about the precise type and can report invalid usages.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

thx, didn't know that syntax

@@ -47,6 +47,10 @@ class PropertyAccessor implements PropertyAccessorInterface
/** @var int Allow magic __call methods */
public const MAGIC_CALL = ReflectionExtractor::ALLOW_MAGIC_CALL;

public const BYPASS_PROPERTY_HOOK_NONE = 0;
public const BYPASS_PROPERTY_HOOK_READ = 1 << 1;
public const BYPASS_PROPERTY_HOOK_WRITE = 1 << 0;
Copy link
Member

Choose a reason for hiding this comment

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

constants for a bit mask are generally defined in increasing order (so that new cases are added at the end of the list). This reverse order is unusual to me

@xavierleune xavierleune force-pushed the feature/property-hooks branch from 3990cad to 7963304 Compare April 10, 2025 07:40
@xavierleune xavierleune force-pushed the feature/property-hooks branch from 7963304 to e4901fb Compare May 5, 2025 09:12
@xavierleune
Copy link
Contributor Author

@stof I thought it would be very relevant in this component as this follows new opportunities offered by the language, wdyt ?

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

Successfully merging this pull request may close these issues.

5 participants
Morty Proxy This is a proxified and sanitized view of the page, visit original site.