findNextEffective() public static method

public static findNextEffective ( PHP_CodeSniffer_File $phpcsFile, integer $startPointer, integer | null $endPointer = null ) : integer | null
$phpcsFile PHP_CodeSniffer_File
$startPointer integer search starts at this token, inclusive
$endPointer integer | null search ends at this token, exclusive
return integer | null
Ejemplo n.º 1
0
 /**
  * @param \PHP_CodeSniffer_File $phpcsFile
  * @param integer $anyPointer any pointer type where the search begins from (backwards)
  * @return string|null
  */
 public static function findCurrentNamespaceName(PHP_CodeSniffer_File $phpcsFile, $anyPointer)
 {
     $namespacePointer = $phpcsFile->findPrevious(T_NAMESPACE, $anyPointer);
     if ($namespacePointer === false) {
         return null;
     }
     $namespaceNameStartPointer = TokenHelper::findNextEffective($phpcsFile, $namespacePointer + 1);
     $namespaceNameEndPointer = TokenHelper::findNextExcluding($phpcsFile, TokenHelper::$nameTokenCodes, $namespaceNameStartPointer + 1);
     return TokenHelper::getContent($phpcsFile, $namespaceNameStartPointer, $namespaceNameEndPointer);
 }
 /**
  * @param \PHP_CodeSniffer_File $phpcsFile
  * @param integer $usePointer
  */
 public function process(PHP_CodeSniffer_File $phpcsFile, $usePointer)
 {
     if (UseStatementHelper::isAnonymousFunctionUse($phpcsFile, $usePointer) || UseStatementHelper::isTraitUse($phpcsFile, $usePointer)) {
         return;
     }
     $tokens = $phpcsFile->getTokens();
     $nextTokenPointer = TokenHelper::findNextEffective($phpcsFile, $usePointer + 1);
     $nextToken = $tokens[$nextTokenPointer];
     if ($nextToken['code'] === T_NS_SEPARATOR) {
         $phpcsFile->addError('Use statement cannot start with a backslash', $nextTokenPointer, self::CODE_STARTS_WITH_BACKSLASH);
     }
 }
 /**
  * @param \PHP_CodeSniffer_File $phpcsFile
  * @param integer $openTagPointer
  * @return \SlevomatCodingStandard\Helpers\ReferencedName[] referenced names
  */
 private static function createAllReferencedNames(PHP_CodeSniffer_File $phpcsFile, $openTagPointer)
 {
     $beginSearchAtPointer = $openTagPointer + 1;
     $tokens = $phpcsFile->getTokens();
     $searchTypes = array_merge([T_RETURN_TYPE], TokenHelper::$nameTokenCodes);
     $types = [];
     while (true) {
         $nameStartPointer = $phpcsFile->findNext($searchTypes, $beginSearchAtPointer);
         if ($nameStartPointer === false) {
             break;
         }
         $nameStartToken = $tokens[$nameStartPointer];
         $nameEndPointer = self::findReferencedNameEndPointer($phpcsFile, $nameStartPointer);
         if ($nameEndPointer === null) {
             $beginSearchAtPointer = TokenHelper::findNextExcluding($phpcsFile, array_merge([T_WHITESPACE, T_RETURN_TYPE], TokenHelper::$nameTokenCodes), $nameStartPointer);
             continue;
         }
         $nextTokenAfterEndPointer = TokenHelper::findNextEffective($phpcsFile, $nameEndPointer);
         $previousTokenBeforeStartPointer = TokenHelper::findPreviousEffective($phpcsFile, $nameStartPointer - 1);
         $type = ReferencedName::TYPE_DEFAULT;
         if ($nextTokenAfterEndPointer !== null && $previousTokenBeforeStartPointer !== null) {
             if ($tokens[$nextTokenAfterEndPointer]['code'] === T_OPEN_PARENTHESIS) {
                 if ($tokens[$previousTokenBeforeStartPointer]['code'] !== T_NEW) {
                     $type = ReferencedName::TYPE_FUNCTION;
                 }
             } elseif ($tokens[$nextTokenAfterEndPointer]['code'] !== T_VARIABLE) {
                 if (!in_array($tokens[$previousTokenBeforeStartPointer]['code'], [T_EXTENDS, T_IMPLEMENTS, T_INSTANCEOF, T_USE, T_NEW, T_COLON], true) && !in_array($tokens[$nextTokenAfterEndPointer]['code'], [T_DOUBLE_COLON], true)) {
                     if ($tokens[$previousTokenBeforeStartPointer]['code'] === T_COMMA) {
                         $precedingTokenPointer = TokenHelper::findPreviousExcluding($phpcsFile, array_merge([T_COMMA], TokenHelper::$nameTokenCodes, TokenHelper::$ineffectiveTokenCodes), $previousTokenBeforeStartPointer - 1);
                         if (!in_array($tokens[$precedingTokenPointer]['code'], [T_IMPLEMENTS, T_EXTENDS, T_USE])) {
                             $type = ReferencedName::TYPE_CONSTANT;
                         }
                     } elseif (PHP_VERSION_ID >= 70100 && ($tokens[$previousTokenBeforeStartPointer]['code'] === T_BITWISE_OR || $tokens[$previousTokenBeforeStartPointer]['code'] === T_OPEN_PARENTHESIS)) {
                         $exclude = [T_BITWISE_OR, T_OPEN_PARENTHESIS];
                         $catchPointer = TokenHelper::findPreviousExcluding($phpcsFile, array_merge($exclude, TokenHelper::$nameTokenCodes, TokenHelper::$ineffectiveTokenCodes), $previousTokenBeforeStartPointer - 1);
                         $exclude = [T_BITWISE_OR];
                         $openParenthesisPointer = TokenHelper::findPreviousExcluding($phpcsFile, array_merge($exclude, TokenHelper::$nameTokenCodes, TokenHelper::$ineffectiveTokenCodes), $previousTokenBeforeStartPointer);
                         if ($tokens[$catchPointer]['code'] !== T_CATCH || $tokens[$openParenthesisPointer]['code'] !== T_OPEN_PARENTHESIS) {
                             $type = ReferencedName::TYPE_CONSTANT;
                         }
                     } else {
                         $type = ReferencedName::TYPE_CONSTANT;
                     }
                 }
             }
         }
         $types[] = new ReferencedName(TokenHelper::getContent($phpcsFile, $nameStartPointer, $nameEndPointer), $nameStartPointer, $type);
         $beginSearchAtPointer = $nameEndPointer + 1;
     }
     return $types;
 }
 /**
  * @param \PHP_CodeSniffer_File $phpcsFile
  * @param integer $openTagPointer
  * @return \SlevomatCodingStandard\Helpers\UseStatement[] canonicalName(string) => useStatement(\SlevomatCodingStandard\Helpers\UseStatement)
  */
 public static function getUseStatements(PHP_CodeSniffer_File $phpcsFile, $openTagPointer)
 {
     $names = [];
     $tokens = $phpcsFile->getTokens();
     foreach (self::getUseStatementPointers($phpcsFile, $openTagPointer) as $usePointer) {
         $nextTokenFromUsePointer = TokenHelper::findNextEffective($phpcsFile, $usePointer + 1);
         $type = UseStatement::TYPE_DEFAULT;
         if ($tokens[$nextTokenFromUsePointer]['code'] === T_STRING) {
             if ($tokens[$nextTokenFromUsePointer]['content'] === 'const') {
                 $type = UseStatement::TYPE_CONSTANT;
             } elseif ($tokens[$nextTokenFromUsePointer]['content'] === 'function') {
                 $type = UseStatement::TYPE_FUNCTION;
             }
         }
         $name = self::getNameAsReferencedInClassFromUse($phpcsFile, $usePointer);
         $useStatement = new UseStatement($name, self::getFullyQualifiedTypeNameFromUse($phpcsFile, $usePointer), $usePointer, $type);
         $names[$useStatement->getCanonicalNameAsReferencedInFile()] = $useStatement;
     }
     return $names;
 }
 /**
  * @param \PHP_CodeSniffer_File $phpcsFile
  * @param integer $keywordPointer
  */
 public function process(PHP_CodeSniffer_File $phpcsFile, $keywordPointer)
 {
     $nameStartPointer = TokenHelper::findNextEffective($phpcsFile, $keywordPointer + 1);
     $this->checkReferencedName($phpcsFile, $keywordPointer, $nameStartPointer);
     $tokens = $phpcsFile->getTokens();
     if ($tokens[$keywordPointer]['code'] === T_IMPLEMENTS) {
         $possibleCommaPointer = $keywordPointer + 1;
         while (true) {
             $possibleCommaPointer = TokenHelper::findNextExcluding($phpcsFile, array_merge(TokenHelper::$nameTokenCodes, [T_WHITESPACE]), $possibleCommaPointer);
             if ($possibleCommaPointer !== null) {
                 $possibleCommaToken = $tokens[$possibleCommaPointer];
                 if ($possibleCommaToken['code'] === T_COMMA) {
                     $nameStartPointer = TokenHelper::findNextEffective($phpcsFile, $possibleCommaPointer + 1);
                     $possibleCommaPointer = $this->checkReferencedName($phpcsFile, $keywordPointer, $nameStartPointer);
                     continue;
                 }
             }
             break;
         }
     }
 }
 /**
  * @param PHP_CodeSniffer_File $phpcsFile
  * @param mixed[] $tokens
  * @param mixed[] $classToken
  * @return integer[] string(name) => pointer
  */
 private function getMethods(PHP_CodeSniffer_File $phpcsFile, array $tokens, array $classToken)
 {
     $reportedMethods = [];
     $findMethodsStartTokenPointer = $classToken['scope_opener'] + 1;
     while (($methodTokenPointer = $phpcsFile->findNext(T_FUNCTION, $findMethodsStartTokenPointer, $classToken['scope_closer'])) !== false) {
         $visibilityModifier = $this->findVisibilityModifier($phpcsFile, $tokens, $methodTokenPointer);
         if ($visibilityModifier === null || $visibilityModifier !== T_PRIVATE) {
             $findMethodsStartTokenPointer = $methodTokenPointer + 1;
             continue;
         }
         $namePointer = TokenHelper::findNextEffective($phpcsFile, $methodTokenPointer + 1);
         if ($namePointer === null) {
             $findMethodsStartTokenPointer = $methodTokenPointer + 1;
             continue;
         }
         $methodName = $tokens[$namePointer]['content'];
         if ($methodName !== '__construct') {
             $reportedMethods[$methodName] = $methodTokenPointer;
         }
         $findMethodsStartTokenPointer = $methodTokenPointer + 1;
     }
     return $reportedMethods;
 }