Skip to content

Does not work with readonly classes introduced in PHP 8.2Β #3727

@siketyan

Description

@siketyan

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:

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions