Checks all lines in the file, and throws warnings if they are over 80
characters in length and errors if they are over 100. Both these
figures can be changed by extending this sniff in your own standard.
/** * Checks if a line is too long. * * @param PHP_CodeSniffer_File $phpcsFile The file being scanned. * @param array $tokens The token stack. * @param int $stackPtr The first token on the next line. * * @return void */ protected function checkLineLength(PHP_CodeSniffer_File $phpcsFile, $tokens, $stackPtr) { if (isset(PHP_CodeSniffer_Tokens::$commentTokens[$tokens[$stackPtr - 1]['code']]) === TRUE) { $doc_comment_tag = $phpcsFile->findFirstOnLine(T_DOC_COMMENT_TAG, $stackPtr - 1); if ($doc_comment_tag !== FALSE) { // Allow doc comment tags such as long @param tags to exceed the 80 // character limit. return; } if ($tokens[$stackPtr - 1]['code'] === T_COMMENT && (preg_match('/^[[:space:]]*\\/\\/ @.+/', $tokens[$stackPtr - 1]['content']) === 1 || strpos(trim($tokens[$stackPtr - 1]['content'], "/ \n"), ' ') === FALSE)) { // Allow @link and @see documentation to exceed the 80 character // limit. return; } // Code examples between @code and @endcode are allowed to exceed 80 // characters. if (isset($tokens[$stackPtr]) === TRUE && $tokens[$stackPtr]['code'] === T_DOC_COMMENT_WHITESPACE) { $tag = $phpcsFile->findPrevious(array(T_DOC_COMMENT_TAG, T_DOC_COMMENT_OPEN_TAG), $stackPtr - 1); if ($tokens[$tag]['content'] === '@code') { return; } } // Drupal 8 annotations can have long translatable descriptions and we // allow them to exceed 80 characters. if ($tokens[$stackPtr - 2]['code'] === T_DOC_COMMENT_STRING && strpos($tokens[$stackPtr - 2]['content'], '@Translation(') !== FALSE) { return; } // Allow comments preceded by the line with @code and ended by the line // with @endcode to be excluded. if ($this->isInCodeExample($phpcsFile, $stackPtr) === TRUE) { return; } parent::checkLineLength($phpcsFile, $tokens, $stackPtr); } }
/** * Checks if a line is too long. * * @param PHP_CodeSniffer_File $phpcsFile The file being scanned. * @param int $stackPtr The token at the end of the line. * @param string $lineContent The content of the line. * * @return void */ protected function checkLineLength(PHP_CodeSniffer_File $phpcsFile, $stackPtr, $lineContent) { $tokens = $phpcsFile->getTokens(); if ($tokens[$stackPtr]['code'] == T_DOC_COMMENT || $tokens[$stackPtr]['code'] == T_COMMENT) { return parent::checkLineLength($phpcsFile, $stackPtr, $lineContent); } }
public function process(PHP_CodeSniffer_File $file, $stackptr) { // Lang files are allowed to have long lines. if (strpos($file->getFilename(), DIRECTORY_SEPARATOR . 'lang' . DIRECTORY_SEPARATOR) !== false) { return; } parent::process($file, $stackptr); }
/** * Checks if a line is too long. * * @param PHP_CodeSniffer_File $phpcsFile The file being scanned. * @param array $tokens The token stack. * @param int $stackPtr The first token on the next line. * * @return null|false */ protected function checkLineLength(PHP_CodeSniffer_File $phpcsFile, $tokens, $stackPtr) { //コメントは除外する $str = substr(trim($stackPtr), 0, 2); if (in_array($str, ['/*', '*', '*' . chr(9), '* ', '//', '*/'], true)) { return; } parent::checkLineLength($phpcsFile, $tokens, $stackPtr); }
/** * Checks if a line is too long. * * @param PHP_CodeSniffer_File $phpcsFile The file being scanned. * @param int $stackPtr The token at the end of the line. * @param string $lineContent The content of the line. * * @return void */ protected function checkLineLength(PHP_CodeSniffer_File $phpcsFile, $stackPtr, $lineContent) { $tokens = $phpcsFile->getTokens(); if ($tokens[$stackPtr]['code'] === T_DOC_COMMENT || $tokens[$stackPtr]['code'] === T_COMMENT) { if (preg_match('/^[[:space:]]*(\\/\\*)?\\*[[:space:]]*@link.*@endlink[[:space:]]*/', $lineContent) === 1) { // Allow @link documentation to exceed the 80 character limit. return; } parent::checkLineLength($phpcsFile, $stackPtr, $lineContent); } }
/** * {@inheritdoc} */ protected function checkLineLength(\PHP_CodeSniffer_File $phpcsFile, $stackPtr, $lineContent) { $previousLineRegexp = '~__\\($|\\bPhrase\\($~'; $currentLineRegexp = '~__\\(.+\\)|\\bPhrase\\(.+\\)~'; $currentLineMatch = preg_match($currentLineRegexp, $lineContent) !== 0; $previousLineMatch = preg_match($previousLineRegexp, $this->previousLineContent) !== 0; $this->previousLineContent = $lineContent; if (!$currentLineMatch && !$previousLineMatch) { parent::checkLineLength($phpcsFile, $stackPtr, $lineContent); } }
/** * Checks if a line is too long. * * @param PHP_CodeSniffer_File $phpcsFile The file being scanned. * @param array $tokens The token stack. * @param int $stackPtr The first token on the next line. * * @return void */ protected function checkLineLength(PHP_CodeSniffer_File $phpcsFile, $tokens, $stackPtr) { if (isset(PHP_CodeSniffer_Tokens::$commentTokens[$tokens[$stackPtr - 1]['code']]) === true) { $doc_comment_tag = $phpcsFile->findFirstOnLine(T_DOC_COMMENT_TAG, $stackPtr - 1); if ($doc_comment_tag !== false) { // Allow doc comment tags such as long @param tags to exceed the 80 // character limit. return; } if ($tokens[$stackPtr - 1]['code'] === T_COMMENT && preg_match('/^[[:space:]]*\\/\\/ @.+/', $tokens[$stackPtr - 1]['content']) === 1) { // Allow @link and @see documentation to exceed the 80 character // limit. return; } parent::checkLineLength($phpcsFile, $tokens, $stackPtr); } }