Skip to content

[deprecation] Deprecate PropertyHookRector, as property hooks are a matter of preference - #8331

Merged
TomasVotruba merged 1 commit into
mainfrom
deprecate-property-hook-rector
Aug 9, 2026
Merged

[deprecation] Deprecate PropertyHookRector, as property hooks are a matter of preference#8331
TomasVotruba merged 1 commit into
mainfrom
deprecate-property-hook-rector

Conversation

@TomasVotruba

Copy link
Copy Markdown
Member

Property hooks are a code style preference, not an upgrade path.

PropertyHookRector was 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 wrap trigger_deprecation() on a public property - a deliberate BC shim, not a getter/setter conversion.

This deprecates the rule following the same shape as JsonThrowOnErrorRector:

  • rule implements DeprecatedInterface, so DeprecatedRulesReporter warns when it is registered or skipped
  • refactor() throws, the transformation logic is removed
  • tests, fixtures and the now-unused PropertyHookFactory / SetterAndGetterFinder are removed
  • the commented-out entry is dropped from config/set/php84.php

The 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.

@TomasVotruba
TomasVotruba enabled auto-merge (squash) August 9, 2026 22:21
@TomasVotruba
TomasVotruba merged commit bd89a0c into main Aug 9, 2026
64 checks passed
@TomasVotruba
TomasVotruba deleted the deprecate-property-hook-rector branch August 9, 2026 22:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

1 participant