本文整理汇总了PHP中PhpCsFixer\Tokenizer\TokensAnalyzer::isWhilePartOfDoWhile方法的典型用法代码示例。如果您正苦于以下问题:PHP TokensAnalyzer::isWhilePartOfDoWhile方法的具体用法?PHP TokensAnalyzer::isWhilePartOfDoWhile怎么用?PHP TokensAnalyzer::isWhilePartOfDoWhile使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类PhpCsFixer\Tokenizer\TokensAnalyzer
的用法示例。
在下文中一共展示了TokensAnalyzer::isWhilePartOfDoWhile方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: fixIndents
private function fixIndents(Tokens $tokens)
{
$classyTokens = Token::getClassyTokenKinds();
$classyAndFunctionTokens = array_merge(array(T_FUNCTION), $classyTokens);
$controlTokens = $this->getControlTokens();
$indentTokens = array_filter(array_merge($classyAndFunctionTokens, $controlTokens), function ($item) {
return T_SWITCH !== $item;
});
$tokensAnalyzer = new TokensAnalyzer($tokens);
for ($index = 0, $limit = count($tokens); $index < $limit; ++$index) {
$token = $tokens[$index];
// if token is not a structure element - continue
if (!$token->isGivenKind($indentTokens)) {
continue;
}
// do not change indent for lambda functions
if ($token->isGivenKind(T_FUNCTION) && $tokensAnalyzer->isLambda($index)) {
continue;
}
// do not change indent for `while` in `do ... while ...`
if ($token->isGivenKind(T_WHILE) && $tokensAnalyzer->isWhilePartOfDoWhile($index)) {
continue;
}
if ($token->isGivenKind($classyAndFunctionTokens)) {
$startBraceIndex = $tokens->getNextTokenOfKind($index, array(';', '{'));
$startBraceToken = $tokens[$startBraceIndex];
} else {
$parenthesisEndIndex = $this->findParenthesisEnd($tokens, $index);
$startBraceIndex = $tokens->getNextNonWhitespace($parenthesisEndIndex);
$startBraceToken = $tokens[$startBraceIndex];
}
// structure without braces block - nothing to do, e.g. do { } while (true);
if (!$startBraceToken->equals('{')) {
continue;
}
$endBraceIndex = $tokens->findBlockEnd(Tokens::BLOCK_TYPE_CURLY_BRACE, $startBraceIndex);
$indent = $this->detectIndent($tokens, $index);
// fix indent near closing brace
$tokens->ensureWhitespaceAtIndex($endBraceIndex - 1, 1, "\n" . $indent);
// fix indent between braces
$lastCommaIndex = $tokens->getPrevTokenOfKind($endBraceIndex - 1, array(';', '}'));
$nestLevel = 1;
for ($nestIndex = $lastCommaIndex; $nestIndex >= $startBraceIndex; --$nestIndex) {
$nestToken = $tokens[$nestIndex];
if ($nestToken->equals(')')) {
$nestIndex = $tokens->findBlockEnd(Tokens::BLOCK_TYPE_PARENTHESIS_BRACE, $nestIndex, false);
continue;
}
if (1 === $nestLevel && $nestToken->equalsAny(array(';', '}'))) {
$nextNonWhitespaceNestIndex = $tokens->getNextNonWhitespace($nestIndex);
$nextNonWhitespaceNestToken = $tokens[$nextNonWhitespaceNestIndex];
if (!$nextNonWhitespaceNestToken->isComment() && !($nestToken->equals('}') && $nextNonWhitespaceNestToken->equalsAny(array(';', ',', ']', array(CT_ARRAY_SQUARE_BRACE_CLOSE)))) && !($nestToken->equals('}') && $nextNonWhitespaceNestToken->equals('(')) && !($nestToken->equals('}') && $tokens[$nestIndex - 1]->equalsAny(array('"', "'", array(T_CONSTANT_ENCAPSED_STRING))))) {
if ($nextNonWhitespaceNestToken->isGivenKind($this->getControlContinuationTokens()) || $nextNonWhitespaceNestToken->isGivenKind(T_WHILE) && $tokensAnalyzer->isWhilePartOfDoWhile($nextNonWhitespaceNestIndex)) {
$whitespace = ' ';
} else {
$nextToken = $tokens[$nestIndex + 1];
$nextWhitespace = '';
if ($nextToken->isWhitespace()) {
$nextWhitespace = rtrim($nextToken->getContent(), " \t");
if (strlen($nextWhitespace) && "\n" === $nextWhitespace[strlen($nextWhitespace) - 1]) {
$nextWhitespace = substr($nextWhitespace, 0, -1);
}
}
$whitespace = $nextWhitespace . "\n" . $indent;
if (!$nextNonWhitespaceNestToken->equals('}')) {
$whitespace .= ' ';
}
}
$tokens->ensureWhitespaceAtIndex($nestIndex + 1, 0, $whitespace);
}
}
if ($nestToken->equals('}')) {
++$nestLevel;
continue;
}
if ($nestToken->equals('{')) {
--$nestLevel;
continue;
}
}
// fix indent near opening brace
if (isset($tokens[$startBraceIndex + 2]) && $tokens[$startBraceIndex + 2]->equals('}')) {
$tokens->ensureWhitespaceAtIndex($startBraceIndex + 1, 0, "\n" . $indent);
} elseif (!$tokens[$index]->isClassy()) {
$nextToken = $tokens[$startBraceIndex + 1];
$nextNonWhitespaceToken = $tokens[$tokens->getNextNonWhitespace($startBraceIndex)];
// set indent only if it is not a case, when comment is following { in same line
if (!$nextNonWhitespaceToken->isComment() || !($nextToken->isWhitespace() && $nextToken->isWhitespace(" \t")) && substr_count($nextToken->getContent(), "\n") === 1) {
$tokens->ensureWhitespaceAtIndex($startBraceIndex + 1, 0, "\n" . $indent . ' ');
}
} else {
$nextToken = $tokens[$startBraceIndex + 1];
if (!$nextToken->isWhitespace()) {
$tokens->ensureWhitespaceAtIndex($startBraceIndex + 1, 0, "\n" . $indent . ' ');
} else {
$tmpIndent = trim($nextToken->getContent(), " \t") . $indent . ' ';
if (!isset($tmpIndent[0]) || "\n" !== $tmpIndent[0]) {
$tmpIndent = "\n" . $tmpIndent;
}
$tokens->ensureWhitespaceAtIndex($startBraceIndex + 1, 0, $tmpIndent);
//.........这里部分代码省略.........
示例2: testIsWhilePartOfDoWhile
public function testIsWhilePartOfDoWhile()
{
$source = <<<'SRC'
<?php
// `not do`
while(false) {
}
while (false);
while (false)?>
<?php
if(false){
}while(false);
if(false){
}while(false)?><?php
while(false){}while(false){}
while ($i <= 10):
echo $i;
$i++;
endwhile;
?>
<?php while(false): ?>
<?php endwhile ?>
<?php
// `do`
do{
} while(false);
do{
} while(false)?>
<?php
if (false){}do{}while(false);
// `not do`, `do`
if(false){}while(false){}do{}while(false);
SRC;
$expected = array(3 => false, 12 => false, 19 => false, 34 => false, 47 => false, 53 => false, 59 => false, 66 => false, 91 => false, 112 => true, 123 => true, 139 => true, 153 => false, 162 => true);
$tokens = Tokens::fromCode($source);
$tokensAnalyzer = new TokensAnalyzer($tokens);
foreach ($tokens as $index => $token) {
if (!$token->isGivenKind(T_WHILE)) {
continue;
}
$this->assertSame($expected[$index], $tokensAnalyzer->isWhilePartOfDoWhile($index), sprintf('Expected token at index "%d" to be detected as %sa "do-while"-loop.', $index, true === $expected[$index] ? '' : 'not '));
}
}