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
[PHP 8.2] Add rule for AllowDynamicProperties attribute #1225
[PHP 8.2] Add rule for AllowDynamicProperties attribute #1225
Conversation
I think this should be configurable, and make default to false (or make to specific classes only maybe?) to avoid unnecessary change, you can implements
with set defalut value:
the class will need a rector-src/rules/Php74/Rector/Property/TypedPropertyRector.php Lines 190 to 194 in 82de366
|
rules-tests/Php82/Rector/Class_/AllowDynamicPropertiesRector/Fixture/some_other_class.php.inc
Outdated
Show resolved
Hide resolved
@samsonasik - I'm unsure if this rector rule should even actually be a default for PHP 8.2 overall. Or one that you would manually add to your config since it's not the de-facto solution for PHP 8.2/9.0 updates. The change of the RFC realistically has two main paths to address the BC deprecation and pending BC break. The way I see it this is the simple and easy solution, but not necessarily the "best" solution. Using a rule that detects dynamic prop usage and adds properly typed properties would be more "forward thinking" for the RFCs intent. Note: This PR was going based on my interpretation of the chat I had with @afilina on twitter. So I'm mostly going on her wealth of experience and wisdom there - granted I could have misunderstood. Initially I thought the rule should do what it can to detect when dynamic props are used and only add the attribute then. However Anna suggested just making he rule add it to all classes, see here: https://twitter.com/afilina/status/1459312382895964166 So with that in mind maybe I shouldn't have staged this rule in the |
Given the intended purpose of this rector rule I'm thinking I should have created it under either: |
Should this rector be part of a default 8.2 upgrade? In my opinion, it shouldn't. There are different upgrade paths. The team may or may not want to address tech debt in their code at the same time as they upgrade to the latest PHP. This means that they may either opt for this quick fix, or a more involved approach using a combination of this other rector, automated tests and manual fixes. Should we attempt to detect dynamic properties before setting the attribute? We may have this as an option, but not as the default behavior. Using static analysis, we cannot accurately guess that a class will have a dynamic property created. |
Great! Thank you @afilina - i've refactored the PR a bit to put this under the Also I adjusted the name of the rector to |
I think under |
rules/Transform/Rector/Class_/AddAllowDynamicPropertiesAttributeRector.php
Outdated
Show resolved
Hide resolved
and remove unused imports
rules/Transform/Rector/Class_/AddAllowDynamicPropertiesAttributeRector.php
Outdated
Show resolved
Hide resolved
…teRector.php Co-authored-by: Abdul Malik Ikhsan <samsonasik@gmail.com>
As I've been working on adjusting for feedback I've been thinking about the RFC a lot. I've re-read it and come to a new understanding for cases that this attribute shouldn't need to be applied. Ignoring the fact that we can't reliably detect when a dynamic property is used and only apply to a class then. And the fact that a follow up PR should add configuration options to allow/exclude paths/classes from being refactored by the rule. The main two cases this new Attribute is not necessary is:
The first case technically covers the cases of "a class that extends However that in mind, I think we still need "3 checks" in rector as checking for In contrast it seems more complex to recursively get all parent nodes and check those for With that in mind, @samsonasik is there a helper/resolver I can use to retrieve the current nodes parent class (as a node) - then we can pass that back into the method to check for attribute/__set? |
rules/Transform/Rector/Class_/AddAllowDynamicPropertiesAttributeRector.php
Outdated
Show resolved
Hide resolved
rules/Transform/Rector/Class_/AddAllowDynamicPropertiesAttributeRector.php
Outdated
Show resolved
Hide resolved
rules/Transform/Rector/Class_/AddAllowDynamicPropertiesAttributeRector.php
Show resolved
Hide resolved
Co-authored-by: Abdul Malik Ikhsan <samsonasik@gmail.com>
Co-authored-by: Abdul Malik Ikhsan <samsonasik@gmail.com>
After reading this message from internals and looking at this piece of code, I've found another case where the attribute is not needed: the method Do you think the current implementation handle it already? |
I believe i covered that under the "class, or ancestor, defines magic __set" method. While not technically an "ancestor" having that method via a trait falls on the same bucket. I believe the use of reflection to check for Edit: butterfinger closed the PR while writing this on my phone. |
Thank you @mallardduck , the RFC seems accepted, I guess it can be merged while open for new improvements ( configurable per-namespace or all, more fixture test, etc) |
This rector rule accounts for the proposed RFC to deprecate implicit dynamic properties in PHP 8.2. Per discussion on twitter this rule would add the new
#[AllowDynamicProperties]
attribute to every class - unless it already has this attribute applied.