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


PHP Tokens::overrideRange方法代码示例

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


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

示例1: fix

 /**
  * {@inheritdoc}
  */
 public function fix(\SplFileInfo $file, Tokens $tokensOrg)
 {
     $content = $tokensOrg->generateCode();
     // replace all <? with <?php to replace all short open tags even without short_open_tag option enabled
     $newContent = preg_replace('/<\\?(\\s|$)/', '<?php$1', $content, -1, $count);
     if (!$count) {
         return;
     }
     /* the following code is magic to revert previous replacements which should NOT be replaced, for example incorrectly replacing
      * > echo '<? ';
      * with
      * > echo '<?php ';
      */
     $tokens = Tokens::fromCode($newContent);
     $tokensOldContent = '';
     $tokensOldContentLength = 0;
     foreach ($tokens as $token) {
         if ($token->isGivenKind(T_OPEN_TAG)) {
             $tokenContent = $token->getContent();
             if ('<?php' !== substr($content, $tokensOldContentLength, 5)) {
                 $tokenContent = '<? ';
             }
             $tokensOldContent .= $tokenContent;
             $tokensOldContentLength += strlen($tokenContent);
             continue;
         }
         if ($token->isGivenKind(array(T_COMMENT, T_DOC_COMMENT, T_CONSTANT_ENCAPSED_STRING, T_ENCAPSED_AND_WHITESPACE, T_STRING))) {
             $tokenContent = '';
             $tokenContentLength = 0;
             $parts = explode('<?php', $token->getContent());
             $iLast = count($parts) - 1;
             foreach ($parts as $i => $part) {
                 $tokenContent .= $part;
                 $tokenContentLength += strlen($part);
                 if ($i !== $iLast) {
                     if ('<?php' === substr($content, $tokensOldContentLength + $tokenContentLength, 5)) {
                         $tokenContent .= '<?php';
                         $tokenContentLength += 5;
                     } else {
                         $tokenContent .= '<?';
                         $tokenContentLength += 2;
                     }
                 }
             }
             $token->setContent($tokenContent);
         }
         $tokensOldContent .= $token->getContent();
         $tokensOldContentLength += strlen($token->getContent());
     }
     $tokensOrg->overrideRange(0, $tokensOrg->count() - 1, $tokens);
 }
开发者ID:infinitely-young,项目名称:PHP-CS-Fixer,代码行数:54,代码来源:FullOpeningTagFixer.php

示例2: overrideAttribs

 /**
  * Apply token attributes.
  *
  * Token at given index is prepended by attributes.
  *
  * @param Tokens $tokens      Tokens collection
  * @param int    $memberIndex token index
  * @param array  $attribs     map of grabbed attributes, key is attribute name and value is array of index and clone of Token
  */
 private function overrideAttribs(Tokens $tokens, $memberIndex, array $attribs)
 {
     $toOverride = array();
     $firstAttribIndex = $memberIndex;
     foreach ($attribs as $attrib) {
         if (null === $attrib) {
             continue;
         }
         if (null !== $attrib['index']) {
             $firstAttribIndex = min($firstAttribIndex, $attrib['index']);
         }
         if (!$attrib['token']->isGivenKind(T_VAR) && '' !== $attrib['token']->getContent()) {
             $toOverride[] = $attrib['token'];
             $toOverride[] = new Token(array(T_WHITESPACE, ' '));
         }
     }
     if (!empty($toOverride)) {
         $tokens->overrideRange($firstAttribIndex, $memberIndex - 1, $toOverride);
     }
 }
开发者ID:cryode,项目名称:PHP-CS-Fixer,代码行数:29,代码来源:VisibilityRequiredFixer.php

示例3: sortTokens

 /**
  * @param Tokens  $tokens
  * @param int     $startIndex
  * @param int     $endIndex
  * @param array[] $elements
  */
 private function sortTokens(Tokens $tokens, $startIndex, $endIndex, array $elements)
 {
     $replaceTokens = array();
     foreach ($elements as $element) {
         for ($i = $element['start']; $i <= $element['end']; ++$i) {
             $replaceTokens[] = clone $tokens[$i];
         }
     }
     $tokens->overrideRange($startIndex + 1, $endIndex, $replaceTokens);
 }
开发者ID:fabpot,项目名称:php-cs-fixer,代码行数:16,代码来源:OrderedClassElementsFixer.php


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