Open
Description
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:
- Create a file called
test.php
with the code sample above... - Run
phpcs test.php ...
- 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.