[deprecation] Deprecate PropertyHookRector, as property hooks are a matter of preference - #8331
Merged
Merged
Conversation
…atter of preference
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Property hooks are a code style preference, not an upgrade path.
PropertyHookRectorwas added as "optional" and never registered in any set. There is no correctness or compatibility reason to convert getters/setters into hooks: the old code keeps working on PHP 8.4 unchanged. What the rule does produce is a property that mixes state and behavior in one declaration, which is harder to read than the two explicit methods it replaces.Real-world usage confirms hooks are not an upgrade target.
symfony/symfony(which requires PHP >= 8.4.1) uses property hooks in only 3 production files, and in all 3 they wraptrigger_deprecation()on a public property - a deliberate BC shim, not a getter/setter conversion.This deprecates the rule following the same shape as
JsonThrowOnErrorRector:DeprecatedInterface, soDeprecatedRulesReporterwarns when it is registered or skippedrefactor()throws, the transformation logic is removedPropertyHookFactory/SetterAndGetterFinderare removedconfig/set/php84.phpThe change the rule used to make:
final class Product { - private string $name; - - public function getName(): string - { - return $this->name; - } - - public function setName(string $name): void - { - $this->name = ucfirst($name); - } + public string $name + { + get => $this->name; + set($value) => $this->name = ucfirst($value); + } }Both versions behave the same. The first one is easier to read, easier to debug and easier to type-hint against, so there is no reason to automate the move.