rectorphp / swiss-knife

Swiss knife in pocket of every upgrade architect!
https://getrector.com
MIT License
68 stars 8 forks source link

The privatize-constants command fails with fatal error while processing interfaces #32

Open OleksandrProtsiuk opened 1 month ago

OleksandrProtsiuk commented 1 month ago

The issue is related to the interfaces that contain constants without or with the public access modifier.

E.G: The next source code:

<?php

namespace Project\Bundle\CoolBundle\Contract;

interface MyCoolInterface
{
    public const COOL_CONST = 'cool_value';
}

will produce the next error

PHP Fatal error:  Access type for interface constant Project\Bundle\CoolBundle\Contract\MyCoolInterface::COOL_CONST must be public

As I can see, after the error is thrown the interface code is changed

<?php

namespace Project\Bundle\CoolBundle\Contract;

interface MyCoolInterface
{
    private const COOL_CONST = 'cool_value';
}

Possible solutions:

  1. Exclude interfaces of the "force setting" of the private modifier
  2. To refactor the class https://github.com/rectorphp/swiss-knife/blob/main/src/ValueObject/ClassConstMatch.php to avoid the thrown fatal error

Please, let me know if you would like to get more details. Thanks)

TomasVotruba commented 1 month ago

Hi, thanks for sharing the report.

It seems your classes are autoloaded by PHPStan and not run statically as they should be. Do you use PHPStan?

If not, we'll need to exclude the interfaces probably. If you do, try to detect what in your phpstan.neon loads code dynamically (e.g. Doctrine or Symfony extension)