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


PHP Tokens::fromArray方法代码示例

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


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

示例1: getNewOrder

 private function getNewOrder(array $uses, Tokens $tokens)
 {
     $indexes = array();
     $originalIndexes = array();
     for ($i = count($uses) - 1; $i >= 0; --$i) {
         $index = $uses[$i];
         $startIndex = $tokens->getTokenNotOfKindSibling($index + 1, 1, array(array(T_WHITESPACE)));
         $endIndex = $tokens->getNextTokenOfKind($startIndex, array(';', array(T_CLOSE_TAG)));
         $previous = $tokens->getPrevMeaningfulToken($endIndex);
         $group = $tokens[$previous]->isGivenKind(CT::T_GROUP_IMPORT_BRACE_CLOSE);
         if ($tokens[$startIndex]->isGivenKind(array(CT::T_CONST_IMPORT))) {
             $type = self::IMPORT_TYPE_CONST;
         } elseif ($tokens[$startIndex]->isGivenKind(array(CT::T_FUNCTION_IMPORT))) {
             $type = self::IMPORT_TYPE_FUNCTION;
         } else {
             $type = self::IMPORT_TYPE_CLASS;
         }
         $namespaceTokens = array();
         $index = $startIndex;
         while ($index <= $endIndex) {
             $token = $tokens[$index];
             if ($index === $endIndex || !$group && $token->equals(',')) {
                 if ($group) {
                     // if group import, sort the items within the group definition
                     // figure out where the list of namespace parts within the group def. starts
                     $namespaceTokensCount = count($namespaceTokens) - 1;
                     $namespace = '';
                     for ($k = 0; $k < $namespaceTokensCount; ++$k) {
                         if ($namespaceTokens[$k]->isGivenKind(CT::T_GROUP_IMPORT_BRACE_OPEN)) {
                             $namespace .= '{';
                             break;
                         }
                         $namespace .= $namespaceTokens[$k]->getContent();
                     }
                     // fetch all parts, split up in an array of strings, move comments to the end
                     $parts = array();
                     for ($k1 = $k + 1; $k1 < $namespaceTokensCount; ++$k1) {
                         $comment = '';
                         $namespacePart = '';
                         for ($k2 = $k1;; ++$k2) {
                             if ($namespaceTokens[$k2]->equalsAny(array(',', array(CT::T_GROUP_IMPORT_BRACE_CLOSE)))) {
                                 break;
                             }
                             if ($namespaceTokens[$k2]->isComment()) {
                                 $comment .= $namespaceTokens[$k2]->getContent();
                                 continue;
                             }
                             $namespacePart .= $namespaceTokens[$k2]->getContent();
                         }
                         $namespacePart = trim($namespacePart);
                         $comment = trim($comment);
                         if ('' !== $comment) {
                             $namespacePart .= ' ' . $comment;
                         }
                         $parts[] = $namespacePart . ', ';
                         $k1 = $k2;
                     }
                     $sortedParts = $parts;
                     sort($parts);
                     // check if the order needs to be updated, otherwise don't touch as we might change valid CS (to other valid CS).
                     if ($sortedParts === $parts) {
                         $namespace = Tokens::fromArray($namespaceTokens)->generateCode();
                     } else {
                         $namespace .= substr(implode('', $parts), 0, -2) . '}';
                     }
                 } else {
                     $namespace = Tokens::fromArray($namespaceTokens)->generateCode();
                 }
                 $indexes[$startIndex] = array('namespace' => $namespace, 'startIndex' => $startIndex, 'endIndex' => $index - 1, 'importType' => $type, 'group' => $group);
                 $originalIndexes[] = $startIndex;
                 if ($index === $endIndex) {
                     break;
                 }
                 $namespaceTokens = array();
                 $nextPartIndex = $tokens->getTokenNotOfKindSibling($index, 1, array(array(','), array(T_WHITESPACE)));
                 $startIndex = $nextPartIndex;
                 $index = $nextPartIndex;
                 continue;
             }
             $namespaceTokens[] = $token;
             ++$index;
         }
     }
     uasort($indexes, 'self::sortingCallBack');
     $index = -1;
     $usesOrder = array();
     // Loop trough the index but use original index order
     foreach ($indexes as $v) {
         $usesOrder[$originalIndexes[++$index]] = $v;
     }
     return $usesOrder;
 }
开发者ID:friendsofphp,项目名称:php-cs-fixer,代码行数:92,代码来源:OrderedImportsFixer.php


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