rectorphp / rector

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

[CodeQuality] Add new rule to change "static::CONSTANT" to "self::CONSTANT" #8818

Closed TomasVotruba closed 1 week ago

TomasVotruba commented 1 week ago

In class that has constant in the same class, has no parent class, follwoing should be changed to self as no other constant is available.

class SomeClass
{
    private const NAME = 'cool rule';

  public function run()
  {
-   return static::NAME;
+   return self::NAME;
   }   
}

It also fixes PHPStan reports :+1:

samsonasik commented 1 week ago

It seems only works on private constant, on protected/public, it still needed, see

https://3v4l.org/rPYm0 vs https://3v4l.org/80571

samsonasik commented 1 week ago

Actually, there is already rule ConvertStaticPrivateConstantToSelfRector for that, see https://getrector.com/demo/5c3ff3fc-8b61-45d1-acab-2bb3f974f3af

samsonasik commented 1 week ago

I added ability to change into self on final class:

TomasVotruba commented 1 week ago

Thank you 🙏