Skip to content

use readonly in args of constructor, in components/ #18075

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

Conversation

alexislefebvre
Copy link
Contributor

@alexislefebvre alexislefebvre commented Mar 17, 2023

This is a follow-up of #18049

I only updated some constructors from the components/ directory, to ensure that maintainers agree before modifying all the files.

Some files have been omitted because it looks like they must be initialized more than once:

protected Order $order,

private object $article,
private Key $key,

And this example is about reading annotations, so I think it must be kept as is:

@alexislefebvre alexislefebvre force-pushed the 6.2-use-readonly-in-args-of-construct branch from 26ec8d2 to 80f5cea Compare March 17, 2023 19:21
@OskarStark
Copy link
Contributor

CPP was introduced in PHP 8.0, which is what Symfony 6.2 needs as minimum requirement.
readonly properties were introduced in PHP 8.1.

So I would be strict to not merge readonly to the docs, unless we require at least Symfony 8.1 for a version.

@symfony/team-symfony-docs do you agree?

@alamirault @MrYamous we should add a rule to DOCtor then to ensure this 👍

@OskarStark OskarStark added Waiting feedback Waiting team decision Request for comments from Symfony Docs Team members labels Mar 17, 2023
@OskarStark
Copy link
Contributor

Oh I was wrong, in the first step we required PHP 8.0, but then switched to 8.1. My fault.

Another aspect is, that CPP reduced the lines of code, while readonly brings nearly no benefit while reading the docs.

I would vote 👎 on this topic

@alexislefebvre
Copy link
Contributor Author

alexislefebvre commented Mar 17, 2023

I see php: >=8.1 here: https://packagist.org/packages/symfony/symfony#v6.2.7

It comes from here: https://github.com/symfony/symfony/blob/v6.2.7/composer.json#L36

Do some components require php: >=8.0?

/dependency-injection requires php: >=8.1 too: https://packagist.org/packages/symfony/dependency-injection#v6.2.7

@alexislefebvre
Copy link
Contributor Author

alexislefebvre commented Mar 17, 2023

Oh I was wrong, in the first step we required PHP 8.0, but then switched to 8.1. My fault.

Another aspect is, that CPP reduced the lines of code, while readonly brings nearly no benefit while reading the docs.

I would vote -1 on this topic

Thanks for your input, this is interesting.

readonly prevent making a mistake like:

        public function __construct(
            private readonly LoggerInterface $logger,
        ) {
        }

        public function doSomething() {
            $this->logger = 'a'; // this is not valid
        }

We can consider that it's an edge case to write this typo and keep the examples as is.

What other people think about this?

@MrYamous
Copy link
Contributor

In context of documentation I feel like using readonly does not add much value but can lead to additionnal maintenance effort as even if it is mostly appropriate to use it (from code pov), sometimes not, and we will have to be careful about that to not have invalid examples

@alexislefebvre alexislefebvre deleted the 6.2-use-readonly-in-args-of-construct branch March 18, 2023 03:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Status: Needs Review Waiting feedback Waiting team decision Request for comments from Symfony Docs Team members
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants