Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
OleksandrProtsiuk opened this issue Jun 7, 2024 · 1 comment

Comments

@OleksandrProtsiuk
Copy link

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
Copy link
Member

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)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants