當前位置: 首頁>>代碼示例>>PHP>>正文


PHP PHP_CodeSniffer_File::gettokens方法代碼示例

本文整理匯總了PHP中PHP_CodeSniffer_File::gettokens方法的典型用法代碼示例。如果您正苦於以下問題:PHP PHP_CodeSniffer_File::gettokens方法的具體用法?PHP PHP_CodeSniffer_File::gettokens怎麽用?PHP PHP_CodeSniffer_File::gettokens使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在PHP_CodeSniffer_File的用法示例。


在下文中一共展示了PHP_CodeSniffer_File::gettokens方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的PHP代碼示例。

示例1: 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(PHP_CodeSniffer_File $phpcsfile, $stackptr)
 {
     $tokens = $phpcsfile->gettokens();
     $token = $tokens[$stackptr];
     // Skip invalid statement.
     if (isset($token['parenthesis_opener']) === false) {
         return;
     }
     $next = ++$token['parenthesis_opener'];
     $end = --$token['parenthesis_closer'];
     $parts = array(0, 0, 0);
     $index = 0;
     for (; $next <= $end; ++$next) {
         $code = $tokens[$next]['code'];
         if ($code === T_SEMICOLON) {
             ++$index;
         } else {
             if (in_array($code, PHP_CodeSniffer_tokens::$emptyTokens) === false) {
                 ++$parts[$index];
             }
         }
     }
     if ($parts[0] === 0 && $parts[2] === 0 && $parts[1] > 0) {
         $error = 'This FOR loop can be simplified to a WHILE loop';
         $phpcsfile->addwarning($error, $stackptr);
     }
 }
開發者ID:vuchannguyen,項目名稱:web,代碼行數:36,代碼來源:ForLoopShouldBeWhileLoopSniff.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(PHP_CodeSniffer_File $phpcsfile, $stackptr)
 {
     $tokens = $phpcsfile->gettokens();
     $token = $tokens[$stackptr];
     // Skip for-loop without body.
     if (isset($token['scope_opener']) === false) {
         return;
     }
     // Find incrementors for outer loop.
     $outer = $this->findincrementers($tokens, $token);
     // Skip if empty.
     if (count($outer) === 0) {
         return;
     }
     // Find nested for loops.
     $start = ++$token['scope_opener'];
     $end = --$token['scope_closer'];
     for (; $start <= $end; ++$start) {
         if ($tokens[$start]['code'] !== T_FOR) {
             continue;
         }
         $inner = $this->findincrementers($tokens, $tokens[$start]);
         $diff = array_intersect($outer, $inner);
         if (count($diff) !== 0) {
             $error = sprintf('Loop incrementor (%s) jumbling with inner loop', join(', ', $diff));
             $phpcsfile->addwarning($error, $stackptr);
         }
     }
 }
開發者ID:vuchannguyen,項目名稱:web,代碼行數:38,代碼來源:JumbledIncrementerSniff.php

示例3: 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(PHP_CodeSniffer_File $phpcsfile, $stackptr)
 {
     $tokens = $phpcsfile->gettokens();
     if (isset($tokens[$stackptr]['scope_opener']) === false) {
         // Ignore the ELSE in ELSE IF. We'll process the IF part later.
         if ($tokens[$stackptr]['code'] === T_ELSE && $tokens[$stackptr + 2]['code'] === T_IF) {
             return;
         }
         if ($tokens[$stackptr]['code'] === T_WHILE) {
             // This could be from a DO WHILE, which doesn't have an opening brace.
             $lastcontent = $phpcsfile->findprevious(T_WHITESPACE, $stackptr - 1, null, true);
             if ($tokens[$lastcontent]['code'] === T_CLOSE_CURLY_BRACKET) {
                 $brace = $tokens[$lastcontent];
                 if (isset($brace['scope_condition']) === true) {
                     $condition = $tokens[$brace['scope_condition']];
                     if ($condition['code'] === T_DO) {
                         return;
                     }
                 }
             }
         }
         // This is a control structure without an opening brace,
         // so it is an inline statement.
         if ($this->error === true) {
             $phpcsfile->adderror('Inline control structures are not allowed', $stackptr);
         } else {
             $phpcsfile->addwarning('Inline control structures are discouraged', $stackptr);
         }
         return;
     }
 }
開發者ID:vuchannguyen,項目名稱:web,代碼行數:40,代碼來源:InlineControlStructureSniff.php

示例4: 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(PHP_CodeSniffer_File $phpcsfile, $stackptr)
 {
     $tokens = $phpcsfile->gettokens();
     $workingstring = $tokens[$stackptr]['content'];
     // Check if it's a double quoted string.
     if (strpos($workingstring, '"') === false) {
         return;
     }
     // Make sure it's not a part of a string started above.
     // If it is, then we have already checked it.
     if ($workingstring[0] !== '"') {
         return;
     }
     // Work through the following tokens, in case this string is stretched
     // over multiple Lines.
     for ($i = $stackptr + 1; $i < $phpcsfile->numTokens; $i++) {
         if ($tokens[$i]['type'] !== 'T_CONSTANT_ENCAPSED_STRING') {
             break;
         }
         $workingstring .= $tokens[$i]['content'];
     }
     $allowedchars = array('\\n', '\\r', '\\f', '\\t', '\\v', '\\x', '\'', '$');
     foreach ($allowedchars as $testchar) {
         if (strpos($workingstring, $testchar) !== false) {
             return;
         }
     }
     $error = "String {$workingstring} does not require double quotes; use single quotes instead";
     $phpcsfile->addwarning($error, $stackptr);
 }
開發者ID:vuchannguyen,項目名稱:web,代碼行數:39,代碼來源:DoubleQuoteUsageSniff.php

示例5: 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(PHP_CodeSniffer_File $phpcsfile, $stackptr)
 {
     $tokens = $phpcsfile->gettokens();
     $token = $tokens[$stackptr];
     // Skip for-statements without body.
     if (isset($token['scope_opener']) === false) {
         return;
     }
     $next = ++$token['scope_opener'];
     $end = --$token['scope_closer'];
     $emptybody = true;
     for (; $next <= $end; ++$next) {
         if (in_array($tokens[$next]['code'], PHP_CodeSniffer_tokens::$emptyTokens) === false) {
             $emptybody = false;
             break;
         }
     }
     if ($emptybody === true) {
         // Get token identifier.
         $name = $phpcsfile->gettokensAsString($stackptr, 1);
         $error = sprintf('Empty %s statement detected', strtoupper($name));
         if ($this->_tokens[$token['code']] === true) {
             $phpcsfile->adderror($error, $stackptr);
         } else {
             $phpcsfile->addwarning($error, $stackptr);
         }
     }
 }
開發者ID:vuchannguyen,項目名稱:web,代碼行數:37,代碼來源:EmptyStatementSniff.php

示例6: 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(PHP_CodeSniffer_File $phpcsfile, $stackptr)
 {
     $tokens = $phpcsfile->gettokens();
     if (isset($tokens[$stackptr]['scope_opener']) === false) {
         $error = 'possible parse error: ';
         $error .= $tokens[$stackptr]['content'];
         $error .= ' missing opening or closing brace';
         $phpcsfile->addwarning($error, $stackptr);
         return;
     }
     $curlybrace = $tokens[$stackptr]['scope_opener'];
     $lastcontent = $phpcsfile->findprevious(T_WHITESPACE, $curlybrace - 1, $stackptr, true);
     $classline = $tokens[$lastcontent]['line'];
     $braceline = $tokens[$curlybrace]['line'];
     if ($braceline != $classline) {
         $error = 'Opening brace of a ';
         $error .= $tokens[$stackptr]['content'];
         $error .= ' must be on the same line as the definition';
         $phpcsfile->adderror($error, $curlybrace);
         return;
     }
     if ($tokens[$curlybrace - 1]['code'] === T_WHITESPACE) {
         $prevcontent = $tokens[$curlybrace - 1]['content'];
         if ($prevcontent !== $phpcsfile->eolChar) {
             $blankspace = substr($prevcontent, strpos($prevcontent, $phpcsfile->eolChar));
             $spaces = strlen($blankspace);
             if ($spaces !== 1) {
                 $error = "Expected 1 space before opening brace; {$spaces} found";
                 $phpcsfile->adderror($error, $curlybrace);
             }
         }
     }
 }
開發者ID:vuchannguyen,項目名稱:web,代碼行數:42,代碼來源:ClassDeclarationSniff.php

示例7: 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(PHP_CodeSniffer_File $phpcsfile, $stackptr)
 {
     $tokens = $phpcsfile->gettokens();
     $token = $tokens[$stackptr];
     // Skip for-loop without body.
     if (isset($token['parenthesis_opener']) === false) {
         return;
     }
     $next = ++$token['parenthesis_opener'];
     $end = --$token['parenthesis_closer'];
     $goodcondition = false;
     for (; $next <= $end; ++$next) {
         $code = $tokens[$next]['code'];
         if (in_array($code, PHP_CodeSniffer_tokens::$emptyTokens) === true) {
             continue;
         } else {
             if ($code !== T_TRUE && $code !== T_FALSE) {
                 $goodcondition = true;
             }
         }
     }
     if ($goodcondition === false) {
         $error = 'Avoid IF statements that are always true or false';
         $phpcsfile->addwarning($error, $stackptr);
     }
 }
開發者ID:vuchannguyen,項目名稱:web,代碼行數:35,代碼來源:UnconditionalIfStatementSniff.php

示例8: 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(PHP_CodeSniffer_File $phpcsfile, $stackptr)
 {
     $tokens = $phpcsfile->gettokens();
     //$nexttoken = $phpcsfile->findnext(PHP_CodeSniffer_tokens::$emptyTokens, ($stackptr + 1), null, true);
     //if ($tokens[$nexttoken]['code'] === T_OPEN_PARENTHESIS) {
     //    $error  = '"'.$tokens[$stackptr]['content'].'"';
     //    $error .= ' is a statement, not a function; ';
     //    $error .= 'no parentheses are required';
     //    $phpcsfile->adderror($error, $stackptr);
     //}
     $incondition = count($tokens[$stackptr]['conditions']) !== 0 ? true : false;
     // Check to see if this including statement is within the parenthesis of a condition.
     // If that's the case then we need to process it as being within a condition, as they
     // are checking the return value.
     if (isset($tokens[$stackptr]['nested_parenthesis']) === true) {
         foreach ($tokens[$stackptr]['nested_parenthesis'] as $left => $right) {
             if (isset($tokens[$left]['parenthesis_owner']) === true) {
                 $incondition = true;
             }
         }
     }
     // Check to see if they are assigning the return value of this including call.
     // If they are then they are probably checking it, so its conditional.
     $previous = $phpcsfile->findprevious(PHP_CodeSniffer_tokens::$emptyTokens, $stackptr - 1, null, true);
     if (in_array($tokens[$previous]['code'], PHP_CodeSniffer_tokens::$assignmentTokens) === true) {
         // The have assigned the return value to it, so its conditional.
         $incondition = true;
     }
     $tokencode = $tokens[$stackptr]['code'];
     if ($incondition === true) {
         // We are inside a conditional statement. We need an include_once.
         if ($tokencode === T_REQUIRE_ONCE) {
             $error = 'File is being conditionally included; ';
             $error .= 'use "include_once" instead';
             $phpcsfile->adderror($error, $stackptr);
         } else {
             if ($tokencode === T_REQUIRE) {
                 $error = 'File is being conditionally included; ';
                 $error .= 'use "include" instead';
                 $phpcsfile->adderror($error, $stackptr);
             }
         }
     } else {
         // We are unconditionally including, we need a require_once.
         if ($tokencode === T_INCLUDE_ONCE) {
             $error = 'File is being unconditionally included; ';
             $error .= 'use "require_once" instead';
             $phpcsfile->adderror($error, $stackptr);
         } else {
             if ($tokencode === T_INCLUDE) {
                 $error = 'File is being unconditionally included; ';
                 $error .= 'use "require" instead';
                 $phpcsfile->adderror($error, $stackptr);
             }
         }
     }
 }
開發者ID:vuchannguyen,項目名稱:web,代碼行數:66,代碼來源:IncludingFileSniff.php

示例9: 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(PHP_CodeSniffer_File $phpcsfile, $stackptr)
 {
     $tokens = $phpcsfile->gettokens();
     $last_token = end($tokens);
     if ($last_token['code'] === T_CLOSE_TAG) {
         $error = 'Closing PHP tag is not required at the end of the file. Please remove.';
         $phpcsfile->addwarning($error, $stackptr);
     }
 }
開發者ID:vuchannguyen,項目名稱:web,代碼行數:18,代碼來源:ClosingPHPTagSniff.php

示例10: process

 /**
  * Processes this sniff, 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();
     $keyword = $tokens[$stackptr]['content'];
     if (strtolower($keyword) !== $keyword) {
         $error = 'TRUE, FALSE and NULL must be lowercase; expected "' . strtolower($keyword) . '" but found "' . $keyword . '"';
         $phpcsfile->adderror($error, $stackptr);
     }
 }
開發者ID:vuchannguyen,項目名稱:web,代碼行數:18,代碼來源:LowerCaseConstantSniff.php

示例11: 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(PHP_CodeSniffer_File $phpcsfile, $stackptr)
 {
     $tokens = $phpcsfile->gettokens();
     if ($tokens[$stackptr]['content'][0] === '#') {
         $error = 'Perl-style comments are not allowed. Use "// Comment."';
         $error .= ' or "/* comment */" instead.';
         $phpcsfile->adderror($error, $stackptr);
     }
 }
開發者ID:vuchannguyen,項目名稱:web,代碼行數:18,代碼來源:InlineCommentSniff.php

示例12: 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(PHP_CodeSniffer_File $phpcsfile, $stackptr)
 {
     $tokens = $phpcsfile->gettokens();
     // Find the next non-empty token.
     $next = $phpcsfile->findnext(PHP_CodeSniffer_tokens::$emptyTokens, $stackptr + 1, null, true);
     if ($tokens[$next]['code'] !== T_OPEN_PARENTHESIS) {
         // Not a function call.
         return;
     }
     if (isset($tokens[$next]['parenthesis_closer']) === false) {
         // Not a function call.
         return;
     }
     // Find the previous non-empty token.
     $previous = $phpcsfile->findprevious(PHP_CodeSniffer_tokens::$emptyTokens, $stackptr - 1, null, true);
     if ($tokens[$previous]['code'] === T_FUNCTION) {
         // It's a function definition, not a function call.
         return;
     }
     if ($tokens[$previous]['code'] === T_NEW) {
         // We are creating an object, not calling a function.
         return;
     }
     if ($stackptr + 1 !== $next) {
         // Checking this: $value = my_function[*](...).
         $error = 'Space before opening parenthesis of function call prohibited';
         $phpcsfile->adderror($error, $stackptr);
     }
     if ($tokens[$next + 1]['code'] === T_WHITESPACE) {
         // Checking this: $value = my_function([*]...).
         $error = 'Space after opening parenthesis of function call prohibited';
         $phpcsfile->adderror($error, $stackptr);
     }
     $closer = $tokens[$next]['parenthesis_closer'];
     if ($tokens[$closer - 1]['code'] === T_WHITESPACE) {
         // Checking this: $value = my_function(...[*]).
         $between = $phpcsfile->findnext(T_WHITESPACE, $next + 1, null, true);
         // Only throw an error if there is some content between the parenthesis.
         // IE. Checking for this: $value = my_function().
         // If there is no content, then we would have thrown an error in the
         // previous IF statement because it would look like this:
         // $value = my_function( ).
         if ($between !== $closer) {
             $error = 'Space before closing parenthesis of function call prohibited';
             $phpcsfile->adderror($error, $closer);
         }
     }
     $next = $phpcsfile->findnext(T_WHITESPACE, $closer + 1, null, true);
     if ($tokens[$next]['code'] === T_SEMICOLON) {
         if (in_array($tokens[$closer + 1]['code'], PHP_CodeSniffer_tokens::$emptyTokens) === true) {
             $error = 'Space after closing parenthesis of function call prohibited';
             $phpcsfile->adderror($error, $closer);
         }
     }
 }
開發者ID:vuchannguyen,項目名稱:web,代碼行數:64,代碼來源:FunctionCallSignatureSniff.php

示例13: process

 /**
  * Processes this test, when one of its tokens is encountered.
  *
  * @param PHP_CodeSniffer_File $phpcsfile The current file being processed.
  * @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();
     $classname = $phpcsfile->findnext(T_STRING, $stackptr);
     $name = trim($tokens[$classname]['content']);
     // Make sure that the word is all lowercase
     if (!preg_match('/[a-z]?/', $name)) {
         $error = ucfirst($tokens[$stackptr]['content']) . ' name is not valid, must be all lower-case';
         $phpcsfile->adderror($error, $stackptr);
     }
 }
開發者ID:vuchannguyen,項目名稱:web,代碼行數:20,代碼來源:ValidClassNameSniff.php

示例14: 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(PHP_CodeSniffer_File $phpcsfile, $stackptr)
 {
     $tokens = $phpcsfile->gettokens();
     $content = $tokens[$stackptr]['content'];
     if ($content !== strtolower($content)) {
         $type = strtoupper($content);
         $expected = strtolower($content);
         $error = "{$type} keyword must be lowercase; expected \"{$expected}\" but found \"{$content}\"";
         $phpcsfile->adderror($error, $stackptr);
     }
 }
開發者ID:vuchannguyen,項目名稱:web,代碼行數:20,代碼來源:LowercaseFunctionKeywordsSniff.php

示例15: 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(PHP_CodeSniffer_File $phpcsfile, $stackptr)
 {
     $tokens = $phpcsfile->gettokens();
     if ($tokens[$stackptr + 1]['code'] !== T_WHITESPACE) {
         $error = 'A cast statement must be followed by a single space';
         $phpcsfile->adderror($error, $stackptr);
         return;
     }
     if ($tokens[$stackptr + 1]['content'] !== ' ') {
         $error = 'A cast statement must be followed by a single space';
         $phpcsfile->adderror($error, $stackptr);
     }
 }
開發者ID:vuchannguyen,項目名稱:web,代碼行數:22,代碼來源:SpaceAfterCastSniff.php


注:本文中的PHP_CodeSniffer_File::gettokens方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。