Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Full Support of PHP 8.1 readonly property #6773

Closed
simon-schubert opened this issue Mar 9, 2022 · 2 comments
Closed

Full Support of PHP 8.1 readonly property #6773

simon-schubert opened this issue Mar 9, 2022 · 2 comments

Comments

@simon-schubert
Copy link

Feature request

The rfc https://wiki.php.net/rfc/readonly_properties_v2 mentions:

Modifications are not necessarily plain assignments, all of the following will also result in an Error exception:

class Test {
    public function __construct(
        public readonly int $i = 0,
        public readonly [array](http://www.php.net/array) $ary = [],
    ) {}
}
 
$test = new Test;
$test->i += 1;
$test->i++;
++$test->i;
$test->ary[] = 1;
$test->ary[0][] = 1;
$ref =& $test->i;
$test->i =& $ref;
byRef($test->i);
foreach ($test as &$prop);

I especially tested the following code:

<?php
final class Repository 
{ 
    /**
      * @param array<string, string> $data
      */
    public function __construct(private readonly array $data)
    {
    }
  
    public function remove(string $key): void
    {
     	unset($this->data[$key]);
    }
}

$repository = new Repository(['lorem' => 'ipsum', 'dolor' => 'sit']);
$repository->remove('lorem');

Which leads to:

No errors! 

see: https://phpstan.org/r/8e14a7a8-7663-49af-9885-21f44543cdfb

It would be amazing, if phpstan would support this!

Did PHPStan help you today? Did it make you happy in any way?

Indeed, it catches all the little things I did not consider. Every single day :)

@phpstan-bot
Copy link
Contributor

@simon-schubert After the latest push in 1.11.x, PHPStan now reports different result with your code snippet:

@@ @@
-PHP 8.1
+PHP 8.1 (1 error)
 ==========
 
-No errors
+13: Readonly property Repository::$data is assigned outside of the constructor.
 
 PHP 7.1 – 8.0 (1 error)
 ==========
 
  7: Syntax error, unexpected T_ARRAY, expecting T_VARIABLE on line 7
Full report

PHP 8.1 (1 error)

Line Error
13 Readonly property Repository::$data is assigned outside of the constructor.

PHP 7.1 – 8.0 (1 error)

Line Error
7 Syntax error, unexpected T_ARRAY, expecting T_VARIABLE on line 7

Copy link

github-actions bot commented Mar 8, 2024

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Mar 8, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants