/**
  * Processes this sniff, when one of its tokens is encountered.
  *
  * @param PHP_CodeSniffer_File $phpcsFile The current file being checked.
  * @param int $stackPtr The position of the current token in the stack passed in $tokens.
  *
  * @return void
  */
 public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
 {
     $tokens = $phpcsFile->getTokens();
     $arrayStart = DWS_Helpers_Bracket::bracketStart($phpcsFile, $stackPtr);
     $arrayEnd = DWS_Helpers_Bracket::bracketEnd($phpcsFile, $stackPtr);
     if (!in_array($arrayStart, [$stackPtr, $stackPtr + 1])) {
         $phpcsFile->addError('No whitespace allowed between the array keyword and the opening parenthesis', $stackPtr, 'SpaceAfterKeyword');
     }
     $firstMember = $phpcsFile->findNext(T_WHITESPACE, $arrayStart + 1, $arrayEnd, true);
     if ($firstMember === false && $arrayEnd - $arrayStart !== 1) {
         $phpcsFile->addError('Empty array declaration must have no space between the brackets', $stackPtr, 'SpaceInEmptyArray');
     }
 }
コード例 #2
0
 /**
  * Returns the positions of all of the commas that belong to the array beginning at $arrayStart.
  *
  * @param PHP_CodeSniffer_File $phpcsFile The file where the array is declared.
  * @param int $arrayStart The position of the opening parenthesis or bracket for the array in the file.
  *
  * @return array The stack pointers for all of the commas in the array (excluding commas in nested arrays, functions, etc.).
  */
 public static function commaPositions(PHP_CodeSniffer_File $phpcsFile, $arrayStart)
 {
     $tokens = $phpcsFile->getTokens();
     $arrayEnd = DWS_Helpers_Bracket::bracketEnd($phpcsFile, $arrayStart);
     $commas = [];
     for ($i = $arrayStart + 1; $i <= $arrayEnd; $i++) {
         if (array_key_exists('parenthesis_closer', $tokens[$i])) {
             $i = $tokens[$i]['parenthesis_closer'];
         } elseif (array_key_exists('bracket_closer', $tokens[$i])) {
             $i = $tokens[$i]['bracket_closer'];
         } elseif ($tokens[$i]['code'] === T_COMMA) {
             $commas[] = $i;
         }
     }
     return $commas;
 }
コード例 #3
0
 /**
  * Processes this sniff, when one of its tokens is encountered.
  *
  * @param PHP_CodeSniffer_File $phpcsFile The current file being checked.
  * @param int $stackPtr The position of the current token in the stack passed in $tokens.
  *
  * @return void
  */
 public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
 {
     $tokens = $phpcsFile->getTokens();
     $arrayStart = DWS_Helpers_Bracket::bracketStart($phpcsFile, $stackPtr);
     $arrayEnd = DWS_Helpers_Bracket::bracketEnd($phpcsFile, $stackPtr);
     $isSingleLine = $tokens[$arrayStart]['line'] === $tokens[$arrayEnd]['line'];
     $commas = DWS_Helpers_Array::commaPositions($phpcsFile, $arrayStart);
     $lastComma = array_pop($commas);
     $trailingComma = $phpcsFile->findNext(PHP_CodeSniffer_Tokens::$emptyTokens, $lastComma + 1, $arrayEnd, true) === false;
     if ($isSingleLine) {
         if ($trailingComma) {
             $phpcsFile->addError('No trailing comma allowed on single-line arrays', $lastComma, 'SingleLineTrailingComma');
         }
     } elseif (!$trailingComma) {
         $previousItem = $phpcsFile->findPrevious(PHP_CodeSniffer_Tokens::$emptyTokens, $arrayEnd - 1, $arrayStart, true);
         $phpcsFile->addError('Trailing comma required for multi-line arrays', $previousItem, 'MultiLineTrailingComma');
     }
 }
 /**
  * Processes this sniff, when one of its tokens is encountered.
  *
  * @param PHP_CodeSniffer_File $phpcsFile The current file being checked.
  * @param int $stackPtr The position of the current token in the stack passed in $tokens.
  *
  * @return void
  */
 public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
 {
     $tokens = $phpcsFile->getTokens();
     $bracketStart = DWS_Helpers_Bracket::bracketStart($phpcsFile, $stackPtr);
     $bracketEnd = DWS_Helpers_Bracket::bracketEnd($phpcsFile, $stackPtr);
     if ($tokens[$bracketStart]['line'] === $tokens[$bracketEnd]['line']) {
         return;
         // Single-line expressions don't have indentation to worry about.
     }
     $beginningIndent = DWS_Helpers_WhiteSpace::indentOfLine($phpcsFile, $bracketStart);
     if ($tokens[$bracketEnd]['column'] !== $beginningIndent) {
         $data = [$beginningIndent - 1, $tokens[$bracketEnd]['column'] - 1, $tokens[$bracketEnd]['content']];
         $phpcsFile->addError('Expected indentation of %s spaces, %s found for ending bracket "%s"', $bracketEnd, 'EndIndent', $data);
     }
     $nextToken = $phpcsFile->findNext(PHP_CodeSniffer_Tokens::$emptyTokens, $bracketStart + 1, $bracketEnd, true);
     if ($nextToken !== false && $tokens[$nextToken]['line'] === $tokens[$bracketStart]['line']) {
         $data = [$tokens[$bracketStart]['content']];
         $phpcsFile->addError('Expected first item after opening "%s" to be indented on a new line', $nextToken, 'OpeningIndent', $data);
     }
     $firstOnLine = $bracketStart + 1;
     for ($line = $tokens[$bracketStart]['line'] + 1; $line < $tokens[$bracketEnd]['line']; $line++) {
         while ($tokens[$firstOnLine]['line'] < $line || in_array($tokens[$firstOnLine]['code'], PHP_CodeSniffer_Tokens::$emptyTokens)) {
             if (array_key_exists('parenthesis_closer', $tokens[$firstOnLine])) {
                 $firstOnLine = $tokens[$firstOnLine]['parenthesis_closer'] + 1;
                 $line = $tokens[$firstOnLine]['line'];
                 continue 2;
             } elseif (array_key_exists('bracket_closer', $tokens[$firstOnLine])) {
                 $firstOnLine = $tokens[$firstOnLine]['bracket_closer'] + 1;
                 $line = $tokens[$firstOnLine]['line'];
                 continue 2;
             }
             $firstOnLine++;
         }
         if ($tokens[$firstOnLine]['line'] === $line && $tokens[$firstOnLine]['column'] !== $beginningIndent + 4) {
             $data = [$beginningIndent + 3, $tokens[$firstOnLine]['column'] - 1];
             $phpcsFile->addError('Expected indentation of %s spaces, %s found', $firstOnLine, 'MultiLineIndent', $data);
         }
     }
 }
コード例 #5
0
 /**
  * Processes this sniff, when one of its tokens is encountered.
  *
  * @param PHP_CodeSniffer_File $phpcsFile The current file being checked.
  * @param int $stackPtr The position of the current token in the stack passed in $tokens.
  *
  * @return void
  */
 public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
 {
     $tokens = $phpcsFile->getTokens();
     $arrayStart = DWS_Helpers_Bracket::bracketStart($phpcsFile, $stackPtr);
     $arrayEnd = DWS_Helpers_Bracket::bracketEnd($phpcsFile, $stackPtr);
     $isSingleLine = $tokens[$arrayStart]['line'] === $tokens[$arrayEnd]['line'];
     foreach (DWS_Helpers_Array::commaPositions($phpcsFile, $arrayStart) as $comma) {
         if ($tokens[$comma - 1]['code'] === T_WHITESPACE) {
             $phpcsFile->addError('No whitespace allowed before commas in an array', $comma, 'SpaceBeforeComma');
         }
         if ($isSingleLine) {
             if ($tokens[$comma + 1]['content'] !== ' ') {
                 $phpcsFile->addError('Expected 1 space after comma in single-line array', $comma, 'SingleLineSpaceAfterComma');
             }
         } elseif ($tokens[$comma + 1]['content'][0] !== "\n") {
             $nextMember = $phpcsFile->findNext([T_WHITESPACE, T_COMMENT], $comma + 1, $arrayEnd, true);
             if ($nextMember !== false && $tokens[$nextMember]['line'] === $tokens[$comma]['line']) {
                 $phpcsFile->addError('Comma in multi-line array was not last token on line', $comma, 'MultiLineNewlineAfterComma');
             }
         }
     }
 }