Verifies that operators have valid spacing surrounding them.
Author: Greg Sherwood (gsherwood@squiz.net)
Author: Marc McIntyre (mmcintyre@squiz.net)
Inheritance: implements PHP_CodeSniffer_Sniff
Example #1
0
 /**
  * Processes this test, when one of its tokens is encountered.
  *
  * @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 process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
 {
     $tokens = $phpcsFile->getTokens();
     // Ignore colons in switch() constructs.
     if (empty($tokens[$stackPtr]['conditions']) === false) {
         $condition = array_pop($tokens[$stackPtr]['conditions']);
         if ($condition === T_SWITCH) {
             return;
         }
     }
     // Reuse the standard operator sniff now.
     $sniff = new Squiz_Sniffs_WhiteSpace_OperatorSpacingSniff();
     $sniff->process($phpcsFile, $stackPtr);
 }
Example #2
0
 /**
  * Processes this test, when one of its tokens is encountered.
  *
  * @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 process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
 {
     $tokens = $phpcsFile->getTokens();
     $operator = $tokens[$stackPtr]['content'];
     // Handle the short ternary operator (?:) introduced in PHP 5.3.
     $previous = $phpcsFile->findPrevious(T_WHITESPACE, $stackPtr - 1, null, true);
     if ($tokens[$previous]['code'] === T_INLINE_THEN) {
         if ($previous !== $stackPtr - 1) {
             $error = 'There must be no space between ? and :';
             $phpcsFile->addError($error, $stackPtr, 'SpaceInlineElse');
         }
         if ($tokens[$stackPtr + 1]['code'] !== T_WHITESPACE) {
             $error = "Expected 1 space after \"{$operator}\"; 0 found";
             $phpcsFile->addError($error, $stackPtr, 'NoSpaceAfter');
         } else {
             if (strlen($tokens[$stackPtr + 1]['content']) !== 1) {
                 $found = strlen($tokens[$stackPtr + 1]['content']);
                 $error = 'Expected 1 space after "%s"; %s found';
                 $data = array($operator, $found);
                 $phpcsFile->addError($error, $stackPtr, 'SpacingAfter', $data);
             }
         }
         return;
     }
     //end if
     $next = $phpcsFile->findNext(T_WHITESPACE, $stackPtr + 1, null, true);
     if ($tokens[$next]['code'] === T_INLINE_ELSE) {
         if ($tokens[$stackPtr - 1]['code'] !== T_WHITESPACE) {
             $error = "Expected 1 space before \"{$operator}\"; 0 found";
             $phpcsFile->addError($error, $stackPtr, 'NoSpaceBefore');
         } else {
             if (strlen($tokens[$stackPtr - 1]['content']) !== 1) {
                 $found = strlen($tokens[$stackPtr - 1]['content']);
                 $error = 'Expected 1 space before "%s"; %s found';
                 $data = array($operator, $found);
                 $phpcsFile->addError($error, $stackPtr, 'SpacingBefore', $data);
             }
         }
         return;
     }
     // Reuse the standard operator sniff now.
     $sniff = new Squiz_Sniffs_WhiteSpace_OperatorSpacingSniff();
     $sniff->process($phpcsFile, $stackPtr);
 }