/** * Processes single-line declarations. * * Just uses the Generic BSD-Allman brace sniff. * * @param PHP_CodeSniffer_File $phpcsFile The file being scanned. * @param int $stackPtr The position of the current token * in the stack passed in $tokens. * @param array $tokens The stack of tokens that make up * the file. * * @return void */ public function processSingleLineDeclaration(PHP_CodeSniffer_File $phpcsFile, $stackPtr, $tokens) { $openBracket = $tokens[$stackPtr]['parenthesis_opener']; $closeBracket = $tokens[$openBracket]['parenthesis_closer']; if ($openBracket === $closeBracket - 1) { return; } $spaceAfterOpen = 0; if ($tokens[$openBracket + 1]['code'] === T_WHITESPACE) { $spaceAfterOpen = strlen($tokens[$openBracket + 1]['content']); } if ($spaceAfterOpen !== 1) { $error = 'Expected 1 space after opening bracket; %s found'; $data = array($spaceAfterOpen); $phpcsFile->addError($error, $stackPtr, 'SpaceAfterOpenBracket', $data); } $spaceBeforeClose = 0; if ($tokens[$closeBracket - 1]['code'] === T_WHITESPACE) { $spaceBeforeClose = strlen($tokens[$closeBracket - 1]['content']); } if ($spaceBeforeClose !== 1) { $error = 'Expected 1 space before closing bracket; %s found'; $data = array($spaceBeforeClose); $phpcsFile->addError($error, $stackPtr, 'SpaceBeforeCloseBracket', $data); } if ($openBracket === $closeBracket - $spaceAfterOpen - 1) { $error = 'Without parameters, space are forbidden in function declaration'; $phpcsFile->addError($error, $stackPtr, 'SpaceBetweenBrackets'); } parent::processSingleLineDeclaration($phpcsFile, $stackPtr, $tokens); }
/** * Processes mutli-line declarations. * * @param PHP_CodeSniffer_File $phpcsFile The file being scanned. * @param int $stackPtr The position of the current token * in the stack passed in $tokens. * @param array $tokens The stack of tokens that make up * the file. * * @return void */ public function processMultiLineDeclaration(PHP_CodeSniffer_File $phpcsFile, $stackPtr, $tokens) { // We do everything the parent sniff does, and a bit more. parent::processMultiLineDeclaration($phpcsFile, $stackPtr, $tokens); $openBracket = $tokens[$stackPtr]['parenthesis_opener']; $closeBracket = $tokens[$stackPtr]['parenthesis_closer']; $hasUse = false; if ($tokens[$stackPtr]['code'] === T_CLOSURE) { $use = $phpcsFile->findNext(T_USE, $closeBracket + 1, $tokens[$stackPtr]['scope_opener']); if ($use !== false) { $hasUse = true; } } // The open bracket should be the last thing on the line. if ($hasUse === false) { $next = $phpcsFile->findNext(T_WHITESPACE, $openBracket + 1, null, true); if ($tokens[$next]['line'] !== $tokens[$openBracket]['line'] + 1) { $error = 'The first parameter of a multi-line function declaration must be on the line after the opening bracket'; $phpcsFile->addError($error, $next, 'FirstParamSpacing'); } } // Each line between the brackets should contain a single parameter. $lastCommaLine = null; for ($i = $openBracket + 1; $i < $closeBracket; $i++) { // Skip brackets, like arrays, as they can contain commas. if (isset($tokens[$i]['parenthesis_opener']) === true) { $i = $tokens[$i]['parenthesis_closer']; continue; } if ($tokens[$i]['code'] === T_COMMA) { if ($lastCommaLine !== null && $lastCommaLine === $tokens[$i]['line']) { $error = 'Multi-line function declarations must define one parameter per line'; $phpcsFile->addError($error, $i, 'OneParamPerLine'); } else { // Comma must be the last thing on the line. $next = $phpcsFile->findNext(T_WHITESPACE, $i + 1, null, true); if ($tokens[$next]['line'] !== $tokens[$i]['line'] + 1) { $error = 'Commas in multi-line function declarations must be the last content on a line'; $phpcsFile->addError($error, $next, 'ContentAfterComma'); } } $lastCommaLine = $tokens[$i]['line']; } } }
/** * Processes multi-line declarations. * * @param PHP_CodeSniffer_File $phpcsFile The file being scanned. * @param int $stackPtr The position of the current token * in the stack passed in $tokens. * @param array $tokens The stack of tokens that make up * the file. * * @return void */ public function processMultiLineDeclaration(PHP_CodeSniffer_File $phpcsFile, $stackPtr, $tokens) { // We do everything the parent sniff does, and a bit more. parent::processMultiLineDeclaration($phpcsFile, $stackPtr, $tokens); $openBracket = $tokens[$stackPtr]['parenthesis_opener']; $this->processBracket($phpcsFile, $openBracket, $tokens, 'function'); if ($tokens[$stackPtr]['code'] !== T_CLOSURE) { return; } $use = $phpcsFile->findNext(T_USE, $tokens[$stackPtr]['parenthesis_closer'] + 1, $tokens[$stackPtr]['scope_opener']); if ($use === false) { return; } $openBracket = $phpcsFile->findNext(T_OPEN_PARENTHESIS, $use + 1, null); $this->processBracket($phpcsFile, $openBracket, $tokens, 'use'); // Also check spacing. if ($tokens[$use - 1]['code'] === T_WHITESPACE) { $gap = strlen($tokens[$use - 1]['content']); } else { $gap = 0; } }