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

[Documentation] Squiz: Function Spacing #451

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

jaymcp
Copy link
Contributor

@jaymcp jaymcp commented Apr 15, 2024

Description

This PR adds documentation for the Squiz.WhiteSpace.FunctionSpacing Sniff.

Suggested changelog entry

Add documentation for the Squiz Function Spacing Sniff

Related issues/external references

Part of #148

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)
    • This change is only breaking for integrators, not for external standards or end-users.
  • Documentation improvement

PR checklist

  • I have checked there is no other PR open for the same change.
  • I have read the Contribution Guidelines.
  • I grant the project the right to include and distribute the code under the BSD-3-Clause license (and I have the right to grant these rights).
  • I have added tests to cover my changes.
  • I have verified that the code complies with the projects coding standards.
  • [Required for new sniffs] I have added XML documentation for the sniff.

@jaymcp
Copy link
Contributor Author

jaymcp commented Apr 15, 2024

@jrfnl apologies for the delay since my last PR. FYI my GitHub handle has now changed (jonmcp => jaymcp).

Copy link
Contributor

@DannyvdSluijs DannyvdSluijs left a comment

Choose a reason for hiding this comment

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

LGTM,

For reference the added documentation renders as follows:

-------------------------------------------
| SQUIZ CODING STANDARD: FUNCTION SPACING |
-------------------------------------------

There should be exactly 2 blank lines before a function within a statement group or control
structure when it is the first block of code.

----------------------------------------- CODE COMPARISON ------------------------------------------
| Valid: Exactly 2 blank lines before the first  | Invalid: Too few blank lines before the first   |
| function.                                      | function.                                       |
----------------------------------------------------------------------------------------------------
| class MyClass                                  | class MyClass                                   |
| {                                              | {                                               |
|                                                |                                                 |
|                                                |     public function foo() {                     |
|     public function foo() {                    |     }                                           |
|     }                                          |                                                 |
|                                                |                                                 |
|                                                | }                                               |
| }                                              |                                                 |
----------------------------------------------------------------------------------------------------

----------------------------------------- CODE COMPARISON ------------------------------------------
| Valid: Exactly 2 blank lines before the first  | Invalid: Too many blank lines before the first  |
| function.                                      | function.                                       |
----------------------------------------------------------------------------------------------------
| if ($something) {                              | if ($something) {                               |
|                                                |                                                 |
|                                                |                                                 |
|     function foo() {                           |                                                 |
|     }                                          |     function foo() {                            |
|                                                |     }                                           |
|                                                |                                                 |
|     function bar() {                           |                                                 |
|     }                                          |     function bar() {                            |
|                                                |     }                                           |
|                                                |                                                 |
| }                                              |                                                 |
|                                                | }                                               |
----------------------------------------------------------------------------------------------------

There should be exactly 2 blank lines before and after a function declaration.

----------------------------------------- CODE COMPARISON ------------------------------------------
| Valid: Exactly 2 blank lines before and after  | Invalid: Incorrect number of blank lines        |
| function declarations.                         | before/after function declarations.             |
----------------------------------------------------------------------------------------------------
| interface MyInterface                          | interface MyInterface                           |
| {                                              | {                                               |
|                                                |                                                 |
|                                                |                                                 |
|     public function foo();                     |     public function foo();                      |
|                                                |     public function bar();                      |
|                                                |                                                 |
|     public function bar();                     |                                                 |
|                                                |                                                 |
|                                                |     public function baz();                      |
|     public function baz();                     |                                                 |
|                                                |                                                 |
|                                                | }                                               |
| }                                              |                                                 |
----------------------------------------------------------------------------------------------------

There should be exactly two blank lines after a function within a statement group or control
structure when it is the last block of code.

----------------------------------------- CODE COMPARISON ------------------------------------------
| Valid: Exactly 2 blank lines after the last    | Invalid: Too few blank lines after the last     |
| function.                                      | function.                                       |
----------------------------------------------------------------------------------------------------
| trait MyTrait                                  | trait MyTrait                                   |
| {                                              | {                                               |
|                                                |                                                 |
|                                                |                                                 |
|     public function foo() {                    |     public function foo() {                     |
|     }                                          |     }                                           |
|                                                |                                                 |
|                                                |                                                 |
|     public function bar() {                    |     public function bar() {                     |
|     }                                          |     }                                           |
|                                                | }                                               |
|                                                |                                                 |
| }                                              |                                                 |
----------------------------------------------------------------------------------------------------

----------------------------------------- CODE COMPARISON ------------------------------------------
| Valid: Exactly 2 blank lines after the last    | Invalid: Too many blank lines after the last    |
| function.                                      | function.                                       |
----------------------------------------------------------------------------------------------------
| if ($something) {                              | if ($something) {                               |
|                                                |                                                 |
|                                                |                                                 |
|     function foo() {                           |     function foo() {                            |
|     }                                          |     }                                           |
|                                                |                                                 |
|                                                |                                                 |
|     function bar() {                           |     function bar() {                            |
|     }                                          |     }                                           |
|                                                |                                                 |
|                                                |                                                 |
| }                                              |                                                 |
|                                                | }                                               |
----------------------------------------------------------------------------------------------------

Copy link
Member

@jrfnl jrfnl left a comment

Choose a reason for hiding this comment

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

@jaymcp Hi Jay, welcome back and thanks for this PR.

Generally speaking, looking good again !

I have a couple of questions. These don't necessarily mean anything needs to change, I'd just like to hear your reasoning and we can discuss further based on that.

  1. In the docs, it mentions "within a statement group or control structure" in a couple of places. Where does this come from ? And is this clear enough ?
  2. While the sniff allows for configuring different expectations for the spacing before/after the first/between/last function, in its default state, the expectations are the same: 2 blank lines. Have you considered combining the standards/code samples ? And if so, what made you decide against that ?

I look forward to seeing your reply.

@jaymcp
Copy link
Contributor Author

jaymcp commented Apr 26, 2024

Thanks @jrfnl!

In the docs, it mentions "within a statement group or control structure" in a couple of places. Where does this come from ? And is this clear enough ?

I struggled with the wording of this; I couldn't think of a reasonably concise way of explaining the expectations of this rule in a way that also provided enough context. I landed on "statement group", but I agree that it's not clear enough. I'd appreciate your advice here.

While the sniff allows for configuring different expectations for the spacing before/after the first/between/last function, in its default state, the expectations are the same: 2 blank lines. Have you considered combining the standards/code samples ? And if so, what made you decide against that ?

I decided on that purely because they have different error codes. Would you like me to combine them?

@jrfnl
Copy link
Member

jrfnl commented Apr 26, 2024

In the docs, it mentions "within a statement group or control structure" in a couple of places. Where does this come from ? And is this clear enough ?

I struggled with the wording of this; I couldn't think of a reasonably concise way of explaining the expectations of this rule in a way that also provided enough context. I landed on "statement group", but I agree that it's not clear enough. I'd appreciate your advice here.

Well, let's break it down: what are you trying to say with it ? (in your own words, doesn't have to ready for the docs, just trying to get a feel for why the phrase was added)

While the sniff allows for configuring different expectations for the spacing before/after the first/between/last function, in its default state, the expectations are the same: 2 blank lines. Have you considered combining the standards/code samples ? And if so, what made you decide against that ?

I decided on that purely because they have different error codes. Would you like me to combine them?

Not necessarily, though I wonder if the amount of examples is overkill. Can you think of a way to still keep the gist of the code samples intact with a more minimal set of examples ? (and "no", is, of course, also a potential answer)

@jrfnl
Copy link
Member

jrfnl commented May 16, 2024

@jaymcp Just checking in: had a chance to think my question over yet ?

@jaymcp
Copy link
Contributor Author

jaymcp commented May 20, 2024

Apologies for the further delay, @jrfnl, and thanks for your continued patience.

In the docs, it mentions "within a statement group or control structure" in a couple of places. Where does this come from ? And is this clear enough ?

I struggled with the wording of this; I couldn't think of a reasonably concise way of explaining the expectations of this rule in a way that also provided enough context. I landed on "statement group", but I agree that it's not clear enough. I'd appreciate your advice here.

Well, let's break it down: what are you trying to say with it ? (in your own words, doesn't have to ready for the docs, just trying to get a feel for why the phrase was added)

I was trying to articulate that, not only will this sniff check functions in the top-level of a file and in perhaps more obvious places like object structures, but also in control structures and whatnot. I could perhaps have said something like "There should be exactly 2 blank lines before/after a function declaration.", but wasn't 100% confident that that was accurate either.

While the sniff allows for configuring different expectations for the spacing before/after the first/between/last function, in its default state, the expectations are the same: 2 blank lines. Have you considered combining the standards/code samples ? And if so, what made you decide against that ?

I decided on that purely because they have different error codes. Would you like me to combine them?

Not necessarily, though I wonder if the amount of examples is overkill. Can you think of a way to still keep the gist of the code samples intact with a more minimal set of examples ? (and "no", is, of course, also a potential answer)

We could probably remove the first and last code samples without impacting readability at all, in my opinion. I wanted to illustrate that this sniff did not only apply to object structures, but we don't need examples of all of them, and perhaps not more than one example for a conditional.
I now realise, though, that I've not included an example similar to that shown in one of the unit tests.

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