From 03a1f8eb2e03dd9188edfe83a7f202a376024cc9 Mon Sep 17 00:00:00 2001 From: Mike Letellier Date: Fri, 16 Jan 2026 18:38:24 -0400 Subject: [PATCH] new sniff to remove whitspace from start of functions --- .../NoBlankLineAfterFunctionOpenSniff.php | 113 ++++++++++++++++++ phpcs-sniffs/Formidable/ruleset.xml | 1 + 2 files changed, 114 insertions(+) create mode 100644 phpcs-sniffs/Formidable/Sniffs/WhiteSpace/NoBlankLineAfterFunctionOpenSniff.php diff --git a/phpcs-sniffs/Formidable/Sniffs/WhiteSpace/NoBlankLineAfterFunctionOpenSniff.php b/phpcs-sniffs/Formidable/Sniffs/WhiteSpace/NoBlankLineAfterFunctionOpenSniff.php new file mode 100644 index 0000000000..196ed4a35a --- /dev/null +++ b/phpcs-sniffs/Formidable/Sniffs/WhiteSpace/NoBlankLineAfterFunctionOpenSniff.php @@ -0,0 +1,113 @@ +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(); + } + } +} diff --git a/phpcs-sniffs/Formidable/ruleset.xml b/phpcs-sniffs/Formidable/ruleset.xml index cb9f4d3fba..30c5db33bc 100644 --- a/phpcs-sniffs/Formidable/ruleset.xml +++ b/phpcs-sniffs/Formidable/ruleset.xml @@ -10,6 +10,7 @@ +