当前位置: 首页>>代码示例>>PHP>>正文


PHP File::isReference方法代码示例

本文整理汇总了PHP中PHP_CodeSniffer\Files\File::isReference方法的典型用法代码示例。如果您正苦于以下问题:PHP File::isReference方法的具体用法?PHP File::isReference怎么用?PHP File::isReference使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在PHP_CodeSniffer\Files\File的用法示例。


在下文中一共展示了File::isReference方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。

示例1: process

 /**
  * 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(File $phpcsFile, $stackPtr)
 {
     $tokens = $phpcsFile->getTokens();
     // Skip default values in function declarations.
     if ($tokens[$stackPtr]['code'] === T_EQUAL || $tokens[$stackPtr]['code'] === T_MINUS) {
         if (isset($tokens[$stackPtr]['nested_parenthesis']) === true) {
             $parenthesis = array_keys($tokens[$stackPtr]['nested_parenthesis']);
             $bracket = array_pop($parenthesis);
             if (isset($tokens[$bracket]['parenthesis_owner']) === true) {
                 $function = $tokens[$bracket]['parenthesis_owner'];
                 if ($tokens[$function]['code'] === T_FUNCTION || $tokens[$function]['code'] === T_CLOSURE) {
                     return;
                 }
             }
         }
     }
     if ($tokens[$stackPtr]['code'] === T_EQUAL) {
         // Skip for '=&' case.
         if (isset($tokens[$stackPtr + 1]) === true && $tokens[$stackPtr + 1]['code'] === T_BITWISE_AND) {
             return;
         }
     }
     // Skip short ternary such as: "$foo = $bar ?: true;".
     if ($tokens[$stackPtr]['code'] === T_INLINE_THEN && $tokens[$stackPtr + 1]['code'] === T_INLINE_ELSE || $tokens[$stackPtr - 1]['code'] === T_INLINE_THEN && $tokens[$stackPtr]['code'] === T_INLINE_ELSE) {
         return;
     }
     if ($tokens[$stackPtr]['code'] === T_BITWISE_AND) {
         // If it's not a reference, then we expect one space either side of the
         // bitwise operator.
         if ($phpcsFile->isReference($stackPtr) === true) {
             return;
         }
         // Check there is one space before the & operator.
         if ($tokens[$stackPtr - 1]['code'] !== T_WHITESPACE) {
             $error = 'Expected 1 space before "&" operator; 0 found';
             $fix = $phpcsFile->addFixableError($error, $stackPtr, 'NoSpaceBeforeAmp');
             if ($fix === true) {
                 $phpcsFile->fixer->addContentBefore($stackPtr, ' ');
             }
             $phpcsFile->recordMetric($stackPtr, 'Space before operator', 0);
         } else {
             if ($tokens[$stackPtr - 2]['line'] !== $tokens[$stackPtr]['line']) {
                 $found = 'newline';
             } else {
                 $found = $tokens[$stackPtr - 1]['length'];
             }
             $phpcsFile->recordMetric($stackPtr, 'Space before operator', $found);
             if ($found !== 1 && ($found !== 'newline' || $this->ignoreNewlines === false)) {
                 $error = 'Expected 1 space before "&" operator; %s found';
                 $data = array($found);
                 $fix = $phpcsFile->addFixableError($error, $stackPtr, 'SpacingBeforeAmp', $data);
                 if ($fix === true) {
                     $phpcsFile->fixer->replaceToken($stackPtr - 1, ' ');
                 }
             }
         }
         //end if
         // Check there is one space after the & operator.
         if ($tokens[$stackPtr + 1]['code'] !== T_WHITESPACE) {
             $error = 'Expected 1 space after "&" operator; 0 found';
             $fix = $phpcsFile->addFixableError($error, $stackPtr, 'NoSpaceAfterAmp');
             if ($fix === true) {
                 $phpcsFile->fixer->addContent($stackPtr, ' ');
             }
             $phpcsFile->recordMetric($stackPtr, 'Space after operator', 0);
         } else {
             if ($tokens[$stackPtr + 2]['line'] !== $tokens[$stackPtr]['line']) {
                 $found = 'newline';
             } else {
                 $found = $tokens[$stackPtr + 1]['length'];
             }
             $phpcsFile->recordMetric($stackPtr, 'Space after operator', $found);
             if ($found !== 1 && ($found !== 'newline' || $this->ignoreNewlines === false)) {
                 $error = 'Expected 1 space after "&" operator; %s found';
                 $data = array($found);
                 $fix = $phpcsFile->addFixableError($error, $stackPtr, 'SpacingAfterAmp', $data);
                 if ($fix === true) {
                     $phpcsFile->fixer->replaceToken($stackPtr + 1, ' ');
                 }
             }
         }
         //end if
         return;
     }
     //end if
     if ($tokens[$stackPtr]['code'] === T_MINUS || $tokens[$stackPtr]['code'] === T_PLUS) {
         // Check minus spacing, but make sure we aren't just assigning
         // a minus value or returning one.
         $prev = $phpcsFile->findPrevious(T_WHITESPACE, $stackPtr - 1, null, true);
         if ($tokens[$prev]['code'] === T_RETURN) {
             // Just returning a negative value; eg. (return -1).
//.........这里部分代码省略.........
开发者ID:thekabal,项目名称:tki,代码行数:101,代码来源:OperatorSpacingSniff.php

示例2: process

 /**
  * 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(File $phpcsFile, $stackPtr)
 {
     $tokens = $phpcsFile->getTokens();
     if ($phpcsFile->tokenizerType === 'JS' && $tokens[$stackPtr]['code'] === T_PLUS) {
         // JavaScript uses the plus operator for string concatenation as well
         // so we cannot accurately determine if it is a string concat or addition.
         // So just ignore it.
         return;
     }
     // If the & is a reference, then we don't want to check for brackets.
     if ($tokens[$stackPtr]['code'] === T_BITWISE_AND && $phpcsFile->isReference($stackPtr) === true) {
         return;
     }
     // There is one instance where brackets aren't needed, which involves
     // the minus sign being used to assign a negative number to a variable.
     if ($tokens[$stackPtr]['code'] === T_MINUS) {
         // Check to see if we are trying to return -n.
         $prev = $phpcsFile->findPrevious(Tokens::$emptyTokens, $stackPtr - 1, null, true);
         if ($tokens[$prev]['code'] === T_RETURN) {
             return;
         }
         $number = $phpcsFile->findNext(T_WHITESPACE, $stackPtr + 1, null, true);
         if ($tokens[$number]['code'] === T_LNUMBER || $tokens[$number]['code'] === T_DNUMBER) {
             $previous = $phpcsFile->findPrevious(T_WHITESPACE, $stackPtr - 1, null, true);
             if ($previous !== false) {
                 $isAssignment = in_array($tokens[$previous]['code'], Tokens::$assignmentTokens);
                 $isEquality = in_array($tokens[$previous]['code'], Tokens::$equalityTokens);
                 $isComparison = in_array($tokens[$previous]['code'], Tokens::$comparisonTokens);
                 if ($isAssignment === true || $isEquality === true || $isComparison === true) {
                     // This is a negative assignment or comparison.
                     // We need to check that the minus and the number are
                     // adjacent.
                     if ($number - $stackPtr !== 1) {
                         $error = 'No space allowed between minus sign and number';
                         $phpcsFile->addError($error, $stackPtr, 'SpacingAfterMinus');
                     }
                     return;
                 }
             }
         }
     }
     //end if
     // Tokens that are allowed inside a bracketed operation.
     $allowed = array(T_VARIABLE, T_LNUMBER, T_DNUMBER, T_STRING, T_WHITESPACE, T_THIS, T_SELF, T_OBJECT_OPERATOR, T_DOUBLE_COLON, T_OPEN_SQUARE_BRACKET, T_CLOSE_SQUARE_BRACKET, T_MODULUS, T_NONE);
     $allowed += Tokens::$operators;
     $lastBracket = false;
     if (isset($tokens[$stackPtr]['nested_parenthesis']) === true) {
         $parenthesis = array_reverse($tokens[$stackPtr]['nested_parenthesis'], true);
         foreach ($parenthesis as $bracket => $endBracket) {
             $prevToken = $phpcsFile->findPrevious(T_WHITESPACE, $bracket - 1, null, true);
             $prevCode = $tokens[$prevToken]['code'];
             if ($prevCode === T_ISSET) {
                 // This operation is inside an isset() call, but has
                 // no bracket of it's own.
                 break;
             }
             if ($prevCode === T_STRING || $prevCode === T_SWITCH) {
                 // We allow simple operations to not be bracketed.
                 // For example, ceil($one / $two).
                 for ($prev = $stackPtr - 1; $prev > $bracket; $prev--) {
                     if (in_array($tokens[$prev]['code'], $allowed) === true) {
                         continue;
                     }
                     if ($tokens[$prev]['code'] === T_CLOSE_PARENTHESIS) {
                         $prev = $tokens[$prev]['parenthesis_opener'];
                     } else {
                         break;
                     }
                 }
                 if ($prev !== $bracket) {
                     break;
                 }
                 for ($next = $stackPtr + 1; $next < $endBracket; $next++) {
                     if (in_array($tokens[$next]['code'], $allowed) === true) {
                         continue;
                     }
                     if ($tokens[$next]['code'] === T_OPEN_PARENTHESIS) {
                         $next = $tokens[$next]['parenthesis_closer'];
                     } else {
                         break;
                     }
                 }
                 if ($next !== $endBracket) {
                     break;
                 }
             }
             //end if
             if (in_array($prevCode, Tokens::$scopeOpeners) === true) {
                 // This operation is inside a control structure like FOREACH
                 // or IF, but has no bracket of it's own.
                 // The only control structure allowed to do this is SWITCH.
//.........这里部分代码省略.........
开发者ID:thekabal,项目名称:tki,代码行数:101,代码来源:OperatorBracketSniff.php


注:本文中的PHP_CodeSniffer\Files\File::isReference方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。