Skip to content

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

Open
@earthiverse

Description

@earthiverse

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.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions