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

PSR2.Classes.PropertyDeclaration.ReadonlyBeforeVisibility doesn't support constructor property promotion #444

Open
3 tasks done
earthiverse opened this issue Apr 12, 2024 · 1 comment

Comments

@earthiverse
Copy link

earthiverse commented Apr 12, 2024

Describe the bug

PSR2.Classes.PropertyDeclaration.ReadonlyBeforeVisibility doesn't apply when constructor property promotion is used

Code sample

<?php

class Test
{
    private readonly int $three;
    readonly private int $four; // <--  Correctly errors (PSR2.Classes.PropertyDeclaration.ReadonlyBeforeVisibility)
    
    public function __construct(
        private readonly int $one,
        readonly private int $two // <-- Does not error
    )
    {
    }
}

To reproduce

Steps to reproduce the behavior:

  1. Create a file called test.php with the code sample above...
  2. Run phpcs test.php ...
  3. See error message displayed
FILE: /Users/earthiverse/Code/Test.php
-------------------------------------------------------------------------------------------------------------------------------------------------
FOUND 1 ERROR AFFECTING 1 LINE
-------------------------------------------------------------------------------------------------------------------------------------------------
 8 | ERROR | [x] The readonly declaration must come after the visibility declaration (PSR2.Classes.PropertyDeclaration.ReadonlyBeforeVisibility)
-------------------------------------------------------------------------------------------------------------------------------------------------
PHPCBF CAN FIX THE 1 MARKED SNIFF VIOLATIONS AUTOMATICALLY
-------------------------------------------------------------------------------------------------------------------------------------------------

Time: 37ms; Memory: 8MB

Expected behavior

The line with readonly private int $two should also have an error.

Versions

Operating System MacOS 14.4.1
PHP version 8.3.4
PHP_CodeSniffer version 3.9.1
Standard PSR2, PSR12
Install type Composer

Please confirm:

  • I have searched the issue list and am not opening a duplicate issue.
  • I confirm that this bug is a bug in PHP_CodeSniffer and not in one of the external standards.
  • I have verified the issue still exists in the master branch of PHP_CodeSniffer.
@jrfnl
Copy link
Member

jrfnl commented Apr 12, 2024

@earthiverse Thank you for reporting this. I've changed the type from "bug" to "feature request" (enhancement) as the PSR2.Classes.PropertyDeclaration sniff does not support constructor property promotion at all - and neither PSR2 nor PSR12 would expect it to, as both predate constructor property promotion, let alone readonly properties.

As things are, what with the sniff being based on the AbstractVariableSniff, adding support for examining properties declared via constructor property promotion to the sniff would likely warrant a complete rewrite of the sniff.

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