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

BlankLineBeforeStatementFixer - can now add blank lines before doc-comments #4571

Merged

Conversation

addiks
Copy link
Contributor

@addiks addiks commented Oct 7, 2019

This PR adds the "doccomment" token to the blank-line-before-statement-fixer.

Now the php-cs-fixer can automatically turn this:

/** @var int $foo */
$foo = 123;
/** @var int $bar */
$bar = 456;
/** @var int $baz */
$baz = 789;

... into this:

/** @var int $foo */
$foo = 123;

/** @var int $bar */
$bar = 456;

/** @var int $baz */
$baz = 789;

@addiks
Copy link
Contributor Author

addiks commented Oct 7, 2019

Is there currently some issue with the CI?

λλλ phive packages

Phive 0.13.0 - Copyright (C) 2015-2019 by Arne Blankerts, Sebastian Heuer and Contributors

Fetching repository list

Downloading https://phar.io/data/repositories.xml

[ERROR]    Failed to download load https://api.github.com/repos/maglnet/ComposerRequireChecker/releases: HTTP Code 403 

The command "./dev-tools/install.sh" failed and exited with 4 during .

Your build has been stopped.

@SpacePossum
Copy link
Contributor

Hi and thanks for the PR!

Looking at the code I'm afraid the approach of using the fixer might not work because of how the tool /software was designed. I really like to have the functionality in, however we need to get it in in a different way.
The BlankLineBeforeStatementFixer has been designed about a very basic principle; for the given token type make sure there is a blank line before/above it. This is good and all and I like to keep it that way. However this simple principle makes it not fitting for when additional rules should be applied.

In this case, we like to have empty like above a PHPDoc, however there are some exceptions.
For example:

final class CombineConsecutiveIssetsFixer extends AbstractFixer
{
    /**
     * {@inheritdoc}
     */
    public function getDefinition()
    {

In this case we don't want to have the empty line (there are a few more exceptions).

So in order to get the fixing we want, I think it would be best to create a new one and not re-utilize the current one. WDYT?

@addiks
Copy link
Contributor Author

addiks commented Oct 8, 2019

I just tested it for exactly this use-case. It did not add a blank line on the first doc-comment of the class.

Before:

final class CombineConsecutiveIssetsFixer extends AbstractFixer{
    /**
     * {@inheritdoc}
     */
    public function getDefinition()
    {
    }
    /** @var def */
    protected $asd;
    /** @var abc */
    protected $qwe;
}

After:

final class CombineConsecutiveIssetsFixer extends AbstractFixer{
    /**
     * {@inheritdoc}
     */
    public function getDefinition()
    {
    }

    /** @var def */
    protected $asd;

    /** @var abc */
    protected $qwe;
}

The same behavior appears when the opening curly-brackets are on it's own line.
Is this not exactly the behavior that you want? Under which circumstances does it behave wrong?

@SpacePossum
Copy link
Contributor

Looking into this a bit more, this fixer will only insert the empty line if the previous line ends with } or ;, so it just might work after all. This makes sense for statements, not sure for PHPDocs.
I still think it makes sense to make a dedicated fixer for this, like to hear input on this though. Even if we got to copy-paste relevant parts to a new fixer; it still looks like a nice feature and very promising solution.

@addiks
Copy link
Contributor Author

addiks commented Oct 9, 2019

Honestly i don't know if this is the best solution or not. I do not have enough experience with this project to judge that. Currently i like it because it creates a feature i want just by adding one very simple line. That seems elegant to me.

Maybe i am more motivated to write a new fixer for this when someone more experienced crushes my hopes by showing how this simple solution fails in some situation i do not yet see. ;-)

In the meantime i write one or two actual new fixers for other features that i would like and explore the project that way...

@SpacePossum
Copy link
Contributor

I try to keep an eye out on the overall responsibilities for each fixer because we have/had a lot of issues with fixer doing to much, making these less flexible for later extension and adding configuration, causing priority issues we can't resolve and the only way out are BC changes on a next major which no one like because these ship rarely, causing issues and PR's to stale for months.

So...no, I didn't spot an issue with your code and I don't want to crush your hopes, sorry if it feels that
way. What I did say about your code it still looks like a nice feature and very promising solution. .

Copy link
Contributor

@kubawerlos kubawerlos left a comment

Choose a reason for hiding this comment

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

Reading the discussion I think this fixer is the right place to update.

If saying handling doc_comment is too much responsibility for the fixer then the same can be said about any member of $tokenMap, right?

With the small change from the other comment, it looks to me like something we can think about merging.

@@ -40,6 +40,7 @@ final class BlankLineBeforeStatementFixer extends AbstractFixer implements Confi
'continue' => T_CONTINUE,
'declare' => T_DECLARE,
'default' => T_DEFAULT,
'doccomment' => T_DOC_COMMENT,
Copy link
Contributor

Choose a reason for hiding this comment

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

Let's have the key named doc_comment (as there is already require_once).

@localheinz
Copy link
Member

@addiks

Can you take another look, and ideally resolve conflicts?

Copy link
Contributor

@kubawerlos kubawerlos left a comment

Choose a reason for hiding this comment

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

Please revert your changes on logo.png.

@addiks
Copy link
Contributor Author

addiks commented Apr 16, 2021

I have now reverted the changes to logo.png. This was a tricky one because git thought it was a text file and changed a CRLF to LF in that file, corrupting it in the process. The line * text=auto eol=lfin .gitattributes defines that all files are text files and that the mentioned automatic replacement must happen for all files, including PNG files. I have added an exception for PNG files to the .gitattributes files so that it stops corrupting the PNG file(s).

@coveralls
Copy link

coveralls commented Apr 16, 2021

Coverage Status

Coverage remained the same at 93.034% when pulling d5c6851 on addiks:feature-blankline-before-doccomment into f2808cf on FriendsOfPHP:master.

@SpacePossum SpacePossum force-pushed the feature-blankline-before-doccomment branch 3 times, most recently from 7ac2c41 to ec70efb Compare December 13, 2021 15:16
@SpacePossum SpacePossum force-pushed the feature-blankline-before-doccomment branch from ec70efb to ef752ba Compare December 13, 2021 15:17
@SpacePossum SpacePossum changed the title Can now add blank lines before doc-comments BlankLineBeforeStatementFixer - can now add blank lines before doc-comments Dec 13, 2021
@SpacePossum
Copy link
Contributor

Thank you @addiks.

@SpacePossum SpacePossum merged commit 627c5c7 into PHP-CS-Fixer:master Dec 13, 2021
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

6 participants