Skip to content

4.0 | PSR12/FileHeader: make "SpacingAfter" and "SpacingInside" errorcodes modular #35

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

Merged

Conversation

jrfnl
Copy link
Member

@jrfnl jrfnl commented Nov 9, 2023

Description

Recreation of upstream PR squizlabs/PHP_CodeSniffer#2729:

This changes the error codes for the SpacingAfterBlock and SpacingInsideBlock errors to modular error codes which include a reference to the type of header block, i.e. SpacingAfterDeclareBlock, SpacingInsideUseFunctionBlock etc.

This allows for more selective application of the rules.

Take for instance the quite common case of the header blocks all being separated by blank lines, except for the open tag and file docblock.

<?php
/**
 * File docblock.
 */

namespace A\B\C;

use B\C;

The modular errorcodes I'm proposing in this PR will allow for the sniff to be used to safeguard the blank lines between each section without enforcing a blank line between the PHP open tag and the file docblock using:

<rule ref="PSR12.Files.FileHeader">
   <exclude name="PSR12.Files.FileHeader.SpacingAfterTagBlock"/>
</rule>

Fixes squizlabs/PHP_CodeSniffer#3453

Suggested changelog entry

  • The PSR12.Files.FileHeader sniff now has more modular error codes to allow for more selectively applying the rules.
    • The PSR12.Files.FileHeader.SpacingAfterBlock error code is replaced by:
      • PSR12.Files.FileHeader.SpacingAfterTagBlock
      • PSR12.Files.FileHeader.SpacingAfterDocblockBlock
      • PSR12.Files.FileHeader.SpacingAfterDeclareBlock
      • PSR12.Files.FileHeader.SpacingAfterNamespaceBlock
      • PSR12.Files.FileHeader.SpacingAfterUseBlock
      • PSR12.Files.FileHeader.SpacingAfterUseFunctionBlock
      • PSR12.Files.FileHeader.SpacingAfterUseConstBlock
    • The PSR12.Files.FileHeader.SpacingInsideBlock error code is replaced by:
      • PSR12.Files.FileHeader.SpacingInsideUseBlock
      • PSR12.Files.FileHeader.SpacingInsideUseFunctionBlock
      • PSR12.Files.FileHeader.SpacingInsideUseConstBlock

@jrfnl jrfnl added this to the 4.0.0 milestone Nov 9, 2023
@jrfnl jrfnl force-pushed the feature/psr12-fileheader-more-modular-errorcode-spacing-after branch 2 times, most recently from 73c72b9 to 43504ca Compare November 11, 2023 03:29
@jrfnl jrfnl changed the base branch from master to 4.0 December 2, 2023 03:17
@jrfnl jrfnl force-pushed the feature/psr12-fileheader-more-modular-errorcode-spacing-after branch from 43504ca to 9049c5c Compare December 2, 2023 03:18
@jrfnl jrfnl changed the title PSR12/FileHeader: make "SpacingAfter" and "SpacingInside" errorcodes modular 4.0 | PSR12/FileHeader: make "SpacingAfter" and "SpacingInside" errorcodes modular Dec 2, 2023
@jrfnl jrfnl force-pushed the feature/psr12-fileheader-more-modular-errorcode-spacing-after branch 2 times, most recently from c718ba3 to 650a507 Compare December 7, 2023 16:03
@jrfnl jrfnl force-pushed the feature/psr12-fileheader-more-modular-errorcode-spacing-after branch from 650a507 to 05662c5 Compare April 15, 2025 16:25
@jrfnl jrfnl changed the base branch from 4.0 to 4.x April 15, 2025 16:27
…modular

This changes the error codes for the `SpacingAfterBlock` and `SpacingInsideBlock` errors to modular error codes which include a reference to the type of header block, i.e. `SpacingAfterDeclareBlock`, `SpacingInsideUseFunctionBlock` etc.

This allows for more selective application of the rules.

Take for instance the quite common case of the header blocks all being separated by blank lines, except for the open tag and file docblock.
```php
<?php
/**
 * File docblock.
 */

namespace A\B\C;

use B\C;
```

The modular errorcodes I'm proposing in this PR will allow for the sniff to be used to safeguard the blank lines between each section without enforcing a blank line between the PHP open tag and the file docblock using:
```xml
<rule ref="PSR12.Files.FileHeader">
   <exclude name="PSR12.Files.FileHeader.SpacingAfterTagBlock"/>
</rule>
```

Fixes #3453
@jrfnl jrfnl force-pushed the feature/psr12-fileheader-more-modular-errorcode-spacing-after branch from 05662c5 to c9c4d05 Compare April 15, 2025 16:27
@jrfnl jrfnl merged commit 37c7d91 into 4.x Apr 15, 2025
52 checks passed
@jrfnl jrfnl deleted the feature/psr12-fileheader-more-modular-errorcode-spacing-after branch April 15, 2025 18:08
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.

[Question] Possible to ignore only one speciifc PSR12 rule?
2 participants