PHPCSStandards / PHP_CodeSniffer

PHP_CodeSniffer tokenizes PHP files and detects violations of a defined set of coding standards.
BSD 3-Clause "New" or "Revised" License
978 stars 58 forks source link

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

Open earthiverse opened 7 months ago

earthiverse commented 7 months ago

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:

- [X] I have searched the issue list and am not opening a duplicate issue.
- [X] I confirm that this bug is a bug in PHP_CodeSniffer and not in one of the external standards.
- [X] I have verified the issue still exists in the `master` branch of PHP_CodeSniffer.
jrfnl commented 7 months ago

@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.