Skip to content

[Console] [PHP 8.0] Add property promotion to commands #41381

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

Closed
wants to merge 1 commit into from

Conversation

TomasVotruba
Copy link
Contributor

Q A
Branch? 6.0
Bug fix? no
New feature? no
Deprecations? no
License MIT

Follow up to #41282


Hi, congrats on PHP 8 version as minimum!

Now we can use new features like promoted properties.
They're not breaking BC, as they unwrap to its former property + param + assign syntax.

This is just a demo for single class.
Would you be interested in series of PRs to add promoted properies with PR per component?

@TomasVotruba TomasVotruba requested a review from chalasr as a code owner May 22, 2021 11:53
@carsonbot carsonbot added this to the 6.0 milestone May 22, 2021
@carsonbot carsonbot changed the title [PHP 8.0][Console] Add property promotion to commands [Console] [PHP 8.0] Add property promotion to commands May 22, 2021
@TomasVotruba TomasVotruba marked this pull request as draft May 22, 2021 11:53
@Nyholm
Copy link
Member

Nyholm commented May 22, 2021

Thank you for this PR.

What would the benefit of this change be?

@nicolas-grekas
Copy link
Member

I fear this will create needless merge conflicts in the future. I think we should wait a few more years before doing so. Like we did for the short array syntax.

@derrabus
Copy link
Member

As much as I'd love to use CPP for the whole codebase, we will still need to merge changes from lower branches. This change is a source of potential merge conflicts, which is why I'm leaning towards rejecting it.

@stof
Copy link
Member

stof commented May 22, 2021

I agree with @nicolas-grekas and @derrabus here. This change brings no value to users of the framework (as using CPP versus writing the unsugared code is purely an implementation detail of the class and does not impact its usage) while having a maintenance cost as long as we cannot apply it on all maintained branches.
So this is a -1 from me

@chalasr
Copy link
Member

chalasr commented May 22, 2021

As explained, having this syntactic sugar only on some branches is likely to trigger headaches when merging branches up.
I'm going to close this PR and use it as reference for future PRs, until we can use it on all active branches (i.e. once everything is PHP 8+).
Thanks for raising the topic, Tomas.

@chalasr chalasr closed this May 22, 2021
@@ -22,18 +22,12 @@
*/
final class LazyCommand extends Command
{
private $command;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this change is even broken, because you turned $command into a typed property with the Closure type, which is not the current expectation of the code (the type of this property is \Closure|Command, see getCommand())

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, will fix that in Rector 👍

@TomasVotruba
Copy link
Contributor Author

TomasVotruba commented May 22, 2021

Thanks for feedback.

I fear this will create needless merge conflicts in the future. I think we should wait a few more years before doing so. Like we did for the short array syntax.

The merge conflicts can be removed as this process is automated. Basically you con contribute old ctor approach a new properties in 2 different PRs and they both will be normalized to promoted ones.

What would the benefit of this change be?

Promoted properties themselves narrow potential bugs to 1/3, as definition is written only once. The rest of benefits can be found in the RFC.

Another benefit is consistency with contributed code. As minimum PHP versoin suggest PHP 8 code is accepted. In some period of time it will be confusing what language version should be respected where.


Let me know when the right time comes :) this a matter of one day to setup with @rectorphp.

@TomasVotruba TomasVotruba deleted the tv-php8-promoted branch May 22, 2021 15:52
@Nyholm
Copy link
Member

Nyholm commented May 22, 2021

The "merge conflicts" referred to here is when we fix a bug in branch 4.4 and have to merge it up to 5.3, 5.4 and 6.0.

Promoted properties themselves narrow potential bugs to 1/3, as definition is written only once

That problem is already solved using static code checkers.

Another benefit is consistency with contributed code.

That is an argument against this PR. We dont want different styles for contributing to 5.4 compared to 6.0. That is what @nicolas-grekas loosely referred to.

Let me know when the right time comes

When we eventually switch to promoted properties, we will definitely automate it.


This was a very short but important discussion. Thank you again Tomas for raising it.

@stof
Copy link
Member

stof commented May 22, 2021

Thus, migrating to CPP involves migrating to typed properties first (as removing the typehints for constructor arguments is not an option). And that change is a lot more risky (as can be seen in this PR where you only migrated 2 properties to CPP, with one of them being broken)

If you update rector to only apply the CPP migration to properties that are already typed (which it should do, otherwise the migration is unsafe), it will not migrate anything in Symfony.

@derrabus
Copy link
Member

The merge conflicts can be removed as this process is automated.

They can't because we will create a codebase that significantly differs from the still maintained 4.4 and 5.2/5.3/5.4 branches. 5.4 will live on for years because it's LTS. Merging from 5.4 to 6.0 and up will become harder this way. CPP is purely syntactic sugar and that's imho not enough to accept such a maintenance burden.

this a matter of one day to setup with @rectorphp.

This is certainly a task that can be automated. I've done this in one of my projects with PhpStorm and I really liked the result.

However, we need to be careful on Symfony's codebase. Just blindly converting all constructors to CPP will also sneak in typed properties that this codebase barely uses at the moment. Unlike CPP alone, typed properties actually do have side effects, so we need to be careful when introducing them to an existing codebase.

Either way: typed properties have to be the first step before we can think of CPP.

We dont want different styles for contributing to 5.4 compared to 6.0.

I would disagree here. I would allow CPP in contributions to a 6.x branch if…

  • … CPP is used in an entirely new class
  • … the constructor of an existing class is changed significantly during a change

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants