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

PSR12.Properties.ConstantVisibility should automatically detect the PHP version #393

Open
1 task done
razvanphp opened this issue Mar 14, 2024 · 4 comments
Open
1 task done

Comments

@razvanphp
Copy link

razvanphp commented Mar 14, 2024

Is your feature request related to a problem?

When running PSR12 against an (older) project, we see this warning message for projects that actually support PHP versions lower than 7.1.

FILE: /Volumes/Code/yii2/framework/grid/GridView.php
------------------------------------------------------------------------------------------------------------------------------------------------------
FOUND 0 ERRORS AND 3 WARNINGS AFFECTING 3 LINES
------------------------------------------------------------------------------------------------------------------------------------------------------
 52 | WARNING | Visibility must be declared on all constants if your project supports PHP 7.1 or later (PSR12.Properties.ConstantVisibility.NotFound)

In my particular case, this is defined in composer.json:

    "require": {
        "php": ">=5.4.0",

The php version running phpcs is not relevant, as it might run just in CI.

Describe the solution you'd like

It would be great to skip this check if the minimum version is below PHP 7.1, ideally checking this in composer.json. Also, I think this is an easy fix for phpcbf, but it's not yet implemented, it should be public as this is the default visibility if it is omitted.

Additional context (optional)

Can I please get some guidance of how this should be implemented? Saw in another issue this, but not sure it's reliable enough for this use-case:

if ($this->phpVersion === null) {
$this->phpVersion = Config::getConfigData('php_version');
if ($this->phpVersion === null) {
$this->phpVersion = PHP_VERSION_ID;
}
}
if ($this->phpVersion < 70000) {
$this->aspTags = (boolean) ini_get('asp_tags');
}

  • I intend to create a pull request to implement this feature.
@jrfnl
Copy link
Member

jrfnl commented Mar 14, 2024

@razvanphp Thanks for opening this issue.

It would be great to skip this check if the minimum version is below PHP 7.1, ideally checking this in composer.json.

PHPCS can not and should not rely on a composer.json file being available. PHPCS can be installed via PHIVE, git clone, stand-alone PHAR, Composer etc, so any solution which presumes the existence of a composer.json file in a project (and presumes a project-based install), will not be accepted.

Also, I think this is an easy fix for phpcbf, but it's not yet implemented, it should be public as this is the default visibility if it is omitted.

As spelled out in the CONTRIBUTING guide, PHPCS does not allow risky fixers for its native sniffs. The appropriate visibility is something which should be deliberately determined by a developer. This is not something a tool can do and making automated BC-compatible fixes just to fix the issues instead of making deliberate choices about the visibility is a risky fix, so any PR for this will not be accepted.

I'm pretty sure there are sniffs available in external standards which can make that fix for you, but it will not be made by the PHPCS native sniffs.

Can I please get some guidance of how this should be implemented ?

The code snippet shown, works based on a configured php_version and defaults to the PHP version used to run PHPCS.
The PHPCS version to run PHPCS is - in most cases - not the same as the minimum supported PHP version, so defaulting to that PHP version would not be appropriate.

@razvanphp
Copy link
Author

ok, thanks for the detailed explanation. So no other options? I can't see a viable way to treat this case otherwise. Maybe the sniff could receive a parameter as the minimum PHP version?

@jrfnl
Copy link
Member

jrfnl commented Mar 14, 2024

So no other options?

That code snippet with php_version could be adjusted to not default to the PHP version on which PHPCS is being run. I can see that as a feasible solution, though I need to think about it a little.

Maybe the sniff could receive a parameter as the minimum PHP version?

A parameter would require a custom ruleset, in which case people can just as well use:

<rule ref="PSR12">
    <exclude name="PSR12.Properties.ConstantVisibility"/>
</rule>

@razvanphp
Copy link
Author

OK, for now I will just exclude it in the project phpcs.xml, but if you think about a clever solution, I'm all in to try it out. Thank you!

@jrfnl jrfnl changed the title PSR12.Properties.ConstantVisibility should automatically detect the PHP version + add phpcbf fix PSR12.Properties.ConstantVisibility should automatically detect the PHP version ~~+ add phpcbf fix~~ Mar 15, 2024
@jrfnl jrfnl changed the title PSR12.Properties.ConstantVisibility should automatically detect the PHP version ~~+ add phpcbf fix~~ PSR12.Properties.ConstantVisibility should automatically detect the PHP version Mar 15, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants