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

Set properties to readonly if possible #5547

Merged
merged 1 commit into from
Jul 28, 2022

Conversation

derrabus
Copy link
Member

Q A
Type improvement
Fixed issues N/A

Summary

This PR flags all properties as readonly when I found it safe to do so:

  • The property is written once in the constructor and only read afterwards and
  • the property is 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.

Copy link
Member Author

@derrabus derrabus left a 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;
Copy link
Member Author

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)
Copy link
Member Author

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.

Copy link
Member

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.

@morozov
Copy link
Member

morozov commented Jul 28, 2022

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.

@derrabus
Copy link
Member Author

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.

@derrabus derrabus force-pushed the improvement/readonly branch from 04d2ce3 to 1481f22 Compare July 28, 2022 15:06
@derrabus derrabus merged commit 1c43874 into doctrine:4.0.x Jul 28, 2022
@derrabus derrabus deleted the improvement/readonly branch July 28, 2022 15:45
@morozov morozov added this to the 4.0.0 milestone Sep 29, 2022
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Sep 30, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants