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

ClassAttributesSeparationFixer - Introduce only_if_meta spacing option #5704

Merged

Conversation

paulbalandan
Copy link
Contributor

@paulbalandan paulbalandan commented May 8, 2021

Closes #4014 .

This PR introduces a new spacing option only_if_meta to class_attributes_separation fixer. The behavior of this option is technically the same with none except that when there is a T_DOC_COMMENT or T_ATTRIBUTE (or both) above the class element the option will now behave as one.

Examples:

['elements' => ['const' => 'only_if_meta']]

// Before
class Sample
{
    /** @var int */
    const FOO = 1;
    /** @var int */
    const BAR = 2;
    #[Foo\Baz]
    const BAZ = 3;
    const OTHER = 4;
}

// After
class Sample
{
    /** @var int */
    const FOO = 1;

    /** @var int */
    const BAR = 2;

    #[Foo\Baz]
    const BAZ = 3;
    const OTHER = 4;
}

['elements' => ['property' => 'only_if_meta']]

// Before
class Sample
{
    public $a;
    public $b;
    /** @var string */
    public $c;
}

// After
class Sample
{
    public $a;
    public $b;

    /** @var string */
    public $c;
}

@paulbalandan paulbalandan force-pushed the class-attr-sep-hybrid-option branch 5 times, most recently from 666e2b9 to 344a9c5 Compare May 17, 2021 18:10
Copy link
Member

@julienfalque julienfalque left a comment

Choose a reason for hiding this comment

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

Nice 👍

@coveralls
Copy link

coveralls commented May 22, 2021

Coverage Status

Coverage increased (+0.01%) to 92.194% when pulling 79bfe09 on paulbalandan:class-attr-sep-hybrid-option into e5ebc7f on FriendsOfPHP:master.

@paulbalandan paulbalandan changed the title ClassAttributesSeparationFixer - Introduce hybrid spacing option ClassAttributesSeparationFixer - Introduce one_if_phpdoc spacing option May 24, 2021
@paulbalandan paulbalandan force-pushed the class-attr-sep-hybrid-option branch 3 times, most recently from cc8c114 to 39f4f71 Compare May 26, 2021 06:51
@julienfalque julienfalque added this to the 3.1.0 milestone May 26, 2021
@SpacePossum SpacePossum self-requested a review May 26, 2021 10:30
@julienfalque julienfalque added the RTM Ready To Merge label May 26, 2021
@ruudk
Copy link
Contributor

ruudk commented Jun 18, 2021

🙌 Amazing! Thanks for tackling this @paulbalandan

I tried this in the past but couldn't get it to work.

Instead of calling the option one_if_phpdoc shouldn't we also think about PHP 8 Attributes? How do we want to support that in the future? Maybe the option should be called one_if_phpdoc_or_attribute and support Attributes as well?

Because properties can be annotated with only an attribute, with an attribute and phpdoc, only a phpdoc, or nothing at all.

@julienfalque
Copy link
Member

@ruudk Good point! I agree PHP 8 attributes should be supported. Maybe a name like only_if_meta would be more appropriate? Anyway, @paulbalandan, could you please make the changes for that?

@julienfalque julienfalque removed the RTM Ready To Merge label Jun 18, 2021
@paulbalandan
Copy link
Contributor Author

@julienfalque sure, I'll look into that. Just to confirm, meta means phpdocs and attributes only, right?

@julienfalque
Copy link
Member

Thanks! Yep, meta means "PHPDocs or attritbute".

@ruudk
Copy link
Contributor

ruudk commented Jun 18, 2021

Does it matter if it's a phpdoc or regular comment? I think as a user you expect the same behavior:

    const FOO = 1;
    
    // Some comment to explain more
    const BAR = 2;
    const BAZ = 3;

?

@julienfalque
Copy link
Member

IMO we can start with PHPDocs and attributes only and see if the option needs adjustements afterwards.

@paulbalandan paulbalandan force-pushed the class-attr-sep-hybrid-option branch 2 times, most recently from 7f6888d to f8d408c Compare June 18, 2021 17:17
@paulbalandan paulbalandan changed the title ClassAttributesSeparationFixer - Introduce one_if_phpdoc spacing option ClassAttributesSeparationFixer - Introduce only_if_meta spacing option Jun 18, 2021
@ruudk
Copy link
Contributor

ruudk commented Jun 18, 2021

I was looking at the example in the OP:

// Before
class Sample
{
    /** @var int */
    const FOO = 1;
    /** @var int */
    const BAR = 2;
    #[Foo\Baz]
    const BAZ = 3;
    const OTHER = 4;
}

// After
class Sample
{
    /** @var int */
    const FOO = 1;

    /** @var int */
    const BAR = 2;

    #[Foo\Baz]
    const BAZ = 3;
    const OTHER = 4;
}

Shouldn't there also be a newline between BAZ and OTHER because BAZ has meta? That's how I would manually format this.

Like this:

// Before
class Sample
{
    /** @var int */
    const FOO = 1;
    /** @var int */
    const BAR = 2;
    #[Foo\Baz]
    const BAZ = 3;
    const OTHER = 4;
    const OTHER2 = 5;
}

// After
class Sample
{
    /** @var int */
    const FOO = 1;

    /** @var int */
    const BAR = 2;

    #[Foo\Baz]
    const BAZ = 3;

    const OTHER = 4;
    const OTHER2 = 5;
}

@paulbalandan
Copy link
Contributor Author

The rule adds a new line if the element itself has a meta above it. It will not be affected if the element above it has the meta instead.

@SpacePossum
Copy link
Contributor

closes #5800 as well

@SpacePossum
Copy link
Contributor

I like this PR a lot and know it is a feature request for a lot for some time 👍

Left a minor and kicked the CI, so lets see on that.
Another thing I was wondering is #5704 (comment) , I would like the style with each PHPDoc + element being separated by itself, so basically agreeing with @ruudk . What do others think and what would be the effort to adjust?

@paulbalandan paulbalandan force-pushed the class-attr-sep-hybrid-option branch 2 times, most recently from f176aad to 0f16c73 Compare July 30, 2021 13:53
@keradus
Copy link
Member

keradus commented Aug 2, 2021

Thank you @paulbalandan.

@keradus keradus merged commit 4b973ad into PHP-CS-Fixer:master Aug 2, 2021
@ruudk
Copy link
Contributor

ruudk commented Aug 2, 2021

Thank you @paulbalandan for this amazing contribution ❤️ 🙌

@paulbalandan paulbalandan deleted the class-attr-sep-hybrid-option branch August 2, 2021 20:13
keradus added a commit that referenced this pull request Aug 29, 2021
…SpacePossum, keradus)

This PR was squashed before being merged into the 3.0 branch.

Discussion
----------

ClassAttributesSeparationFixer - fixes & enhancements

closes #5852
closes #5854
closes #5843 (including #5843 (comment))

fixes #5704 (comment) (cc `@ruudk` for option `none`)

replaces #5869
replaces #5868

I haven't found BC breaks, however there is a minor change in how comments that are not related at all to a class element are handled. This is of no concern as people shouldn't just drop comments all over the place and expect these to end up somewhere.

Additionally fixes a bunch of issues with comments, include cases where the fixer had to be run multiple times before done with fixing.

Have to check how `only_if_meta` comes into play on `master` as I suspect this act near the same as `none` now.

Commits
-------

ac1a41d ClassAttributesSeparationFixer - fixes & enhancements
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.

None yet

7 participants