Skip to content

Comments

Make the CS stricter; fix some bugs / issues#1007

Merged
dunglas merged 3 commits intoapi-platform:2.0from
teohhanhui:feature/make-cs-strict-again
Apr 7, 2017
Merged

Make the CS stricter; fix some bugs / issues#1007
dunglas merged 3 commits intoapi-platform:2.0from
teohhanhui:feature/make-cs-strict-again

Conversation

@teohhanhui
Copy link
Contributor

Q A
Bug fix? yes
New feature? no
BC breaks? no
Deprecations? no
Tests pass? yes
Fixed tickets N/A
License MIT
Doc PR N/A

Stricter CS = better code, especially declare(strict_types=1);

'after' => [
[
'dummyDate' => true,
'dummyDate' => null,
Copy link
Member

Choose a reason for hiding this comment

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

why the change here?

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 is $nullManagement, which can only be string or null, not bool.

'ordered_imports' => true,
// 'phpdoc_add_missing_param_annotation' => [
// 'only_untyped' => false,
// ],
Copy link
Member

Choose a reason for hiding this comment

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

should we remove those?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I plan to get these in next... 😄

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@dunglas I want to get this in too... But this is gonna require quite a bit of manual work. 😆

@teohhanhui teohhanhui force-pushed the feature/make-cs-strict-again branch from 4941c42 to 9d18991 Compare March 24, 2017 10:53
'modernize_types_casting' => true,
// 'native_function_invocation' => true,
'no_extra_consecutive_blank_lines' => [
'break',
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This one may be a bit controversial, but I think it's good that it encourages us to not have complex logic nested inside a switch-case, or even avoid switch-case altogether.

Copy link
Member

Choose a reason for hiding this comment

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

I agree with the intent, but it makes the code (even simple statements) less readable (IMO).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah, personally I prefer the blank line too, but for the sake of consistency...

Copy link
Member

Choose a reason for hiding this comment

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

Consistency with what?

Copy link
Contributor Author

@teohhanhui teohhanhui Mar 28, 2017

Choose a reason for hiding this comment

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

We cannot enforce a blank line before/after break;, but we can enforce break; always sticking to the next case:

switch ($str) {
    case 'A':
        $this->doSomething();

        $this->doSomethingElse();

        break;
    case 'B':
        $this->doSomethingAwesome();
        break;
}

Copy link
Member

Choose a reason for hiding this comment

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

But we can ignore this rule too and check it manually (it's not a big deal after all).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Do you need me to revert this? I'll have to do it manually...

Copy link
Member

Choose a reason for hiding this comment

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

Don't worry about that, keep it as is.

$methods = $route->getMethods();

if ($resourceClass === $currentResourceClass && null !== $operation && (empty($methods) || in_array('GET', $methods))) {
if ($resourceClass === $currentResourceClass && null !== $operation && (empty($methods) || in_array('GET', $methods, true))) {
Copy link
Member

Choose a reason for hiding this comment

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

The call to empty can be replaced by the ! operator.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I really don't like such implicit conversions... And I don't like empty too...

My preference would be 0 === count($methods), but I understand there is a performance overhead (how much?) for the function call.

Copy link
Member

Choose a reason for hiding this comment

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

I prefer to keep in sync with what we do in Symfony (we remove empty, count and so on).

@dunglas
Copy link
Member

dunglas commented Mar 27, 2017

Awesome. Can you just fix StyleCI ? Putting the declare line before the license header should do the trick.

@teohhanhui
Copy link
Contributor Author

Can you just fix StyleCI ? Putting the declare line before the license header should do the trick.

No. StyleCI is buggy here. See PHP-CS-Fixer/PHP-CS-Fixer#2590

Also, declare should go after file-level docblock, according to PSR-12 draft.

@dunglas
Copy link
Member

dunglas commented Mar 28, 2017

So what do we do in the meantime? Disable StyleCI? Or disable the header check?

@teohhanhui
Copy link
Contributor Author

Disabling the header check sounds reasonable. But it might escape our watchful eyes. 😄

@dunglas
Copy link
Member

dunglas commented Mar 28, 2017

We'll restore the check when PHP CS Fixer will be updated.

@dunglas
Copy link
Member

dunglas commented Apr 1, 2017

StyleCI config updated. Can you rebase?

@teohhanhui teohhanhui force-pushed the feature/make-cs-strict-again branch from 9d18991 to b4b3cc4 Compare April 3, 2017 05:07
],
'declare_strict_types' => true,
'modernize_types_casting' => true,
// 'native_function_invocation' => true,
Copy link
Contributor Author

Choose a reason for hiding this comment

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

What do you guys think about this? I remember @dunglas talking about this before... We'll need to do Blackfire profiling for this to see the performance difference.

@teohhanhui teohhanhui force-pushed the feature/make-cs-strict-again branch from b4b3cc4 to b80f208 Compare April 3, 2017 05:15
@teohhanhui
Copy link
Contributor Author

Rebased. But StyleCI still has a problem with return null; when Symfony has already changed its CS in view of the introduction of the void return type. I suspect they are using outdated PHP-CS-Fixer? 😞

@teohhanhui
Copy link
Contributor Author

It could make sense for us to disable StyleCI (at least for now?), because frankly I don't see what's the added value of having it.

@dunglas
Copy link
Member

dunglas commented Apr 3, 2017

Are you sure Symfony has changed this CS?

@dunglas
Copy link
Member

dunglas commented Apr 3, 2017

Anyway StyleCI is useless since we run our own PHP CS Fixer instance. I've just disabled it. Can you rebase again?

@soyuka
Copy link
Member

soyuka commented Apr 3, 2017

Styleci would make sense if it prevented the other CI from running on failure.

@teohhanhui
Copy link
Contributor Author

Yes, since symfony/symfony-docs#6614

  • Use return null; when a function explicitly returns null values and use return; when the function returns void values;

http://symfony.com/doc/current/contributing/code/standards.html

@teohhanhui
Copy link
Contributor Author

The only merit I see for StyleCI is the nice output, and the ability to send a PR. 😆

@teohhanhui teohhanhui force-pushed the feature/make-cs-strict-again branch from b80f208 to 766d106 Compare April 3, 2017 11:57
@dunglas dunglas merged commit 8c0664a into api-platform:2.0 Apr 7, 2017
@dunglas
Copy link
Member

dunglas commented Apr 7, 2017

Thank you @teohhanhui

@teohhanhui teohhanhui deleted the feature/make-cs-strict-again branch April 10, 2017 04:56
hoangnd25 pushed a commit to hoangnd25/core that referenced this pull request Feb 23, 2018
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.

3 participants