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


PHP CS\Utils类代码示例

本文整理汇总了PHP中Symfony\CS\Utils的典型用法代码示例。如果您正苦于以下问题:PHP Utils类的具体用法?PHP Utils怎么用?PHP Utils使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。


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

示例1: fix

 /**
  * {@inheritdoc}
  */
 public function fix(\SplFileInfo $file, Tokens $tokens)
 {
     $tokensAnalyzer = new TokensAnalyzer($tokens);
     foreach ($tokensAnalyzer->getImportUseIndexes() as $index) {
         $indent = '';
         // if previous line ends with comment and current line starts with whitespace, use current indent
         if ($tokens[$index - 1]->isWhitespace(" \t") && $tokens[$index - 2]->isGivenKind(T_COMMENT)) {
             $indent = $tokens[$index - 1]->getContent();
         } elseif ($tokens[$index - 1]->isWhitespace()) {
             $indent = Utils::calculateTrailingWhitespaceIndent($tokens[$index - 1]);
         }
         $newline = "\n";
         // Handle insert index for inline T_COMMENT with whitespace after semicolon
         $semicolonIndex = $tokens->getNextTokenOfKind($index, array(';', '{'));
         $insertIndex = $semicolonIndex + 1;
         if ($tokens[$insertIndex]->isWhitespace(" \t") && $tokens[$insertIndex + 1]->isComment()) {
             ++$insertIndex;
         }
         // Increment insert index for inline T_COMMENT or T_DOC_COMMENT
         if ($tokens[$insertIndex]->isComment()) {
             ++$insertIndex;
         }
         $afterSemicolon = $tokens->getNextMeaningfulToken($semicolonIndex);
         if (!$tokens[$afterSemicolon]->isGivenKind(T_USE)) {
             $newline .= "\n";
         }
         if ($tokens[$insertIndex]->isWhitespace()) {
             $nextToken = $tokens[$insertIndex];
             $nextToken->setContent($newline . $indent . ltrim($nextToken->getContent()));
         } elseif ($newline && $indent) {
             $tokens->insertAt($insertIndex, new Token(array(T_WHITESPACE, $newline . $indent)));
         }
     }
 }
开发者ID:rafwlaz,项目名称:PHP-CS-Fixer,代码行数:37,代码来源:SingleLineAfterImportsFixer.php

示例2: fix

 public function fix(\SplFileInfo $file, $content)
 {
     $tokens = Tokens::fromCode($content);
     foreach ($tokens->getImportUseIndexes() as $index) {
         $indent = '';
         if ($tokens[$index - 1]->isWhitespace(array('whitespaces' => " \t")) && $tokens[$index - 2]->isGivenKind(T_COMMENT)) {
             $indent = $tokens[$index - 1]->getContent();
         } elseif ($tokens[$index - 1]->isWhitespace()) {
             $indent = Utils::calculateTrailingWhitespaceIndent($tokens[$index - 1]);
         }
         $newline = "\n";
         $semicolonIndex = $tokens->getNextTokenOfKind($index, array(';', '{'));
         $insertIndex = $semicolonIndex + 1;
         if ($tokens[$insertIndex]->isWhitespace(array('whitespaces' => " \t")) && $tokens[$insertIndex + 1]->isComment()) {
             ++$insertIndex;
         }
         if ($tokens[$insertIndex]->isGivenKind(T_COMMENT)) {
             $newline = '';
         }
         if ($tokens[$insertIndex]->isComment()) {
             ++$insertIndex;
         }
         $afterSemicolon = $tokens->getNextMeaningfulToken($semicolonIndex);
         if (!$tokens[$afterSemicolon]->isGivenKind(T_USE)) {
             $newline .= "\n";
         }
         if ($tokens[$insertIndex]->isWhitespace()) {
             $nextToken = $tokens[$insertIndex];
             $nextToken->setContent($newline . $indent . ltrim($nextToken->getContent()));
         } else {
             $tokens->insertAt($insertIndex, new Token(array(T_WHITESPACE, $newline . $indent)));
         }
     }
     return $tokens->generateCode();
 }
开发者ID:Ryu0621,项目名称:SaNaVi,代码行数:35,代码来源:SingleLineAfterImportsFixer.php

示例3: fixWhitespace

 private function fixWhitespace(Token $token)
 {
     $content = $token->getContent();
     if (substr_count($content, "\n") > 1) {
         $lines = Utils::splitLines($content);
         $token->setContent("\n" . end($lines));
     }
 }
开发者ID:Ryu0621,项目名称:SaNaVi,代码行数:8,代码来源:NoEmptyLinesAfterPhpdocsFixer.php

示例4: fixWhitespace

 /**
  * Cleanup a whitespace token.
  *
  * @param Token $token
  */
 private function fixWhitespace(Token $token)
 {
     $content = $token->getContent();
     // if there is more than one new line in the whitespace, then we need to fix it
     if (substr_count($content, "\n") > 1) {
         // the final bit of the whitespace must be the next statement's indentation
         $lines = Utils::splitLines($content);
         $token->setContent("\n" . end($lines));
     }
 }
开发者ID:rafwlaz,项目名称:PHP-CS-Fixer,代码行数:15,代码来源:NoBlankLinesAfterClassOpeningFixer.php

示例5: fixDocBlock

 /**
  * Fix a given docblock.
  *
  * @param string $content
  *
  * @return string
  */
 private function fixDocBlock($content)
 {
     $lines = Utils::splitLines($content);
     $l = count($lines);
     for ($i = 0; $i < $l; ++$i) {
         $items = array();
         $matches = $this->getMatches($lines[$i]);
         if (null === $matches) {
             continue;
         }
         $current = $i;
         $items[] = $matches;
         while ($matches = $this->getMatches($lines[++$i], true)) {
             $items[] = $matches;
         }
         // compute the max length of the tag, hint and variables
         $tagMax = 0;
         $hintMax = 0;
         $varMax = 0;
         foreach ($items as $item) {
             if (null === $item['tag']) {
                 continue;
             }
             $tagMax = max($tagMax, strlen($item['tag']));
             $hintMax = max($hintMax, strlen($item['hint']));
             $varMax = max($varMax, strlen($item['var']));
         }
         $currTag = null;
         // update
         foreach ($items as $j => $item) {
             if (null === $item['tag']) {
                 if ($item['desc'][0] === '@') {
                     $lines[$current + $j] = $item['indent'] . ' * ' . $item['desc'] . "\n";
                     continue;
                 }
                 $line = $item['indent'] . ' *  ' . str_repeat(' ', $tagMax + $hintMax + $varMax + ('param' === $currTag ? 3 : 2)) . $item['desc'] . "\n";
                 $lines[$current + $j] = $line;
                 continue;
             }
             $currTag = $item['tag'];
             $line = $item['indent'] . ' * @' . $item['tag'] . str_repeat(' ', $tagMax - strlen($item['tag']) + 1) . $item['hint'];
             if (!empty($item['var'])) {
                 $line .= str_repeat(' ', $hintMax - strlen($item['hint']) + 1) . $item['var'] . (!empty($item['desc']) ? str_repeat(' ', $varMax - strlen($item['var']) + 1) . $item['desc'] . "\n" : "\n");
             } elseif (!empty($item['desc'])) {
                 $line .= str_repeat(' ', $hintMax - strlen($item['hint']) + 1) . $item['desc'] . "\n";
             } else {
                 $line .= "\n";
             }
             $lines[$current + $j] = $line;
         }
     }
     return implode($lines);
 }
开发者ID:Flesh192,项目名称:magento,代码行数:60,代码来源:PhpdocParamsFixer.php

示例6: fixDocBlock

 /**
  * Fix a given docblock.
  *
  * @param string $content
  *
  * @return string
  */
 protected function fixDocBlock($content)
 {
     $lines = Utils::splitLines($content);
     $l = count($lines);
     for ($i = 0; $i < $l; ++$i) {
         $items = [];
         $matches = $this->getMatches($lines[$i]);
         if (!$matches) {
             continue;
         }
         $current = $i;
         $items[] = $matches;
         while ($matches = $this->getMatches($lines[++$i], true)) {
             $items[] = $matches;
         }
         foreach ($items as $j => $item) {
             $pieces = explode('|', $item['hint']);
             $hints = [];
             foreach ($pieces as $piece) {
                 $hints[] = trim($piece);
             }
             $desc = trim($item['desc']);
             while (!empty($desc) && mb_substr($desc, 0, 1) === '|') {
                 $desc = trim(mb_substr($desc, 1));
                 $pos = mb_strpos($desc, ' ');
                 if ($pos > 0) {
                     $hints[] = trim(mb_substr($desc, 0, $pos));
                     $desc = trim(mb_substr($desc, $pos));
                 } else {
                     $hints[] = $desc;
                     $desc = '';
                 }
             }
             $item['hint'] = implode('|', $hints);
             $item['desc'] = $desc;
             $line = $item['indent'] . ' * @' . $item['tag'] . ' ' . $item['hint'];
             if (!empty($item['var'])) {
                 $line .= ' ' . $item['var'] . (!empty($item['desc']) ? ' ' . $item['desc'] . "\n" : "\n");
             } elseif (!empty($item['desc'])) {
                 $line .= ' ' . $item['desc'] . "\n";
             } else {
                 $line .= "\n";
             }
             $lines[$current + $j] = $line;
         }
     }
     return implode($lines);
 }
开发者ID:php-fig-rectified,项目名称:psr2r-fixer,代码行数:55,代码来源:PhpdocPipeFixer.php

示例7: getBestDelimiter

 private function getBestDelimiter($pattern)
 {
     $delimiters = array();
     foreach (self::$delimiters as $k => $d) {
         if (false === strpos($pattern, $d)) {
             return $d;
         }
         $delimiters[$d] = array(substr_count($pattern, $d), $k);
     }
     uasort($delimiters, function ($a, $b) {
         if ($a[0] === $b[0]) {
             return Utils::cmpInt($a, $b);
         }
         return $a[0] < $b[0] ? -1 : 1;
     });
     return key($delimiters);
 }
开发者ID:Ryu0621,项目名称:SaNaVi,代码行数:17,代码来源:EregToPregFixer.php

示例8: fix

 public function fix(\SplFileInfo $file, $content)
 {
     $tokens = Tokens::fromCode($content);
     foreach ($tokens->findGivenKind(T_DOC_COMMENT) as $index => $token) {
         $nextIndex = $tokens->getNextMeaningfulToken($index);
         if (null === $nextIndex || $tokens[$nextIndex]->equals('}')) {
             continue;
         }
         $prevToken = $tokens[$index - 1];
         if ($prevToken->isGivenKind(T_OPEN_TAG) || $prevToken->isWhitespace(array('whitespaces' => " \t")) && !$tokens[$index - 2]->isGivenKind(T_OPEN_TAG) || $prevToken->equalsAny(array(';', '{'))) {
             continue;
         }
         $indent = '';
         if ($tokens[$nextIndex - 1]->isWhitespace()) {
             $indent = Utils::calculateTrailingWhitespaceIndent($tokens[$nextIndex - 1]);
         }
         $prevToken->setContent($this->fixWhitespaceBefore($prevToken->getContent(), $indent));
         $token->setContent($this->fixDocBlock($token->getContent(), $indent));
     }
     return $tokens->generateCode();
 }
开发者ID:Ryu0621,项目名称:SaNaVi,代码行数:21,代码来源:PhpdocIndentFixer.php

示例9: fix

 /**
  * @param \SplFileInfo $file
  * @param string $content
  *
  * @return string
  */
 public function fix(\SplFileInfo $file, $content)
 {
     $tokens = Tokens::fromCode($content);
     for ($index = $tokens->count() - 1; $index >= 0; --$index) {
         /* @var Token $token */
         $token = $tokens[$index];
         // We skip T_FOR, T_WHILE for now as they can have valid inline assignment
         if (!$token->isGivenKind([T_FOREACH, T_IF, T_SWITCH])) {
             continue;
         }
         $startIndex = $tokens->getNextMeaningfulToken($index);
         $endIndex = $tokens->findBlockEnd(Tokens::BLOCK_TYPE_PARENTHESIS_BRACE, $startIndex);
         $indexEqualSign = null;
         $hasInlineAssignment = $this->hasInlineAssignment($index, $endIndex, $tokens, $indexEqualSign);
         if (!$hasInlineAssignment) {
             continue;
         }
         // Extract to own $var into line above
         $string = '';
         $var = '';
         for ($i = $startIndex + 1; $i < $endIndex; ++$i) {
             $string .= $tokens[$i]->getContent();
             if ($i < $indexEqualSign) {
                 $var .= $tokens[$i]->getContent();
             }
             $tokens[$i]->clear();
         }
         $string .= ';';
         $tokens[$i - 1]->setContent(trim($var));
         $content = $tokens[$index]->getContent();
         $indent = Utils::calculateTrailingWhitespaceIndent($tokens[$index - 1]);
         $content = $indent . $content;
         $content = $string . PHP_EOL . $content;
         $tokens[$index]->setContent($content);
     }
     return $tokens->generateCode();
 }
开发者ID:php-fig-rectified,项目名称:psr2r-fixer,代码行数:43,代码来源:NoInlineAssignmentFixer.php

示例10: fix

 /**
  * {@inheritdoc}
  */
 public function fix(\SplFileInfo $file, Tokens $tokens)
 {
     foreach ($tokens as $index => $token) {
         if (!$token->isGivenKind(T_DOC_COMMENT)) {
             continue;
         }
         $nextIndex = $tokens->getNextMeaningfulToken($index);
         // skip if there is no next token or if next token is block end `}`
         if (null === $nextIndex || $tokens[$nextIndex]->equals('}')) {
             continue;
         }
         $prevToken = $tokens[$index - 1];
         // ignore inline docblocks
         if ($prevToken->isGivenKind(T_OPEN_TAG) || $prevToken->isWhitespace(" \t") && !$tokens[$index - 2]->isGivenKind(T_OPEN_TAG) || $prevToken->equalsAny(array(';', '{'))) {
             continue;
         }
         $indent = '';
         if ($tokens[$nextIndex - 1]->isWhitespace()) {
             $indent = Utils::calculateTrailingWhitespaceIndent($tokens[$nextIndex - 1]);
         }
         $prevToken->setContent($this->fixWhitespaceBefore($prevToken->getContent(), $indent));
         $token->setContent($this->fixDocBlock($token->getContent(), $indent));
     }
 }
开发者ID:skillberto,项目名称:PHP-CS-Fixer,代码行数:27,代码来源:PhpdocIndentFixer.php

示例11: sortFixers

 private function sortFixers()
 {
     usort($this->fixers, function (FixerInterface $a, FixerInterface $b) {
         return Utils::cmpInt($b->getPriority(), $a->getPriority());
     });
 }
开发者ID:nazimodi,项目名称:PHP-CS-Fixer,代码行数:6,代码来源:Fixer.php

示例12: toJson

 public function toJson()
 {
     static $options = null;
     if (null === $options) {
         $options = Utils::calculateBitmask(array('JSON_PRETTY_PRINT', 'JSON_NUMERIC_CHECK'));
     }
     $output = new \SplFixedArray(count($this));
     foreach ($this as $index => $token) {
         $output[$index] = $token->toArray();
     }
     $this->rewind();
     return json_encode($output, $options);
 }
开发者ID:jimlind,项目名称:PHP-CS-Fixer,代码行数:13,代码来源:Tokens.php

示例13: sortFixers

 /**
  * Sort fixers by their priorities.
  *
  * @return $this
  */
 private function sortFixers()
 {
     // Schwartzian transform is used to improve the efficiency and avoid
     // `usort(): Array was modified by the user comparison function` warning for mocked objects.
     $data = array_map(function (FixerInterface $fixer) {
         return array($fixer, $fixer->getPriority());
     }, $this->fixers);
     usort($data, function (array $a, array $b) {
         return Utils::cmpInt($b[1], $a[1]);
     });
     $this->fixers = array_map(function (array $item) {
         return $item[0];
     }, $data);
     return $this;
 }
开发者ID:tirozhang,项目名称:PHP-CS-Fixer,代码行数:20,代码来源:FixerFactory.php

示例14: getName

 /**
  * {@inheritdoc}
  */
 public function getName()
 {
     $nameParts = explode('\\', get_called_class());
     $name = end($nameParts);
     return Utils::camelCaseToUnderscore($name);
 }
开发者ID:Doability,项目名称:magento2dev,代码行数:9,代码来源:AbstractTransformer.php

示例15: getFixerName

 /**
  * @return string
  */
 protected function getFixerName()
 {
     $reflection = new \ReflectionClass($this);
     $name = preg_replace('/FixerTest$/', '', $reflection->getShortName());
     return Utils::camelCaseToUnderscore($name);
 }
开发者ID:EspadaV8,项目名称:PHP-CS-Fixer,代码行数:9,代码来源:AbstractFixerTestCase.php


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