diff --git a/BigBite/Docs/Objects/NewLineAfterOpeningBraceStandard.xml b/BigBite/Docs/Objects/NewLineAfterOpeningBraceStandard.xml new file mode 100644 index 0000000..33e04fa --- /dev/null +++ b/BigBite/Docs/Objects/NewLineAfterOpeningBraceStandard.xml @@ -0,0 +1,25 @@ + + + + + + + + + public function bar(); +} + ]]> + + + + public function bar(); +} + + ]]> + + + diff --git a/BigBite/Sniffs/Objects/NewLineAfterOpeningBraceSniff.php b/BigBite/Sniffs/Objects/NewLineAfterOpeningBraceSniff.php new file mode 100644 index 0000000..20707b8 --- /dev/null +++ b/BigBite/Sniffs/Objects/NewLineAfterOpeningBraceSniff.php @@ -0,0 +1,84 @@ + + */ + public $supportedTokenizers = array( + 'PHP', + ); + + /** + * Returns an array of tokens this test wants to listen for. + * + * @return array + */ + public function register() { + return array( + \T_CLASS, + \T_ENUM, + \T_INTERFACE, + \T_TRAIT, + ); + } + + /** + * Processes this test, when one of its tokens is encountered. + * + * @param \PHP_CodeSniffer\Files\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(); + + $openingLineNo = $tokens[ $stackPtr ]['line']; + $openingBrace = $tokens[ $stackPtr ]['scope_opener']; + $closingBrace = $tokens[ $stackPtr ]['scope_closer']; + $closingLineNo = $tokens[ $closingBrace ]['line']; + + // Other sniffs will handle objects with empty or malformed bodies. + if ( $openingLineNo === $closingLineNo || ( $openingLineNo + 1 ) === $closingLineNo ) { + return; + } + + $nextContent = $phpcsFile->findNext( \T_WHITESPACE, ( $openingBrace + 1 ), null, true ); + + // Blank line exists between opening brace and content. + if ( ( $tokens[ $openingBrace ]['line'] + 2 ) === $tokens[ $nextContent ]['line'] ) { + return; + } + + $error = 'There must be exactly one blank line between the %s definition and its first content.'; + $data = array( $tokens[ $stackPtr ]['content'] ); + $fix = $phpcsFile->addFixableError( $error, $stackPtr, 'NotFound', $data ); + + if ( true !== $fix ) { + return; + } + + $phpcsFile->fixer->beginChangeset(); + $phpcsFile->fixer->addNewline( $openingBrace ); + $phpcsFile->fixer->endChangeset(); + } +} diff --git a/BigBite/Tests/Objects/NewLineAfterOpeningBraceUnitTest.1.inc b/BigBite/Tests/Objects/NewLineAfterOpeningBraceUnitTest.1.inc new file mode 100644 index 0000000..de50791 --- /dev/null +++ b/BigBite/Tests/Objects/NewLineAfterOpeningBraceUnitTest.1.inc @@ -0,0 +1,20 @@ + + */ + public $supportedTokenizers = array( + 'PHP', + ); + + /** + * Returns an array of tokens this test wants to listen for. + * + * @return array + */ + public function register() { + return array( + \T_CLASS, + \T_ENUM, + \T_INTERFACE, + \T_TRAIT, + ); + } + + /** + * Returns the lines where errors should occur. + * + * The key of the array should represent the line number and the value + * should represent the number of errors that should occur on that line. + * + * @param string $testFile The name of the file being tested. + * + * @return array + */ + public function getErrorList( $testFile = '' ) { + switch ( $testFile ) { + case 'NewLineAfterOpeningBraceUnitTest.1.inc': + return array( + 3 => 1, + 7 => 1, + 13 => 1, + 17 => 1, + ); + default: + return array(); + } + } + + /** + * Returns the lines where warnings should occur. + * + * The key of the array should represent the line number and the value + * should represent the number of warnings that should occur on that line. + * + * @param string $testFile The name of the file being tested. + * + * @return array + */ + public function getWarningList( $testFile = '' ) { + return array(); + } +}