8000 [CS][2.7] yoda_style, no_unneeded_curly_braces, no_unneeded_final_method, semicolon_after_instruction by SpacePossum · Pull Request #24123 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[CS][2.7] yoda_style, no_unneeded_curly_braces, no_unneeded_final_method, semicolon_after_instruction #24123

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 2 commits into from

Conversation

SpacePossum
Copy link
Contributor
Q A
Branch? 2.7
Bug fix? no
New feature? no
BC breaks? no
Deprecations? no
Tests pass? yes
License MIT

Hi all and congratulations on the 1 billion of downloads! awesome :D

This is a courtesy call (or PR) from the PHP-CS-Fixer community.
As always I don't expect this PR to be merged nor do I've any strong feelings about why not.

The goal of this PR is to determine if the following new rules should be added to the Symfony ruleset by default. If so fabbot.io will (at some point) start reporting violations of these rules.
Please be aware that if the rules are accepted but this PR is not, any new PR's might contain a part of the fixes as proposed in this PR as fabbot.io will suggest to the authors of the new PR's to update the CS, consequently this might create noise.

Here are the rules:

    'no_unneeded_curly_braces' => true,
    'no_unneeded_final_method' => true,
    'semicolon_after_instruction' => true,
    'yoda_style' => true,

The first three are done in the first commit.
The yoda_style in the last.

I know the yoda-style fixing has a long history about it and some people have strong opinions on it. Please consider two things before commenting about Yoda

  • this PR is not meant to determine the Symfony code style, I'm sure Symfony has other channels for these kind of discussions
  • if you don't like Yoda style, please consider heading over to Add YodaStyleFixer PHP-CS-Fixer/PHP-CS-Fixer#2446 and review my PR. If it gets merged you'll have a free tool to change all Yoda expressions in your code to non-Yoda (you can do it around as well as you can see in both PR's).

Thank you for your time!

@SpacePossum SpacePossum changed the title 2 7 cs [CS][2.7] yoda_style, no_unneeded_curly_braces, no_unneeded_final_method, semicolon_after_instruction Sep 7, 2017
@nicolas-grekas
Copy link
Member

yoda_style is nice
semicolon_after_instruction doesn't look useful to me
no_unneeded_final_method & no_unneeded_curly_braces would need an actual example to decide

@SpacePossum
Copy link
Contributor Author

no_unneeded_final_method & no_unneeded_curly_braces would need an actual example to decide

$ php php-cs-fixer describe no_unneeded_final_method
Description of no_unneeded_final_method rule.
A final class must not have final methods.
--- Original
+++ New
@@ @@
<?php
final class Foo {
-    final public function foo() {}
-    final protected function bar() {}
-    final private function baz() {}
+    public function foo() {}
+    protected function bar() {}
+    private function baz() {}
}
$ php php-cs-fixer describe no_unneeded_curly_braces
Description of no_unneeded_curly_braces rule.
Removes unneeded curly braces that are superfluous and aren't part of a control structure's body.
--- Original
+++ New
@@ @@
-<?php {
+<?php 
     echo 1;
-}

 switch ($b) {
-    case 1: {
+    case 1: 
         break;
-    }
+    
 }

@keradus
Copy link
Member
keradus commented Sep 7, 2017

just to complete the picture:

$ php-cs-fixer describe semicolon_after_instruction
Description of semicolon_after_instruction rule.
Instructions must be terminated with a semicolon.
--- Original
+++ New
@@ @@
-<?php echo 1 ?>
+<?php echo 1; ?>

@linaori
Copy link
Contributor
linaori commented Sep 7, 2017

I believe the semicolon_after_instruction would also affect how the multi line termination are done?

Example from the source
    protected function getVoterFor2Roles($token, $vote1, $vote2)
    {
        $voter = $this->getMockBuilder('Symfony\Component\Security\Core\Authorization\Voter\VoterInterface')->getMock();
        $voter->expects($this->any())
              ->method('vote')
              ->will($this->returnValueMap(array(
                  array($token, null, array('ROLE_FOO'), $vote1),
                  array($token, null, array('ROLE_BAR'), $vote2),
-              )))
-        ;
+              )));

        return $voter;
    }

@stof
Copy link
Member
stof commented Sep 7, 2017

The case given by @iltar looks wrong to me. The semi-colon was here, even though there was a newline before it. It is not a missing semi-colon.

@SpacePossum
Copy link
Contributor Author

semicolon_after_instruction shouldn't move semicolons around, it will insert one if it is missing.
The logic behind is; if there is a close tag (?>) check if there is semicolon before it, if not check if needed and insert if so.
The example you link to should therefor not be touched by the fixer (there is no close tag),
my guess is that the diff is the result from no_multiline_whitespace_before_semicolons, which I didn't run (I hope...).

@linaori
Copy link
Contributor
linaori commented Sep 7, 2017

My case was a question if that would be the case as well (sorry if I put it a bit weird), I manually diffed it to see if my assumption was correct about the rule.

@SpacePossum
Copy link
Contributor Author

all good :)
so to answer; no the semicolon_after_instruction should not create the diff you showed, but we have another rule for that.

@linaori
Copy link
Contributor
linaori commented Sep 7, 2017

I'm not even sure if this is something that rule (with the ; on newline) was agreed upon, I just know that it's being done differently in different files, just wanted to add it to make sure that was taking into account if the rule would've done that.

I don't think the check semicolon_after_instruction is very useful, because Symfony doesn't really contain many ?> in the source. The only places I've seen this being used, is in form templates and they don't really include the ; before the ?>, and in tests.

@nicolas-grekas
Copy link
Member
nicolas-grekas commented Sep 7, 2017

Oh btw, we should disable this (I mean, php-cs-fixer should not do that):

final class Foo {
-    protected function bar() {}
+    private function bar() {}
}

@SpacePossum
Copy link
Contributor Author

semicolon_after_instruction
will not be added to the SF set 👍 I'll update the PR later.

no_unneeded_final_method ,
@nicolas-grekas by we should disable this: you mean it should not be added to the current set?

no_unneeded_curly_braces
to be determined

yoda_style
to be determined

I'm not even sure if this is something that rule (with the ; on newline) was agreed upon,
took some digging, but here is this discussion PHP-CS-Fixer/PHP-CS-Fixer#511 (comment) , concluding not to enforce this in SF. Let me know if you guys want this changed to be enforced.

@nicolas-grekas
Copy link
Member
nicolas-grekas commented Sep 7, 2017

I mean this should not be done by cs-fixer (it is right now, I already saw a false-positive by fabbot on a PR)

@nicolas-grekas
Copy link
Member
nicolas-grekas commented Sep 7, 2017

yoda_style

I'm definitely 👍 on that one, that will save us hundred of comments reminding we follow yoda style

@nicolas-grekas
Copy link
Member

btw, if you know how to fix this false positive, that'd be great:
https://fabbot.io/report/symfony/symfony/23901/1f92e459db0efd2576eaa63a4fa0fe39e68b32dd

@keradus
Copy link
Member
keradus commented Sep 7, 2017

we should disable this
I mean this should not be done by cs-fixer (it is right now, I already saw a false-positive by fabbot on a PR)

what's the purpose of protected method inside of final class that does not inherit ?
please provide case (fabbot-ed or not) when sth went wrong, but in example you did provided, there were no reason for protected instead of private visibility attribute

@nicolas-grekas
Copy link
Member
nicolas-grekas commented Sep 7, 2017

The use case is easy: just try changing that with the existing code in CacheItem and you'll see by looking at tests. Hint: it relates to casting to arrays, which does not produce the same result in both cases.

@keradus
Copy link
Member
keradus commented Sep 7, 2017

I analysed the scenario.

IMHO, accessing protected key property that way:

        $item = (array) $item;
        $key = $item["\0*\0key"];

is abusing the language to use (array) casting to extract encapsulated properties...

The rule behind the fixer is good in general, you just hint edge cases, where it doesn't work for you due to using that workaround to get encapsulated property.
If this class is violating OOP, it's hard to apply OOP good practices for it.

I was looking how to fix CacheItem handling, and then I found that lovely piece of code to set protected variables (CacheItem has no public properties):

        $this->createCacheItem = \Closure::bind(
            function ($key, $value, $isHit) use ($defaultLifetime) {
                $item = new CacheItem();
                $item->key = $key;
                $item->value = $value;
                $item->isHit = $isHit;
                $item->defaultLifetime = $defaultLifetime;

                return $item;
            },
            null,
            CacheItem::class
        );

what's the thing here, why it has to abuse OOP that much ?

@nicolas-grekas
Copy link
Member
nicolas-grekas commented Sep 7, 2017

If you want to have that rule, it belongs to a "code quality" tool - not to a CS fixer.
Here, you're asking for changing the implementation. That's something very different than the code style.

@keradus
Copy link
Member
keradus commented Sep 7, 2017

Fixer went far beyond touching whitespaces only ages ago ;)
Standard could not touch a formatting only, it's not an issue.

Yet, you did not answer my question :(

@fabpot
Copy link
Member
fabpot commented Sep 7, 2017

👍 for all new proposed rules

@nicolas-grekas
Copy link
Member
nicolas-grekas commented Sep 8, 2017

@keradus I did not answer your question because it's off topic for this PR. Yet, you say something is abusing OOP. To me, nothing does. Accessing private state internally is not abusing OOP. That's what it is here. Of course, "private" here doesn't mean "private by PHP rules". But still it's private - internal detail if you prefer. PHP is just a tool. If we were in C++, we would use friendship. Here, we do with what we have to achieve the same. SOLID everywhere.

@keradus
Copy link
Member
keradus commented Sep 8, 2017

you raised a concern and pointed the class, so i did investigated it ;)
let me rephrase - code is abusing oop of php, you dont have friendship relation, sealed accessibility, nothing like that. in pointed code, there is a big workaround in way not promoted by majority of projects.
Rule came to promote oop and encapsulation. I propose to keep it simply excluding that one file in config if there is no willing to stop abusing php visibility.

@linaori
Copy link
Contributor
linaori commented Sep 8, 2017

I agree with @keradus on this one. The solution is more of a hack and an exception (due to limited features). Protected functions in a final class make no sense, if this is done on purpose, it should explicitly be excluded from the check and also documented why (bus/truck/plane/etc factor).

@nicolas-grekas
Copy link
Member

What matters to me is fixing this false positive. An exclusion would do it so I'm fine with it.

@keradus
Copy link
Member
keradus commented Sep 8, 2017

and also documented why (bus/truck/plane/etc factor).

big 👍

@nicolas-grekas
Copy link
Member

@SpacePossum can you open another PR to just enable the new rules (without applying them on the code base)? Than would enforce them for the new PRs, that'd be a good first step.

@SpacePossum
Copy link
Contributor Author

@nicolas-grekas that wouldn't work, but no worries.

Since we've established that these rules should go the SF ruleset I've opened;
PHP-CS-Fixer/PHP-CS-Fixer#3037
If that is all well and done the rules are added to the default SF ruleset and when we release the new version of the fixer these become available (and effective here) when fabbot.io has been updated to use the fixer version.
Adding the rules now in this repo would crash fabbot.io because the new rules are not available to it.
We're close to releasing the new version of the fixer, we didn't do it over the weekend because to some members were not available to wrap things up, but we're close :)

nicolas-grekas added a commit that referenced this pull request Sep 11, 2017
This PR was merged into the 3.3 branch.

Discussion
----------

[CS] Remove `protected_to_private` rule

| Q             | A
| ------------- | ---
| Branch?       | 3.1
| Bug fix?      | no
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | n/a
| License       | MIT

Following:
#24123 (comment)

I picked 3.1 as target as the rule discussed;

| branch | hits
| --------- | ---
| 2.7 | 0
| 2.8 | 0
| 3.0 | 0
| 3.1 | 1
| master |  1 hit (same file as on 3.1)

Commits
-------

bfae454 Remove `protected_to_private` rule.
@SpacePossum
Copy link
Contributor Author

fabbot.io licence header failure doesn't provide anything useful (https://fabbot.io/report/symfony/symfony/24123/ef371741353cb0ec6b1e8405ad5a4dbbd436f85c)
not sure how to resolve

@nicolas-grekas
Copy link
Member

Thank you @SpacePossum.

nicolas-grekas added a commit that referenced this pull request Sep 15, 2017
…ded_final_method, semicolon_after_instruction (SpacePossum)

This PR was squashed before being merged into the 2.7 branch (closes #24123).

Discussion
----------

[CS][2.7] yoda_style, no_unneeded_curly_braces, no_unneeded_final_method, semicolon_after_instruction

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

Hi all and congratulations on the 1 billion of downloads! awesome :D

This is a courtesy call (or PR) from the PHP-CS-Fixer community.
As always I don't expect this PR to be merged nor do I've any strong feelings about why not.

The goal of this PR is to determine if the following new rules should be added to the Symfony ruleset by default. If so `fabbot.io` will (at some point) start reporting violations of these rules.
Please be aware that if the rules are accepted but this PR is not, any new PR's might contain a part of the fixes as proposed in this PR as fabbot.io will suggest to the authors of the new PR's to update the CS, consequently this might create noise.

Here are the rules:

```
    'no_unneeded_curly_braces' => true,
    'no_unneeded_final_method' => true,
    'semicolon_after_instruction' => true,
    'yoda_style' => true,
```

The first three are done in the first commit.
The `yoda_style` in the last.

I know the `yoda`-style fixing has a long history about it and some people have strong opinions on it. Please consider two things before commenting about Yoda
- this PR is not meant to determine the Symfony code style, I'm sure Symfony has other channels for these kind of discussions
- if you don't like Yoda style, please consider heading over to PHP-CS-Fixer/PHP-CS-Fixer#2446 and review my PR. If it gets merged you'll have a free tool to change all Yoda expressions in your code to non-Yoda (you can do it around as well as you can see in both PR's).

Thank you for your time!

Commits
-------

3e90138 [CS][2.7] yoda_style, no_unneeded_curly_braces, no_unneeded_final_method, semicolon_after_instruction
@nicolas-grekas
Copy link
Member
nicolas-grekas commented Sep 15, 2017

now applied up to master

@SpacePossum SpacePossum deleted the 2_7_CS branch September 15, 2017 10:52
fabpot added a commit that referenced this pull request Sep 15, 2017
This PR was merged into the 2.7 branch.

Discussion
----------

CS: recover no_break_comment

| Q             | A
| ------------- | ---
| Branch?       | 2.7
| Bug fix?      | no
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | n/a, see CIs
| Fixed tickets | n/a
| License       | MIT
| Doc PR        | n/a

rule was excluded in #24123, yet without any information about it
it was not excluded in original form of given PR, it's not part of any commitlog nor any comment, title claims to touch only other rules.

this rule shall not be excluded, it's part of PSR and is applied for most places.
I added missing ones.

Commits
-------

4e9d160 CS: recover no_break_comment
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
0