本文整理匯總了PHP中Symfony\CS\Tokenizer\Tokens::findBlockEnd方法的典型用法代碼示例。如果您正苦於以下問題:PHP Tokens::findBlockEnd方法的具體用法?PHP Tokens::findBlockEnd怎麽用?PHP Tokens::findBlockEnd使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類Symfony\CS\Tokenizer\Tokens
的用法示例。
在下文中一共展示了Tokens::findBlockEnd方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的PHP代碼示例。
示例1: fixFunction
private function fixFunction(Tokens $tokens, $functionIndex, array $functionParams)
{
$startBraceIndex = $tokens->getNextTokenOfKind($functionIndex, array('('));
$endBraceIndex = $tokens->findBlockEnd(Tokens::BLOCK_TYPE_PARENTHESIS_BRACE, $startBraceIndex);
$commaCounter = 0;
$sawParameter = false;
for ($index = $startBraceIndex + 1; $index < $endBraceIndex; ++$index) {
$token = $tokens[$index];
if (!$token->isWhitespace() && !$token->isComment()) {
$sawParameter = true;
}
if ($token->equals('(')) {
$index = $tokens->findBlockEnd(Tokens::BLOCK_TYPE_PARENTHESIS_BRACE, $index);
continue;
}
if ($token->equals('[')) {
$index = $tokens->findBlockEnd(Tokens::BLOCK_TYPE_SQUARE_BRACE, $index);
continue;
}
if ($token->equals(',')) {
++$commaCounter;
continue;
}
}
$functionParamsQuantity = count($functionParams);
$paramsQuantity = ($sawParameter ? 1 : 0) + $commaCounter;
if ($paramsQuantity === $functionParamsQuantity) {
return;
}
$tokensToInsert = array();
for ($i = $paramsQuantity; $i < $functionParamsQuantity; ++$i) {
if (!$functionParams[$i]) {
return;
}
$tokensToInsert[] = new Token(',');
$tokensToInsert[] = new Token(array(T_WHITESPACE, ' '));
if (!is_array($functionParams[$i])) {
$tokensToInsert[] = clone $functionParams[$i];
continue;
}
foreach ($functionParams[$i] as $param) {
$tokensToInsert[] = clone $param;
}
}
$beforeEndBraceIndex = $tokens->getPrevNonWhitespace($endBraceIndex, array());
$tokens->insertAt($beforeEndBraceIndex + 1, $tokensToInsert);
}
示例2: fixArray
private function fixArray(Tokens $tokens, $index)
{
if (!$tokens->isArrayMultiLine($index)) {
return;
}
$startIndex = $index;
if ($tokens[$startIndex]->isGivenKind(T_ARRAY)) {
$startIndex = $tokens->getNextTokenOfKind($startIndex, array('('));
$endIndex = $tokens->findBlockEnd(Tokens::BLOCK_TYPE_PARENTHESIS_BRACE, $startIndex);
} else {
$endIndex = $tokens->findBlockEnd(Tokens::BLOCK_TYPE_SQUARE_BRACE, $startIndex);
}
$beforeEndIndex = $tokens->getPrevMeaningfulToken($endIndex);
$beforeEndToken = $tokens[$beforeEndIndex];
if ($startIndex !== $beforeEndIndex && !$beforeEndToken->equalsAny(array(',', array(T_END_HEREDOC)))) {
$tokens->insertAt($beforeEndIndex + 1, new Token(','));
$endToken = $tokens[$endIndex];
if (!$endToken->isComment() && !$endToken->isWhitespace()) {
$tokens->ensureWhitespaceAtIndex($endIndex, 1, ' ');
}
}
}
示例3: injectAlignmentPlaceholders
/**
* Inject into the text placeholders of candidates of vertical alignment.
*
* @param Tokens $tokens
*/
private function injectAlignmentPlaceholders(Tokens $tokens)
{
$deepestLevel = 0;
$limit = $tokens->count();
for ($index = 0; $index < $limit; ++$index) {
$token = $tokens[$index];
if ($token->equals('=')) {
$token->setContent(sprintf(self::ALIGNABLE_PLACEHOLDER, $deepestLevel) . $token->getContent());
continue;
}
if ($token->isGivenKind(T_FUNCTION)) {
++$deepestLevel;
continue;
}
if ($token->equals('(')) {
$index = $tokens->findBlockEnd(Tokens::BLOCK_TYPE_PARENTHESIS_BRACE, $index);
continue;
}
if ($token->equals('[')) {
$index = $tokens->findBlockEnd(Tokens::BLOCK_TYPE_INDEX_SQUARE_BRACE, $index);
continue;
}
if ($token->isGivenKind(CT_ARRAY_SQUARE_BRACE_OPEN)) {
$index = $tokens->findBlockEnd(Tokens::BLOCK_TYPE_ARRAY_SQUARE_BRACE, $index);
continue;
}
}
$this->deepestLevel = $deepestLevel;
}
示例4: skipNonArrayElements
/**
* Method to move index over the non-array elements like function calls or function declarations.
*
* @param int $index
* @param Tokens $tokens
*
* @return int New index
*/
private function skipNonArrayElements($index, Tokens $tokens)
{
if ($tokens[$index]->equals('}')) {
return $tokens->findBlockEnd(Tokens::BLOCK_TYPE_CURLY_BRACE, $index, false);
}
if ($tokens[$index]->equals(')')) {
$startIndex = $tokens->findBlockEnd(Tokens::BLOCK_TYPE_PARENTHESIS_BRACE, $index, false);
$startIndex = $tokens->getPrevMeaningfulToken($startIndex);
if (!$tokens->isArray($startIndex)) {
return $startIndex;
}
}
return $index;
}
示例5: fix
/**
* {@inheritdoc}
*/
public function fix(\SplFileInfo $file, Tokens $tokens)
{
for ($index = $tokens->count() - 1; $index >= 0; --$index) {
$token = $tokens[$index];
if (!$token->isGivenKind(T_FUNCTION)) {
continue;
}
$startParenthesisIndex = $tokens->getNextTokenOfKind($index, array('('));
$endParenthesisIndex = $tokens->findBlockEnd(Tokens::BLOCK_TYPE_PARENTHESIS_BRACE, $startParenthesisIndex);
for ($iter = $endParenthesisIndex - 1; $iter > $startParenthesisIndex; --$iter) {
if (!$tokens[$iter]->isGivenKind(T_VARIABLE)) {
continue;
}
// skip ... before $variable for variadic parameter
if (defined('T_ELLIPSIS')) {
$prevNonWhitespaceIndex = $tokens->getPrevNonWhitespace($iter);
if ($tokens[$prevNonWhitespaceIndex]->isGivenKind(T_ELLIPSIS)) {
$iter = $prevNonWhitespaceIndex;
}
}
// skip & before $variable for parameter passed by reference
$prevNonWhitespaceIndex = $tokens->getPrevNonWhitespace($iter);
if ($tokens[$prevNonWhitespaceIndex]->equals('&')) {
$iter = $prevNonWhitespaceIndex;
}
if (!$tokens[$iter - 1]->equalsAny(array(array(T_WHITESPACE), array(T_COMMENT), array(T_DOC_COMMENT), '(', ','))) {
$tokens->insertAt($iter, new Token(array(T_WHITESPACE, ' ')));
}
}
}
}
示例6: fix
/**
* {@inheritdoc}
*/
public function fix(\SplFileInfo $file, Tokens $tokens)
{
foreach ($tokens as $index => $token) {
if (!$token->isGivenKind(T_ECHO)) {
continue;
}
$nextTokenIndex = $tokens->getNextMeaningfulToken($index);
$endTokenIndex = $tokens->getNextTokenOfKind($index, array(';', array(T_CLOSE_TAG)));
$canBeConverted = true;
for ($i = $nextTokenIndex; $i < $endTokenIndex; ++$i) {
if ($tokens[$i]->equalsAny(array('(', array(CT_ARRAY_SQUARE_BRACE_OPEN)))) {
$blockType = $tokens->detectBlockType($tokens[$i]);
$i = $tokens->findBlockEnd($blockType['type'], $i);
}
if ($tokens[$i]->equals(',')) {
$canBeConverted = false;
break;
}
}
if (false === $canBeConverted) {
continue;
}
$tokens->overrideAt($index, array(T_PRINT, 'print'));
}
}
示例7: fix
/**
* {@inheritdoc}
*/
public function fix(\SplFileInfo $file, Tokens $tokens)
{
$functionyTokens = $this->getFunctionyTokenKinds();
$languageConstructionTokens = $this->getLanguageConstructionTokenKinds();
foreach ($tokens as $index => $token) {
// looking for start brace
if (!$token->equals('(')) {
continue;
}
// last non-whitespace token
$lastTokenIndex = $tokens->getPrevNonWhitespace($index);
if (null === $lastTokenIndex) {
continue;
}
// check for ternary operator
$endParenthesisIndex = $tokens->findBlockEnd(Tokens::BLOCK_TYPE_PARENTHESIS_BRACE, $index);
$nextNonWhiteSpace = $tokens->getNextMeaningfulToken($endParenthesisIndex);
if (!empty($nextNonWhiteSpace) && $tokens[$nextNonWhiteSpace]->equals('?') && $tokens[$lastTokenIndex]->isGivenKind($languageConstructionTokens)) {
continue;
}
// check if it is a function call
if ($tokens[$lastTokenIndex]->isGivenKind($functionyTokens)) {
$this->fixFunctionCall($tokens, $index);
}
}
}
示例8: replaceNameOccurrences
/**
* Replace occurrences of the name of the classy element by "self" (if possible).
*
* @param Tokens $tokens
* @param string $name
* @param int $startIndex
* @param int $endIndex
*/
private function replaceNameOccurrences(Tokens $tokens, $name, $startIndex, $endIndex)
{
$tokensAnalyzer = new TokensAnalyzer($tokens);
for ($i = $startIndex; $i < $endIndex; ++$i) {
$token = $tokens[$i];
// skip lambda functions (PHP < 5.4 compatibility)
if ($token->isGivenKind(T_FUNCTION) && $tokensAnalyzer->isLambda($i)) {
$i = $tokens->getNextTokenOfKind($i, array('{'));
$i = $tokens->findBlockEnd(Tokens::BLOCK_TYPE_CURLY_BRACE, $i);
continue;
}
if (!$token->equals(array(T_STRING, $name), false)) {
continue;
}
$prevToken = $tokens[$tokens->getPrevMeaningfulToken($i)];
$nextToken = $tokens[$tokens->getNextMeaningfulToken($i)];
// skip tokens that are part of a fully qualified name
if ($prevToken->isGivenKind(T_NS_SEPARATOR) || $nextToken->isGivenKind(T_NS_SEPARATOR)) {
continue;
}
if ($prevToken->isGivenKind(array(T_INSTANCEOF, T_NEW)) || $nextToken->isGivenKind(T_PAAMAYIM_NEKUDOTAYIM)) {
$token->setContent('self');
}
}
}
示例9: process
/**
* {@inheritdoc}
*/
public function process(Tokens $tokens, Token $token, $index)
{
$prevTokenIndex = $tokens->getPrevMeaningfulToken($index);
$prevToken = $prevTokenIndex === null ? null : $tokens[$prevTokenIndex];
// Skip whole class braces content.
// That way we can skip whole tokens in class declaration, therefore skip `T_USE` for traits.
if ($token->isClassy() && !$prevToken->isGivenKind(T_DOUBLE_COLON)) {
$index = $tokens->getNextTokenOfKind($index, array('{'));
$innerLimit = $tokens->findBlockEnd(Tokens::BLOCK_TYPE_CURLY_BRACE, $index);
while ($index < $innerLimit) {
$token = $tokens[++$index];
if (!$token->isGivenKind(T_USE)) {
continue;
}
if ($this->isUseForLambda($tokens, $index)) {
$token->override(array(CT_USE_LAMBDA, $token->getContent()));
} else {
$token->override(array(CT_USE_TRAIT, $token->getContent()));
}
}
return;
}
if ($token->isGivenKind(T_USE) && $this->isUseForLambda($tokens, $index)) {
$token->override(array(CT_USE_LAMBDA, $token->getContent()));
}
}
示例10: fix
/**
* {@inheritdoc}
*/
public function fix(\SplFileInfo $file, Tokens $tokens)
{
// Checks if specific statements are set and uses them in this case.
$loops = array_intersect_key(self::$loops, array_flip($this->controlStatements));
foreach ($tokens as $index => $token) {
if (!$token->equals('(')) {
continue;
}
$blockStartIndex = $index;
$index = $tokens->getPrevMeaningfulToken($index);
$token = $tokens[$index];
foreach ($loops as $loop) {
if (!$token->isGivenKind($loop['lookupTokens'])) {
continue;
}
$blockEndIndex = $tokens->findBlockEnd(Tokens::BLOCK_TYPE_PARENTHESIS_BRACE, $blockStartIndex);
$blockEndNextIndex = $tokens->getNextMeaningfulToken($blockEndIndex);
if (!$tokens[$blockEndNextIndex]->equalsAny($loop['neededSuccessors'])) {
continue;
}
if ($tokens[$blockStartIndex - 1]->isWhitespace() || $tokens[$blockStartIndex - 1]->isComment()) {
$this->clearParenthesis($tokens, $blockStartIndex);
} else {
// Adds a space to prevent broken code like `return2`.
$tokens->overrideAt($blockStartIndex, array(T_WHITESPACE, ' '));
}
$this->clearParenthesis($tokens, $blockEndIndex);
}
}
}
示例11: injectAlignmentPlaceholders
/**
* Inject into the text placeholders of candidates of vertical alignment.
*
* @param Tokens $tokens
* @param int $startAt
* @param int $endAt
*
* @return array($code, $context_counter)
*/
private function injectAlignmentPlaceholders(Tokens $tokens, $startAt, $endAt)
{
for ($index = $startAt; $index < $endAt; ++$index) {
$token = $tokens[$index];
if ($token->isGivenKind(array(T_FOREACH, T_FOR, T_WHILE, T_IF, T_SWITCH))) {
$index = $tokens->getNextMeaningfulToken($index);
$index = $tokens->findBlockEnd(Tokens::BLOCK_TYPE_PARENTHESIS_BRACE, $index);
continue;
}
if ($token->isGivenKind(T_ARRAY)) {
// don't use "$tokens->isArray()" here, short arrays are handled in the next case
$from = $tokens->getNextMeaningfulToken($index);
$until = $tokens->findBlockEnd(Tokens::BLOCK_TYPE_PARENTHESIS_BRACE, $from);
$index = $until;
++$this->deepestLevel;
++$this->currentLevel;
$this->injectAlignmentPlaceholders($tokens, $from, $until);
--$this->currentLevel;
continue;
}
if ($token->isGivenKind(CT_ARRAY_SQUARE_BRACE_OPEN)) {
$prevToken = $tokens[$tokens->getPrevMeaningfulToken($index)];
if ($prevToken->isGivenKind(array(T_STRING, T_VARIABLE))) {
continue;
}
$from = $index;
$until = $tokens->findBlockEnd(Tokens::BLOCK_TYPE_ARRAY_SQUARE_BRACE, $from);
$index = $until;
++$this->deepestLevel;
++$this->currentLevel;
$this->injectAlignmentPlaceholders($tokens, $from + 1, $until - 1);
--$this->currentLevel;
continue;
}
if ($token->isGivenKind(T_DOUBLE_ARROW)) {
$tokenContent = sprintf(self::ALIGNABLE_PLACEHOLDER, $this->currentLevel) . $token->getContent();
$nextToken = $tokens[$index + 1];
if (!$nextToken->isWhitespace()) {
$tokenContent .= ' ';
} elseif ($nextToken->isWhitespace(" \t")) {
$nextToken->setContent(' ');
}
$token->setContent($tokenContent);
continue;
}
if ($token->equals(';')) {
++$this->deepestLevel;
++$this->currentLevel;
continue;
}
if ($token->equals(',')) {
for ($i = $index; $i < $endAt - 1; ++$i) {
if ($tokens[$i + 1]->isGivenKind(array(T_ARRAY, CT_ARRAY_SQUARE_BRACE_OPEN)) || false !== strpos($tokens[$i - 1]->getContent(), "\n")) {
break;
}
++$index;
}
}
}
}
示例12: fix
/**
* {@inheritdoc}
*/
public function fix(\SplFileInfo $file, Tokens $tokens)
{
for ($index = $tokens->count() - 1; $index >= 0; --$index) {
$token = $tokens[$index];
if (!$token->isGivenKind(T_NEW)) {
continue;
}
$nextIndex = $tokens->getNextTokenOfKind($index, array(':', ';', ',', '(', ')', '[', ']', array(CT_ARRAY_SQUARE_BRACE_OPEN), array(CT_ARRAY_SQUARE_BRACE_CLOSE), array(CT_BRACE_CLASS_INSTANTIATION_OPEN), array(CT_BRACE_CLASS_INSTANTIATION_CLOSE)));
$nextToken = $tokens[$nextIndex];
// entrance into array index syntax - need to look for exit
while ($nextToken->equals('[')) {
$nextIndex = $tokens->findBlockEnd(Tokens::BLOCK_TYPE_INDEX_SQUARE_BRACE, $nextIndex) + 1;
$nextToken = $tokens[$nextIndex];
}
// new statement has a gap in it - advance to the next token
if ($nextToken->isGivenKind(T_WHITESPACE)) {
$nextIndex = $tokens->getNextNonWhitespace($nextIndex);
$nextToken = $tokens[$nextIndex];
}
// new statement with () - nothing to do
if ($nextToken->equals('(')) {
continue;
}
$meaningBeforeNextIndex = $tokens->getPrevNonWhitespace($nextIndex);
$tokens->insertAt($meaningBeforeNextIndex + 1, array(new Token('('), new Token(')')));
}
}
示例13: fixContent
/**
* @param Tokens|Token[] $tokens
*
* @return void
*/
protected function fixContent(Tokens $tokens)
{
$wrongTokens = [T_FUNCTION, T_OBJECT_OPERATOR, T_NEW];
foreach ($tokens as $index => $token) {
$tokenContent = strtolower($token->getContent());
if (empty($tokenContent) || !isset(self::$matching[$tokenContent])) {
continue;
}
$prevIndex = $tokens->getPrevNonWhitespace($index);
if (in_array($tokens[$prevIndex]->getId(), $wrongTokens, true)) {
continue;
}
$openingBrace = $tokens->getNextMeaningfulToken($index);
if ($tokens[$openingBrace]->getContent() !== '(') {
continue;
}
$closingBrace = $tokens->findBlockEnd(Tokens::BLOCK_TYPE_PARENTHESIS_BRACE, $openingBrace);
// Skip for non-trivial cases
for ($i = $openingBrace + 1; $i < $closingBrace; ++$i) {
if ($tokens[$i]->equals(',')) {
continue 2;
}
}
$cast = '(' . self::$matching[$tokenContent] . ')';
$tokens[$index]->setContent($cast);
$tokens[$openingBrace]->setContent('');
$tokens[$closingBrace]->setContent('');
}
}
示例14: process
public function process(Tokens $tokens)
{
foreach ($tokens as $index => $token) {
if (!$token->equals('$')) {
continue;
}
$openIndex = $tokens->getNextMeaningfulToken($index);
if (null === $openIndex) {
continue;
}
$openToken = $tokens[$openIndex];
if (!$openToken->equals('{')) {
continue;
}
$closeIndex = $tokens->findBlockEnd(Tokens::BLOCK_TYPE_CURLY_BRACE, $openIndex);
$closeToken = $tokens[$closeIndex];
$openToken->override(array(CT_DYNAMIC_VAR_BRACE_OPEN, '{', $openToken->getLine()));
$closeToken->override(array(CT_DYNAMIC_VAR_BRACE_CLOSE, '}', $closeToken->getLine()));
}
}
示例15: findStart
private function findStart(Tokens $tokens, $index)
{
do {
$index = $tokens->getPrevMeaningfulToken($index);
$token = $tokens[$index];
$blockType = $tokens->detectBlockType($token);
if (null !== $blockType && !$blockType['isStart']) {
$index = $tokens->findBlockEnd($blockType['type'], $index, false);
$token = $tokens[$index];
}
} while (!$token->equalsAny(array('$', array(T_VARIABLE))));
$prevIndex = $tokens->getPrevMeaningfulToken($index);
$prevToken = $tokens[$prevIndex];
if ($prevToken->equals('$')) {
$index = $prevIndex;
$prevIndex = $tokens->getPrevMeaningfulToken($index);
$prevToken = $tokens[$prevIndex];
}
if ($prevToken->isGivenKind(T_OBJECT_OPERATOR)) {
return $this->findStart($tokens, $prevIndex);
}
if ($prevToken->isGivenKind(T_PAAMAYIM_NEKUDOTAYIM)) {
$prevPrevIndex = $tokens->getPrevMeaningfulToken($prevIndex);
if (!$tokens[$prevPrevIndex]->isGivenKind(T_STRING)) {
return $this->findStart($tokens, $prevIndex);
}
$index = $tokens->getTokenNotOfKindSibling($prevIndex, -1, array(array(T_NS_SEPARATOR), array(T_STRING)));
$index = $tokens->getNextMeaningfulToken($index);
}
return $index;
}