Code Coverage
 
Classes and Traits
Functions and Methods
Lines
Total
0.00% covered (danger)
0.00%
0 / 1
50.00% covered (danger)
50.00%
1 / 2
CRAP
96.00% covered (success)
96.00%
24 / 25
DisallowEmptyLineFunctionsSniff
0.00% covered (danger)
0.00%
0 / 1
50.00% covered (danger)
50.00%
1 / 2
8
96.00% covered (success)
96.00%
24 / 25
 register
100.00% covered (success)
100.00%
1 / 1
1
100.00% covered (success)
100.00%
2 / 2
 process
0.00% covered (danger)
0.00%
0 / 1
7
95.65% covered (success)
95.65%
22 / 23
<?php
/**
 * Disallow empty line at the begin of function.
 */
namespace MediaWiki\Sniffs\WhiteSpace;
use PHP_CodeSniffer\Files\File;
use PHP_CodeSniffer\Sniffs\Sniff;
class DisallowEmptyLineFunctionsSniff implements Sniff {
    /**
     * @inheritDoc
     */
    public function register() : array {
        return [
            T_FUNCTION,
            T_CLOSURE,
        ];
    }
    /**
     * @param File $phpcsFile
     * @param int $stackPtr The current token index.
     * @return void
     */
    public function process( File $phpcsFile, $stackPtr ) {
        $tokens = $phpcsFile->getTokens();
        $current = $tokens[$stackPtr];
        if ( !isset( $current['scope_opener'] ) ||
            !isset( $current['parenthesis_closer'] )
        ) {
            return;
        }
        $openBrace = $current['scope_opener'];
        $next = $phpcsFile->findNext( T_WHITESPACE, $openBrace + 1, null, true );
        if ( $next === false ) {
            return;
        }
        if ( $tokens[$next]['line'] > $tokens[$openBrace]['line'] + 1 ) {
            $fix = $phpcsFile->addFixableError(
                'Unexpected empty line at the begin of function.',
                $stackPtr,
                'NoEmptyLine'
            );
            if ( $fix ) {
                $phpcsFile->fixer->beginChangeset();
                $i = $openBrace + 1;
                while ( $tokens[$i]['line'] !== $tokens[$next]['line'] ) {
                    $phpcsFile->fixer->replaceToken( $i, '' );
                    $i++;
                }
                $phpcsFile->fixer->addNewlineBefore( $i );
                $phpcsFile->fixer->endChangeset();
            }
        }
    }
}