Chris@0: getTokens(); Chris@0: $errorData = array(strtolower($tokens[$stackPtr]['content'])); Chris@0: Chris@0: if (isset($tokens[$stackPtr]['scope_opener']) === false) { Chris@0: $error = 'Possible parse error: %s missing opening or closing brace'; Chris@0: $phpcsFile->addWarning($error, $stackPtr, 'MissingBrace', $errorData); Chris@0: return; Chris@0: } Chris@0: Chris@0: $openingBrace = $tokens[$stackPtr]['scope_opener']; Chris@0: Chris@0: $next = $phpcsFile->findNext(T_WHITESPACE, ($openingBrace + 1), null, true); Chris@0: if ($tokens[$next]['line'] === $tokens[$openingBrace]['line'] && $tokens[$next]['code'] !== T_CLOSE_CURLY_BRACKET) { Chris@0: $error = 'Opening brace must be the last content on the line'; Chris@0: $fix = $phpcsFile->addFixableError($error, $openingBrace, 'ContentAfterBrace'); Chris@0: if ($fix === true) { Chris@0: $phpcsFile->fixer->addNewline($openingBrace); Chris@0: } Chris@0: } Chris@0: Chris@0: $previous = $phpcsFile->findPrevious(T_WHITESPACE, ($openingBrace - 1), null, true); Chris@0: $decalrationLine = $tokens[$previous]['line']; Chris@0: $braceLine = $tokens[$openingBrace]['line']; Chris@0: Chris@0: $lineDifference = ($braceLine - $decalrationLine); Chris@0: Chris@0: if ($lineDifference > 0) { Chris@0: $error = 'Opening brace should be on the same line as the declaration'; Chris@0: $fix = $phpcsFile->addFixableError($error, $openingBrace, 'BraceOnNewLine'); Chris@0: if ($fix === true) { Chris@0: $phpcsFile->fixer->beginChangeset(); Chris@0: for ($i = ($previous + 1); $i < $openingBrace; $i++) { Chris@0: $phpcsFile->fixer->replaceToken($i, ''); Chris@0: } Chris@0: Chris@0: $phpcsFile->fixer->addContent($previous, ' '); Chris@0: $phpcsFile->fixer->endChangeset(); Chris@0: } Chris@0: Chris@0: return; Chris@0: } Chris@0: Chris@0: $openingBrace = $tokens[$stackPtr]['scope_opener']; Chris@0: if ($tokens[($openingBrace - 1)]['code'] !== T_WHITESPACE) { Chris@0: $length = 0; Chris@0: } else if ($tokens[($openingBrace - 1)]['content'] === "\t") { Chris@0: $length = '\t'; Chris@0: } else { Chris@0: $length = strlen($tokens[($openingBrace - 1)]['content']); Chris@0: } Chris@0: Chris@0: if ($length !== 1) { Chris@0: $error = 'Expected 1 space before opening brace; found %s'; Chris@0: $data = array($length); Chris@0: $fix = $phpcsFile->addFixableError($error, $openingBrace, 'SpaceBeforeBrace', $data); Chris@0: if ($fix === true) { Chris@0: if ($length === 0) { Chris@0: $phpcsFile->fixer->replaceToken(($openingBrace), ' {'); Chris@0: } else { Chris@0: $phpcsFile->fixer->replaceToken(($openingBrace - 1), ' '); Chris@0: } Chris@0: } Chris@0: } Chris@0: Chris@0: // Now call the open spacing method from PSR2. Chris@0: $this->processOpen($phpcsFile, $stackPtr); Chris@0: Chris@0: $this->processClose($phpcsFile, $stackPtr); Chris@0: Chris@0: }//end process() Chris@0: Chris@0: Chris@0: /** Chris@0: * Processes the closing section of a class declaration. Chris@0: * Chris@17: * @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned. Chris@17: * @param int $stackPtr The position of the current token Chris@17: * in the stack passed in $tokens. Chris@0: * Chris@0: * @return void Chris@0: */ Chris@17: public function processClose(File $phpcsFile, $stackPtr) Chris@0: { Chris@0: $tokens = $phpcsFile->getTokens(); Chris@0: Chris@0: // Just in case. Chris@0: if (isset($tokens[$stackPtr]['scope_closer']) === false) { Chris@0: return; Chris@0: } Chris@0: Chris@0: // Check that the closing brace comes right after the code body. Chris@0: $closeBrace = $tokens[$stackPtr]['scope_closer']; Chris@0: $prevContent = $phpcsFile->findPrevious(T_WHITESPACE, ($closeBrace - 1), null, true); Chris@0: if ($prevContent !== $tokens[$stackPtr]['scope_opener'] Chris@0: && $tokens[$prevContent]['line'] !== ($tokens[$closeBrace]['line'] - 2) Chris@0: // If the class only contains a comment no extra line is needed. Chris@17: && isset(Tokens::$commentTokens[$tokens[$prevContent]['code']]) === false Chris@0: ) { Chris@0: $error = 'The closing brace for the %s must have an empty line before it'; Chris@0: $data = array($tokens[$stackPtr]['content']); Chris@0: $fix = $phpcsFile->addFixableError($error, $closeBrace, 'CloseBraceAfterBody', $data); Chris@0: Chris@0: if ($fix === true) { Chris@0: $phpcsFile->fixer->beginChangeset(); Chris@0: for ($i = ($prevContent + 1); $i < $closeBrace; $i++) { Chris@0: $phpcsFile->fixer->replaceToken($i, ''); Chris@0: } Chris@0: Chris@0: $phpcsFile->fixer->replaceToken($closeBrace, $phpcsFile->eolChar.$phpcsFile->eolChar.$tokens[$closeBrace]['content']); Chris@0: Chris@0: $phpcsFile->fixer->endChangeset(); Chris@0: } Chris@0: }//end if Chris@0: Chris@0: // Check the closing brace is on it's own line, but allow Chris@0: // for comments like "//end class". Chris@0: $nextContent = $phpcsFile->findNext(T_COMMENT, ($closeBrace + 1), null, true); Chris@0: if ($tokens[$nextContent]['content'] !== $phpcsFile->eolChar Chris@0: && $tokens[$nextContent]['line'] === $tokens[$closeBrace]['line'] Chris@0: ) { Chris@0: $type = strtolower($tokens[$stackPtr]['content']); Chris@0: $error = 'Closing %s brace must be on a line by itself'; Chris@0: $data = array($tokens[$stackPtr]['content']); Chris@0: $phpcsFile->addError($error, $closeBrace, 'CloseBraceSameLine', $data); Chris@0: } Chris@0: Chris@0: }//end processClose() Chris@0: Chris@0: Chris@0: }//end class