-
Notifications
You must be signed in to change notification settings - Fork 41
new sniff to remove whitspace from start of functions #2827
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
Crabcyborg
merged 1 commit into
master
from
new_sniff_to_remove_whitespace_from_start_of_function
Jan 16, 2026
+114
−0
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
113 changes: 113 additions & 0 deletions
113
phpcs-sniffs/Formidable/Sniffs/WhiteSpace/NoBlankLineAfterFunctionOpenSniff.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,113 @@ | ||
| <?php | ||
| /** | ||
| * Formidable_Sniffs_WhiteSpace_NoBlankLineAfterFunctionOpenSniff | ||
| * | ||
| * Ensures there is no blank line immediately after the opening brace of a function/method. | ||
| * | ||
| * @package Formidable\Sniffs | ||
| */ | ||
|
|
||
| namespace Formidable\Sniffs\WhiteSpace; | ||
|
|
||
| use PHP_CodeSniffer\Sniffs\Sniff; | ||
| use PHP_CodeSniffer\Files\File; | ||
|
|
||
| /** | ||
| * Ensures there is no blank line immediately after the opening brace of a function/method. | ||
| * | ||
| * Bad: | ||
| * function example() { | ||
| * | ||
| * do_something(); | ||
| * } | ||
| * | ||
| * Good: | ||
| * function example() { | ||
| * do_something(); | ||
| * } | ||
| */ | ||
| class NoBlankLineAfterFunctionOpenSniff implements Sniff { | ||
|
|
||
| /** | ||
| * Returns an array of tokens this test wants to listen for. | ||
| * | ||
| * @return array | ||
| */ | ||
| public function register() { | ||
| return array( T_FUNCTION, T_CLOSURE ); | ||
| } | ||
|
|
||
| /** | ||
| * Processes this test, when one of its tokens is encountered. | ||
| * | ||
| * @param File $phpcsFile The file being scanned. | ||
| * @param int $stackPtr The position of the current token in the stack passed in $tokens. | ||
| * | ||
| * @return void | ||
| */ | ||
| public function process( File $phpcsFile, $stackPtr ) { | ||
| $tokens = $phpcsFile->getTokens(); | ||
|
|
||
| // Make sure this function has a scope (curly braces). | ||
| if ( ! isset( $tokens[ $stackPtr ]['scope_opener'] ) ) { | ||
| return; | ||
| } | ||
|
|
||
| $scopeOpener = $tokens[ $stackPtr ]['scope_opener']; | ||
| $scopeCloser = $tokens[ $stackPtr ]['scope_closer']; | ||
|
|
||
| // Find the first non-whitespace token after the opening brace. | ||
| $firstContent = $phpcsFile->findNext( T_WHITESPACE, $scopeOpener + 1, $scopeCloser, true ); | ||
|
|
||
| if ( false === $firstContent ) { | ||
| // Empty function body. | ||
| return; | ||
| } | ||
|
|
||
| $openerLine = $tokens[ $scopeOpener ]['line']; | ||
| $contentLine = $tokens[ $firstContent ]['line']; | ||
|
|
||
| // Check if there's a blank line between the opener and first content. | ||
| // A blank line means the content is more than 1 line after the opener. | ||
| if ( $contentLine <= $openerLine + 1 ) { | ||
| // No blank line, content is on the next line or same line. | ||
| return; | ||
| } | ||
|
|
||
| $fix = $phpcsFile->addFixableError( | ||
| 'No blank line should follow the opening brace of a function.', | ||
| $scopeOpener, | ||
| 'BlankLineAfterFunctionOpen' | ||
| ); | ||
|
|
||
| if ( true === $fix ) { | ||
| $phpcsFile->fixer->beginChangeset(); | ||
|
|
||
| // Find the whitespace token right after the opener that contains the blank line. | ||
| $nextToken = $scopeOpener + 1; | ||
|
|
||
| if ( $tokens[ $nextToken ]['code'] === T_WHITESPACE ) { | ||
| $content = $tokens[ $nextToken ]['content']; | ||
| $newlineCount = substr_count( $content, "\n" ); | ||
|
|
||
| if ( $newlineCount >= 2 ) { | ||
| // Multiple newlines - reduce to single newline + indentation. | ||
| $lastNewline = strrpos( $content, "\n" ); | ||
| $indent = substr( $content, $lastNewline + 1 ); | ||
| $phpcsFile->fixer->replaceToken( $nextToken, "\n" . $indent ); | ||
| } elseif ( $newlineCount === 1 ) { | ||
| // Single newline here, but there might be another whitespace token creating the blank. | ||
| // Check if the next token is also whitespace with a newline. | ||
| $afterNext = $nextToken + 1; | ||
|
|
||
| if ( $afterNext < $firstContent && $tokens[ $afterNext ]['code'] === T_WHITESPACE ) { | ||
| // Remove this extra whitespace token. | ||
| $phpcsFile->fixer->replaceToken( $afterNext, '' ); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| $phpcsFile->fixer->endChangeset(); | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.