PHP_CodeSniffer: Does not work with readonly classes introduced in PHP 8.2
May be closed by ~#3686~ #3728
Describe the bug In PHP 8.2, “readonly class” feature was introduced. Using readonly classes, we can shorten readonly property declarations as follows:
class Foo {
public function __construct(
private readonly string $foo,
private readonly int $bar,
) {
}
}
equals to:
readonly class Foo {
public function __construct(
private string $foo,
private string $bar,
) {
}
}
However, the latest version of phpcs v3.7.1 does not work on the classes which are marked as “readonly”. (The phpcs error output is in “To reproduce” section)
Code sample
readonly class Foo {
public function __construct(
private string $foo,
private string $bar,
) {
}
}
Custom ruleset N/A
To reproduce Steps to reproduce the behavior:
- Run phpcs with the default ruleset, with the code sample above.
FILE: /project/Foo.php
----------------------------------------------------------------------
FOUND 0 ERRORS AND 1 WARNING AFFECTING 1 LINE
----------------------------------------------------------------------
1 | WARNING | A file should declare new symbols (classes, functions,
| | constants, etc.) and cause no other side effects, or
| | it should execute logic with side effects, but should
| | not do both. The first symbol is defined on line 13
| | and the first side effect is on line 13.
| | (PSR1.Files.SideEffects.FoundWithSymbols)
----------------------------------------------------------------------
Expected behavior A clear and concise description of what you expected to happen.
Versions (please complete the following information):
- OS: macOS Ventura 13.0.1 (Apple M1 Max; aarch64-apple-darwin)
- PHP: 8.2.0
- PHPCS: 3.7.1
- Standard: PSR12
Additional context N/A
About this issue
- Original URL
- State: closed
- Created 2 years ago
- Reactions: 31
- Comments: 15 (7 by maintainers)
Commits related to this issue
- Fix final readonly class There is a bug when dealing with readonly class. See squizlabs/PHP_CodeSniffer#3727 This commit will temporary exclude PSR1.Files.SideEffects.FoundWithSymbols — committed to asispts/ptscs by asispts a year ago
- Serve *a* `/etc/passwd` if requested (#112) This is to satisfy some scanners looking for one. I mean, it's Easter, after all. The `FourOhFourButFound` class could be marked as `readonly` but the c... — committed to spaze/michalspacek.cz by spaze a year ago
@gsherwood Is this something that you could tag soon? It’d be nice to stop disabling the busted rule
@jrfnl @gsherwood any ETA on getting this out?
I’ve merged the fix. Thanks for the bug report.
PHP 8.2 beta came out July 21, 2022. The first RC came out on Sep 1. The official stable release was on Dec 8, 2022. And here we are, April 30, 2023, and we still have to choose between dropping PHP_CodeSniffer and actually making use of the readonly classes feature.
Thanks for reporting this.
PR #3686 would not fix this, though I have a number of follow-up commits waiting on #3686 to be merged to add support for
readonly
classes to various sniffs. However, looks like I missed thePSR1.Files.SideEffects
sniff and as the fix for that sniff does not depend on #3686, I have pulled PR #3728 now to fix this.FYI: the fix for this issue is included in today’s PHP_CodeSniffer 3.8.0 release.
As per #3932, development on PHP_CodeSniffer will continue in the PHPCSStandards/PHP_CodeSniffer repository. If you want to stay informed, you may want to start “watching” that repo (or watching releases from that repo).
@iquito We’re working on getting it unstuck, but comments like the one I responded to are not helpful and highly demotivating.
@still-dreaming-1 Have you tried contributing ?
Just ran into this myself; any chance for a new release of PHPCS? Thank you for considering!
@jrfnl Would contributing help though? There are so many pull requests already waiting (you seem to have already fixed this issue for quite some time, for example, thanks for that!) and no avenue to help out or indication what would help. I would donate some money for the project or for a maintainer (as I do for other open source projects), but there is no information about that either. It does seem that this project is somehow stuck.