We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
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
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!
Indeed, it catches all the little things I did not consider. Every single day :)
The text was updated successfully, but these errors were encountered:
@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
Readonly property Repository::$data is assigned outside of the constructor.
Syntax error, unexpected T_ARRAY, expecting T_VARIABLE on line 7
Sorry, something went wrong.
phpstan/phpstan-src@d332bf5
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.
No branches or pull requests
Feature request
The rfc https://wiki.php.net/rfc/readonly_properties_v2 mentions:
I especially tested the following code:
Which leads to:
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 :)
The text was updated successfully, but these errors were encountered: