8000 Added SingleClassElementPerStatementFixer by phansys · Pull Request #1889 · PHP-CS-Fixer/PHP-CS-Fixer · GitHub
[go: up one dir, main page]

Skip to content

Added SingleClassElementPerStatementFixer #1889

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

Merged
merged 2 commits into from
Jul 4, 2016

Conversation

phansys
Copy link
Contributor
@phansys phansys commented Apr 28, 2016
Q A
Branch master
Bug fix? no
New feature? yes
BC breaks? no
Deprecations? no
Tests pass? yes
Fixed tickets #1839
License MIT
Doc PR

According to PSR-2:

There MUST NOT be more than one property declared per statement.

*
* @author Javier Spagnoletti <phansys@gmail.com>
*/
final class SingleClassPropertyDeclarationFixer extends AbstractFixer
Copy link
Contributor

Choose a reason for hiding this comment

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

We have the SingleImportPerStatementFixer. So I think this fixer should be named SingleClassPropertyPerStatementFixer.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Updated fixer name.

@gharlan
Copy link
Contributor
gharlan commented Apr 28, 2016

Thanks for working on this!

I would like to see the same for class constants.

@phansys
Copy link
Contributor Author
phansys commented Apr 28, 2016

What do you mean @gharlan? AFAIK this syntax isn't supported for class constants.

@phansys phansys force-pushed the ticket_1839 branch 3 times, most recently from 5516133 to 717d3eb Compare April 29, 2016 01:28
@gharlan
Copy link
Contributor
gharlan commented Apr 29, 2016

What do you mean @gharlan? AFAIK this syntax isn't supported for class constants.

https://3v4l.org/jLJiC

*
* @author Javier Spagnoletti <phansys@gmail.com>
*/
final class SingleClassPropertyDeclaredPerStatementFixer extends AbstractFixer
Copy link
Contributor

Choose a reason for hiding this comment

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

hmm, my proposal was SingleClassPropertyPerStatementFixer, without "Declared"

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Updated again.

@phansys
Copy link
Contributor Author
phansys commented Apr 29, 2016

@gharlan, yesterday when I've read your message I've tested that syntax in 3v4l.org too and I'm sure that failed (I don't know what can I've tested now).
BTW, that syntax isn't covered by PSR-2, so I guess it can be done later within a contrib fixer.

@phansys phansys changed the title [WIP] [PSR-2] Add SingleClassPropertyDeclarationFixer [WIP] [PSR-2] Add single_property_per_statement Apr 29, 2016
@keradus
Copy link
Member
keradus commented Apr 29, 2016

@phansys same fixer, different configuration

@phansys
Copy link
Contributor Author
phansys commented Apr 29, 2016

@keradus, it's possible to attach just a fixer configuration to a ruleset (PSR-2 in this case)?

@keradus
Copy link
Member
keradus commented Apr 29, 2016

yes

@keradus
Copy link
Member
keradus commented Apr 29, 2016
'@mySet' => {
    'fixer_A' => true, // no-settings / default settings
    'fixer_B' => [ 'some' => 'settings' ],
}

@keradus
Copy link
Member
keradus commented Jun 7, 2016

any news here ?

@phansys
Copy link
Contributor Author
phansys commented Jun 8, 2016

I'm very busy these days @keradus, my apologies. I'll try to find some time on weekend in order to continue this task. BTW, if somebody else wants to put some effort on it, I'll be very glad.

@SpacePossum
Copy link
Contributor

@phansys please see phansys#1
let me know if you think it is helpful

With or without the PR this still needs work on the priorities.

@phansys
Copy link
Contributor Author
phansys commented Jun 17, 2016

Thanks for the time you're dedicating to this @SpacePossum. I totally agree if you want to provide a PR in replacement of mine. Please let me know.

@keradus
Copy link
Member
keradus commented Jun 17, 2016

@phansys if you able to finish this PR it would be great !

@SpacePossum thanks for your willingness for help, but please focus on currently opened PR of yours, like PHP7 bugfixes for 1.11 line ! I would say it's more important.

@keradus
Copy link
Member
keradus commented Jun 17, 2016

btw, please regenerate the readme ;)

@SpacePossum
Copy link
Contributor

@phansys I have no time to take this one over, but I'm happy to help out or doing reviewing.
To wrap this one up I think a few details have to done:

  • in my changes I insert the modifiers sequences without cloning the tokens, we need to make sure this is correct or change the code to do the cloning
  • I can't think of priority issues with the fixer on prio. 0. However to be a bit more sure about it please run the fixer together with others on some projects (of your own and maybe something like Symfony, Twig, etc.). Run the fixer twice, the second time there shouldn't be any changes
  • since it is PSR2 fixer it will be added to the Symfony by default, it would be nice to run the fixer on the Symfony project and if there are any hits please PR it @ Symfony, state it is the result of a new fixer and the goal of the PR is not to get it merged, but more of a heads up

good luck!

@SpacePossum
Copy link
Contributor

btw. ping @gharlan I've added your sample case, can you confirm this is what you meant?
(https://github.com/FriendsOfPHP/PHP-CS-Fixer/pull/1889/files#diff-35d2bcc89af69c2ad43f99869b477e02R46)

@gharlan
Copy link
Contributor
gharlan commented Jun 18, 2016

btw. ping @gharlan I've added your sample case, can you confirm this is what you meant?

yes, thanks!

use PhpCsFixer\Test\AbstractFixerTestCase;

/**
* @author Javier Spagnoletti <phansys@gmail.com>
Copy link
Member

Choose a reason for hiding this comment

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

please make tests green.

@phansys
Copy link
Contributor Author
phansys commented Jun 19, 2016

Test are green now @keradus.
Since this fixer takes care about class constants too, should we rename it? I propose "single_class_element_per_statement" or something similar.
I don't know if the same fixer could be used later as base for single variable declarations by instance.

@keradus
Copy link
A93C
Member
keradus commented Jun 24, 2016

@phansys
yes, please rename

@SpacePossum
please, review and thumb if good to go

@phansys
Copy link
Contributor Author
phansys commented Jun 24, 2016

I'll try to check this fixer today against some projects as @SpacePossum suggested.

@phansys
Copy link
Contributor Author
phansys commented Jun 25, 2016

I've renamed the fixer, added some other tests and opened a PR against Symfony in order to get some feedback.
Ping @keradus, @SpacePossum.

<<<'EOT'
<?php

class Foo
{

public $bar = null;

Copy link
Contributor

Choose a reason for hiding this comment

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

?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Which is your concern about this removed LF @GrahamCampbell?

Copy link
Contributor

Choose a reason for hiding this comment

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

Yes, I'm asking why this was removed.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Because this fixer now uses just the first previous line break as separator, based on feedback from Symfony.

@phansys phansys changed the title [WIP] [PSR-2] Add single_property_per_statement [WIP] [PSR-2] Add single_class_element_per_statement Jun 25, 2016
fabpot added a commit to symfony/symfony that referenced this pull request Jun 27, 2016
This PR was merged into the 2.7 branch.

Discussion
----------

[CS] Respect PSR2 4.2

| Q             | A
| ------------- | ---
| Branch        | 2.7
| Bug fix?      | no
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets |
| License       | MIT
| Doc PR        |

http://www.php-fig.org/psr/psr-2/#4-2-properties

I'm writing a [new fixer](PHP-CS-Fixer/PHP-CS-Fixer#1889) (`single_class_element_per_statement`), which intends to avoid multiple class elements in one statement.
This PR is a POC about the result of this fixer, so I'd like to know what do you think about, since it will be also added to the `@Symfony` ruleset.
Please note that class constants are considered too, but here isn't any offending code about that.

Example output:
```
$ ./php-cs-fixer.phar fix . --rules=single_class_element_per_statement -vvv
   1) src/Symfony/Component/Console/Tests/Input/InputDefinitionTest.php (single_class_element_per_statement)
   2) src/Symfony/Component/DependencyInjection/Tests/ContainerTest.php (single_class_element_per_statement)
   3) src/Symfony/Component/DependencyInjection/Tests/Fixtures/includes/foo.php (single_class_element_per_statement)
   4) src/Symfony/Bridge/ProxyManager/Tests/LazyProxy/Fixtures/includes/foo.php (single_class_element_per_statement)
```

Commits
-------

d250b1d [CS] Respect PSR2 4.2
$divisionContent = null;
if ($tokens[$startIndex - 1]->isWhitespace()) {
$divisionContent = $tokens[$startIndex - 1]->getContent();
if (preg_match('#(\n|\n\r)#', $divisionContent, $matches)) {
Copy link
Contributor

Choose a reason for hiding this comment

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

The sequence is typically \r\n IIRC, but it shouldn't matter, i.e. both cases should match.
So I think the match should be something like ( 0-N times \r 1-N times \n 0-N times \r ).

< 10000 /path> Copy link
Member

Choose a reason for hiding this comment

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

yes, \r\n for Windows

@@ -144,7 +142,9 @@ private function expandElement(Tokens $tokens, $startIndex, $endIndex)
if ($divisionContent) {
$tokens->insertAt($i + 1, new Token(array(T_WHITESPACE, $divisionContent)));
}
$tokens->insertAt($i + 2, $modifiers);
// collect modifiers
$sequence = $this->getModifiersSequences($tokens, $startIndex, $endIndex);
Copy link
Member

Choose a reason for hiding this comment

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

I'm not sure that calling getModifiersSequences method in loop is good, as it's already iterate through tokens in loop as well

@keradus
Copy link
Member
keradus commented Jul 1, 2016

Could you squash consecutive commits of same author please?

phansys and others added 2 commits July 1, 2016 07:42
| Q             | A
| ------------- | ---
| Branch        | master
| Bug fix?      | no
| New feature?  | yes
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | PHP-CS-Fixer#1839
| License       | MIT
| Doc PR        |
@phansys
Copy link
Contributor Author
phansys commented Jul 1, 2016

Of course @keradus, done.

@keradus
Copy link
Member
keradus commented Jul 1, 2016

👍

@phansys phansys changed the title [WIP] [PSR-2] Add single_class_element_per_statement [PSR-2] Add single_class_element_per_statement Jul 1, 2016
@SpacePossum
Copy link
Contributor

👍
LGTM

@keradus keradus added this to the v2.0 milestone Jul 1, 2016
@keradus keradus added kind/feature RTM Ready To Merge labels Jul 1, 2016
@keradus keradus changed the title [PSR-2] Add single_class_element_per_statement Added SingleClassElementPerStatementFixer Jul 4, 2016
@keradus keradus removed the RTM Ready To Merge label Jul 4, 2016
@keradus keradus merged commit c18509c into PHP-CS-Fixer:master Jul 4, 2016
keradus added a commit that referenced this pull request Jul 4, 2016
…cePossum)

This PR was merged into the 2.0-dev branch.

Discussion
----------

Added SingleClassElementPerStatementFixer

| Q             | A
| ------------- | ---
| Branch        | master
| Bug fix?      | no
| New feature?  | yes
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | #1839
| License       | MIT
| Doc PR        |

According to PSR-2:
>There MUST NOT be more than one property declared per statement.

Commits
-------

c18509c Fix
a329745 [PSR-2] Add `SingleClassElementPerStatementFixer`
@keradus
Copy link
Member
keradus commented Jul 4, 2016

Thank you @phansys and @SpacePossum ! Great work !!!

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.

5 participants
0