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

Squiz/FunctionDeclarationArgumentSpacing: handle constructor property promotion #792

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

jrfnl
Copy link
Member

@jrfnl jrfnl commented Jan 23, 2025

Description

Squiz/FunctionDeclarationArgumentSpacing: refactor logic for for "spacing after comma" check

This refactor has two purposes:

  • Reduce code duplication.
  • Prevent introducing even more code duplication when a new error code will be introduced in the next commit.

Squiz/FunctionDeclarationArgumentSpacing: special case "spacing after comma" vs constructor property promotion

While incorrect spacing after a comma for constructor property promotion parameters would already be flagged and fixed by the sniff, the error message and code were incorrect/unclear.

Given the following test code:

class PropertyPromotionSpacingAfterComma {
    public function __construct(private string|int $propA, protected bool $correctSpace, public MyClass $tooMuchSpace, readonly string $noSpace) {}
}

Previously the following would be reported:

 198 | ERROR | [x] Expected 1 space between comma and type hint "MyClass"; 2 found
     |       |     (Squiz.Functions.FunctionDeclarationArgumentSpacing.SpacingBeforeHint)
 198 | ERROR | [x] Expected 1 space between comma and type hint "string"; 0 found
     |       |     (Squiz.Functions.FunctionDeclarationArgumentSpacing.NoSpaceBeforeHint)

Take note of the "type hint" in the message and the "Hint" suffix for the error codes.

With the fix from this commit in place, this will now be reported as follows:

 198 | ERROR | [x] Expected 1 space between comma and property modifier "public"; 2 found
     |       |     (Squiz.Functions.FunctionDeclarationArgumentSpacing.SpacingBeforePropertyModifier)
 198 | ERROR | [x] Expected 1 space between comma and property modifier "readonly"; 0 found
     |       |     (Squiz.Functions.FunctionDeclarationArgumentSpacing.NoSpaceBeforePropertyModifier)

Includes tests.

Squiz/FunctionDeclarationArgumentSpacing: handle modifiers for constructor property promotion

The spacing after visibility/readonly modifiers for constructor property promotion were so far not checked by this sniff.

While the Squiz.WhiteSpace.ScopeKeywordSpacing sniff will already handle this, that sniff may not be in use in all standards using this sniff. As things were, this sniff was just no longer feature complete for the task this sniff is supposed to handle: spacing of function declaration arguments.

This commit adds handling the spacing after modifiers used for constructor property promotion to this sniff.

The spacing requirements are aligned with the spacing expectations of the Squiz.WhiteSpace.ScopeKeywordSpacing sniff, so the sniffs should not conflict with each other.

Additionally, the new checks in this sniff have dedicated error codes, which means that - if there would be a conflict anywhere - the modifier spacing checks within this sniff can easily be turned off.

Includes tests.

Suggested changelog entry

Changed

  • Squiz.Functions.FunctionDeclarationArgumentSpacing: incorrect spacing after a comma followed by a promoted property has an improved error message and will now be flagged with the SpacingBeforePropertyModifier or NoSpaceBeforePropertyModifier error codes.
  • The Squiz.Functions.FunctionDeclarationArgumentSpacing sniff will now also check the spacing around property modifiers for promoted properties in the function signature of constructors

Related issues/external references

Loosely related to/ follow up on #620 and #783

Types of changes

  • New feature (non-breaking change which adds functionality)

jrfnl added 3 commits January 23, 2025 20:25
…cing after comma" check

This refactor has two purposes:
* Reduce code duplication.
* Prevent introducing even more code duplication when a new error code will be introduced in the next commit.
… comma" vs constructor property promotion

While incorrect spacing after a comma for constructor property promotion parameters would already be flagged and fixed by the sniff, the error message and code were incorrect/unclear.

Given the following test code:
```php
class PropertyPromotionSpacingAfterComma {
    public function __construct(private string|int $propA, protected bool $correctSpace, public MyClass $tooMuchSpace, readonly string $noSpace) {}
}
```

Previously the following would be reported:
```
 198 | ERROR | [x] Expected 1 space between comma and type hint "MyClass"; 2 found
     |       |     (Squiz.Functions.FunctionDeclarationArgumentSpacing.SpacingBeforeHint)
 198 | ERROR | [x] Expected 1 space between comma and type hint "string"; 0 found
     |       |     (Squiz.Functions.FunctionDeclarationArgumentSpacing.NoSpaceBeforeHint)
```

Take note of the "type hint" in the message and the "Hint" suffix for the error codes.

With the fix from this commit in place, this will now be reported as follows:
```
 198 | ERROR | [x] Expected 1 space between comma and property modifier "public"; 2 found
     |       |     (Squiz.Functions.FunctionDeclarationArgumentSpacing.SpacingBeforePropertyModifier)
 198 | ERROR | [x] Expected 1 space between comma and property modifier "readonly"; 0 found
     |       |     (Squiz.Functions.FunctionDeclarationArgumentSpacing.NoSpaceBeforePropertyModifier)
```

Includes tests.
…uctor property promotion

The spacing after visibility/`readonly` modifiers for constructor property promotion were so far not checked by this sniff.

While the `Squiz.WhiteSpace.ScopeKeywordSpacing` sniff will already handle this, that sniff may not be in use in all standards using this sniff. As things were, this sniff was just no longer feature complete for the task this sniff is supposed to handle: spacing of function declaration arguments.

This commit adds handling the spacing after modifiers used for constructor property promotion to this sniff.

The spacing requirements are aligned with the spacing expectations of the `Squiz.WhiteSpace.ScopeKeywordSpacing` sniff, so the sniffs should not conflict with each other.

Additionally, the new checks in this sniff have dedicated error codes, which means that - if there would be a conflict anywhere - the modifier spacing checks within this sniff can easily be turned off.

Includes 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.

1 participant