rectorphp / rector

Instant Upgrades and Automated Refactoring of any PHP 5.3+ code
https://getrector.com
MIT License
8.32k stars 670 forks source link

turn private properties into private constants #8601

Closed staabm closed 1 month ago

staabm commented 1 month ago

Feature Request

from time to time I stuble over code which should have used a native constant property, but uses a static property instead. I think some people mix up static with constant.

could rector detect private properties which are only used in a readonly fashion and turn them into constants?

Example

class A {
-    private static array $availableSizes = [
+    private const AVAILABLE_SIZES = [
        'sm' => 'small',
        'me' => 'medium',
        'la' => 'large',
    ];

   public function getFoo() {
-     return self::$availableSizes;
+     return self::AVAILABLE_SIZES
   }
}
TomasVotruba commented 1 month ago

Agreed :+1:

We actually had such a rule but it was impossible to detect if property is changed or not. Due to references, magic and more magic :) we removed it to avoid WTFs and false positives.