-
Notifications
You must be signed in to change notification settings - Fork 27
Add an example for closure with an empty body #133
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
base: master
Are you sure you want to change the base?
Conversation
spec.md
Outdated
| }; | ||
| ``` | ||
|
|
||
| If a closure contains no statements or comments then the body SHOULD be abbreviated |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
would recommend MUST
(but happy with both)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it should be similar to the Classes, Properties, and Methods section:
Lines 592 to 593 in c83ab63
| If a function or method contains no statements or comments (such as an empty no-op implementation or when using | |
| constructor property promotion), then the body SHOULD be abbreviated as `{}` and placed on the same |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
would recommend MUST in both places then ;)
keradus
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
would love this to be explicitly clarified ! 🎉
|
This isn't so much a clarification as it is a change to requirements. Closures currently have the following requirement: And abbreviated I think it'd be okay to make this change, but it's important to be clear that it's not just a clarification. That said, when would someone actually want to do this instead of using a short function which already allows single line declaration? IE: |
Agreed, so this would need a new major release. |
From the outside, it seems that this case was simply forgotten to be described, since it is written explicitly for class methods and functions. But I agree with you - this is actually a new requirement. |
I think this is a real case, because:
|
|
For context, the reason we went with a SHOULD for classes was to avoid it being a hard BC break. It allowed existing code to continue to be legal, while formatters changed their defaults gradually to single-line it. Would a SHOULD here be safe enough to only trigger a 3.1, not a 4.0, since it leaves existing code still legal? I'd prefer to not tag a 4.0 if possible, though I agree the same logic should apply to anything with a body. That said, @KorvinSzanto is correct that the use case in practice seems weird, when short closures are even nicer and have no such concerns. To @mspirkov's point, 7.4 is long-since abandoned so we don't really care about its limitations at this point. 😄 |
I was seeing this as forgotten case, as examples were not mentioning it. and potential breaking change could go to new major, separately, if aligned |
this was actually coming as request to PHP CS Fixer from PHP community. If usage of given syntax is allowed, it's great to document style for it explicitly. If you believe it's better to recommend avoiding using |
|
Let's go with "if a closure contains no statements, MUST use the short-hand syntax" (with an example). That neatly sidesteps the BC issue on braces, and gives a clear standard of behavior. Also, we already have a clause somewhere that says "ignore things that don't apply to your PHP version", so anyone who cares about <7.4 for some reason has nothing to do. |
|
Do I understand correctly that we are talking about |
|
Yes, that. |
|
@Crell , if I understand idea of MUST vs SHALL in BC break context for PER-CS, I believe your suggestion is same BC as defining spaces/newline for it is not that its reserved for PHP <7.4 only, as |
Crell
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's go with "if a closure contains no statements, MUST use the short-hand syntax" (with an example). That neatly sidesteps the BC issue on braces, and gives a clear standard of behavior. Also, we already have a clause somewhere that says "ignore things that don't apply to your PHP version", so anyone who cares about <7.4 for some reason has nothing to do.
By shorthand, we mean fn() => null.
May I suggest making that explicit in the text ? I.e. "if a closure contains no statements, it MUST be declared as an arrow function" |
|
@Crell Please take a look |
Co-authored-by: Larry Garfield <[email protected]>
Crell
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I will merge this in a few days if no members of the WG speak up in opposition.
@keradus suggested clarifying the situation with closures that have an empty body: PHP-CS-Fixer/PHP-CS-Fixer#9329 (comment)
I agree with him. I think the
Closuressection lacks an example with an empty body. It seems logical to add an example similar to the one in theClasses, Properties, and Methodssection:per-coding-style/spec.md
Lines 592 to 603 in c83ab63
What do you think about this?