-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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
Set properties to readonly if possible #5547
Conversation
c599c96
to
04d2ce3
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A little bit of context for the reviewer.
@@ -16,19 +16,15 @@ | |||
*/ | |||
final class ArrayResult implements Result | |||
{ | |||
private int $columnCount = 0; | |||
private int $num = 0; | |||
private readonly int $columnCount; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
readonly
properties must not have a default. The old default was moved to the constructor logic.
@@ -33,7 +33,7 @@ final class Connection implements ConnectionInterface | |||
* | |||
* @param resource $connection | |||
*/ | |||
public function __construct(private $connection) | |||
public function __construct(private readonly mixed $connection) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Only typed properties may be flagged readonly
and there is no native resource
type. These are known limitations of the PHP engine. This is why I'm adding a mixed
type here. Since the doc block is more precise, this change should not make a difference for static analysis.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't really like using the mixed
native type where the argument is of a more specific type but given that it's the limitation of the type system, it should be okay.
This is also implemented to a certain extent in #5546. But I don't mind merging this PR first and then rebasing the rest of the changes. |
We can also merge the automated changes first. This should make this PR easier to review because I've made some changes an automated tool would probably not make. |
04d2ce3
to
1481f22
Compare
Summary
This PR flags all properties as
readonly
when I found it safe to do so:private
or it or its class is flagged as@internal
.My main motivation is to make clear that we consider those properties as immutable. My expectation is that this makes the code easier to comprehend and analyze.