Source for file OpeningBraceSameLineSniff.php
Documentation is available at OpeningBraceSameLineSniff.php
* Checks that the opening brace of a class/interface/trait is on the same line as the class declaration.
* @author Greg Sherwood <gsherwood@squiz.net>
* @copyright 2006-2015 Squiz Pty Ltd (ABN 77 084 670 600)
* @license https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
namespace PHP_CodeSniffer\Standards\Generic\Sniffs\Classes;
use PHP_CodeSniffer\Sniffs\Sniff;
use PHP_CodeSniffer\Files\File;
class OpeningBraceSameLineSniff implements Sniff
* Returns an array of tokens this test wants to listen for.
public function register ()
* 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.
public function process (File $phpcsFile, $stackPtr)
$tokens = $phpcsFile->getTokens ();
$scopeIdentifier = $phpcsFile->findNext (T_STRING , ($stackPtr + 1 ));
$errorData = array (strtolower($tokens[$stackPtr]['content']). ' '. $tokens[$scopeIdentifier]['content']);
if (isset ($tokens[$stackPtr]['scope_opener']) === false ) {
$error = 'Possible parse error: %s missing opening or closing brace';
$phpcsFile->addWarning ($error, $stackPtr, 'MissingBrace', $errorData);
$openingBrace = $tokens[$stackPtr]['scope_opener'];
// Is the brace on the same line as the class/interface/trait declaration ?
$lastClassLineToken = $phpcsFile->findPrevious (T_WHITESPACE , ($openingBrace - 1 ), $stackPtr, true );
$lastClassLine = $tokens[$lastClassLineToken]['line'];
$braceLine = $tokens[$openingBrace]['line'];
$lineDifference = ($braceLine - $lastClassLine);
if ($lineDifference > 0 ) {
$phpcsFile->recordMetric ($stackPtr, 'Class opening brace placement', 'new line');
$error = 'Opening brace should be on the same line as the declaration for %s';
$fix = $phpcsFile->addFixableError ($error, $openingBrace, 'BraceOnNewLine', $errorData);
$phpcsFile->fixer ->beginChangeset ();
$phpcsFile->fixer ->addContent ($lastClassLineToken, ' {');
$phpcsFile->fixer ->replaceToken ($openingBrace, '');
$phpcsFile->fixer ->endChangeset ();
$phpcsFile->recordMetric ($stackPtr, 'Class opening brace placement', 'same line');
// Is the opening brace the last thing on the line ?
$next = $phpcsFile->findNext (T_WHITESPACE , ($openingBrace + 1 ), null , true );
if ($tokens[$next]['line'] === $tokens[$openingBrace]['line']) {
if ($next === $tokens[$stackPtr]['scope_closer']) {
$error = 'Opening brace must be the last content on the line';
$fix = $phpcsFile->addFixableError ($error, $openingBrace, 'ContentAfterBrace');
$phpcsFile->fixer ->addNewline ($openingBrace);
// Only continue checking if the opening brace looks good.
if ($lineDifference > 0 ) {
// Is there precisely one space before the opening brace ?
if ($tokens[($openingBrace - 1 )]['code'] !== T_WHITESPACE ) {
} else if ($tokens[($openingBrace - 1 )]['content'] === "\t") {
$length = strlen($tokens[($openingBrace - 1 )]['content']);
$error = 'Expected 1 space before opening brace; found %s';
$fix = $phpcsFile->addFixableError ($error, $openingBrace, 'SpaceBeforeBrace', $data);
if ($length === 0 || $length === '\t') {
$phpcsFile->fixer ->addContentBefore ($openingBrace, ' ');
$phpcsFile->fixer ->replaceToken (($openingBrace - 1 ), ' ');
Documentation generated on Mon, 11 Mar 2019 15:27:40 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.
|