Author: Greg Sherwood (gsherwood@squiz.net)
Author: Marc McIntyre (mmcintyre@squiz.net)
Inheritance: extends PHP_CodeSniffer_Standards_AbstractVariableSniff
Ejemplo n.º 1
0
 /**
  * @param PHP_CodeSniffer_File $phpcsFile
  * @param int                  $stackPtr
  */
 public function processMemberVar(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
 {
     $tokens = $phpcsFile->getTokens();
     $commentToken = array(T_DOC_COMMENT_CLOSE_TAG);
     $commentEnd = $phpcsFile->findPrevious($commentToken, $stackPtr);
     if ($commentEnd !== false) {
         $commentStart = $tokens[$commentEnd]['comment_opener'];
         if ($tokens[$commentStart]['line'] == $tokens[$commentEnd]['line']) {
             $error = 'The variable doc block shouldn\'t be on one line';
             $phpcsFile->addError($error, $commentStart, 'OneLiner');
         }
     }
     parent::processMemberVar($phpcsFile, $stackPtr);
 }
Ejemplo n.º 2
0
 /**
  * Called to process class member vars.
  *
  * @param PHP_CodeSniffer_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 processMemberVar(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
 {
     parent::processMemberVar($phpcsFile, $stackPtr);
     // Wikidi
     $tokens = $phpcsFile->getTokens();
     $start = $phpcsFile->findPrevious(T_DOC_COMMENT_OPEN_TAG, $stackPtr - 1);
     $end = $phpcsFile->findNext(T_DOC_COMMENT_CLOSE_TAG, $start) + 1;
     // include last */
     $multiline = $tokens[$start]['line'] !== $tokens[$end]['line'];
     $comment = $phpcsFile->getTokensAsString($start, $end - $start);
     $description = $phpcsFile->findNext(T_DOC_COMMENT_STRING, $start);
     if ($multiline) {
         // seach for empty lines in comment
         if (preg_match("/(^[\r\n]*|[\r\n]+)[\\s\t]*[\r\n]+/", $comment)) {
             $phpcsFile->addError('Remove blank lines from comment', $start, 'WrongStyle');
             return;
         }
         // empty new lines in comment description not allowed
         $shorten = null;
         for ($i = $start + 1; $i < $tokens[$start]['comment_tags'][0]; $i++) {
             if (!in_array($tokens[$i]['type'], ['T_DOC_COMMENT_STAR'])) {
                 $shorten .= $tokens[$i]['content'];
             }
         }
         if (preg_match("/(^[\r\n]*|[\r\n]+)[\\s\t]*[\r\n]+/", trim($shorten))) {
             $phpcsFile->addError('Remove blank lines from comment description', $start, 'WrongStyle');
             return;
         }
         // exact one single line expect between description and tag line
         if (preg_match("/\n\\s*\n\\s*\n\\s*\$/", $shorten)) {
             $phpcsFile->addError('Exact one empty line expected after description', $description, 'WrongStyle');
             return;
         }
         /*
         // @var musí být první a poslední zároveň ===> nemůžeme použít @deprecated @see atd... což je IMHO špatně
         // search for @var
         $var = null;
         foreach ($tokens[$start]['comment_tags'] as $tag) {
         	if ($tokens[$tag]['content'] === '@var') {
         		$var = $tag;
         	}
         }
         
         // Tag must be on the last line
         $star = $phpcsFile->findPrevious(T_DOC_COMMENT_STAR, $end - 1);
         if ($var['type'] !== 'T_DOC_COMMENT_TAG') {
         	$phpcsFile->addError('Last line need contains comment tag (@var, @depreceted etc.)', $star, 'WrongStyle');
         	return;
         }
         */
         // Comment string need to be present on first line
         $star = $phpcsFile->findNext(T_DOC_COMMENT_STAR, $start);
         // first star after comment start
         $firstStar = $phpcsFile->findNext(T_DOC_COMMENT_STAR, $star);
         $x = $phpcsFile->getTokensAsString($star, $firstStar - $star);
         if ($tokens[$star]['line'] !== $tokens[$description]['line']) {
             $phpcsFile->addError('Comment string need to be on first line', $star, 'WrongStyle');
             return;
         }
         // description and @var only one line allowed between
     }
 }