本文整理匯總了PHP中Symfony\CS\Tokenizer\Tokens::findSequence方法的典型用法代碼示例。如果您正苦於以下問題:PHP Tokens::findSequence方法的具體用法?PHP Tokens::findSequence怎麽用?PHP Tokens::findSequence使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類Symfony\CS\Tokenizer\Tokens
的用法示例。
在下文中一共展示了Tokens::findSequence方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的PHP代碼示例。
示例1: fixUseStatements
private function fixUseStatements(Tokens $tokens)
{
$classIndexes = array_keys($tokens->findGivenKind(T_CLASS));
if ($tokens->findSequence([[T_STRING, 'OptionsResolverInterface']], array_pop($classIndexes))) {
return $this->addUseStatement($tokens, ['Symfony', 'Component', 'OptionsResolver', 'OptionsResolver']);
}
$this->renameUseStatements($tokens, ['Symfony', 'Component', 'OptionsResolver', 'OptionsResolverInterface'], 'OptionsResolver');
}
示例2: getUseStatements
protected function getUseStatements(Tokens $tokens, array $fqcn)
{
$sequence = [[T_USE]];
foreach ($fqcn as $component) {
$sequence = array_merge($sequence, [[T_STRING, $component], [T_NS_SEPARATOR]]);
}
$sequence[count($sequence) - 1] = ';';
return $tokens->findSequence($sequence);
}
示例3: renameConstants
protected function renameConstants(Tokens $tokens, $className, $old, $new)
{
$matchedTokens = $tokens->findSequence([[T_STRING, $className], [T_DOUBLE_COLON], [T_STRING, $old]]);
if (null === $matchedTokens) {
return;
}
$matchedTokensIndexes = array_keys($matchedTokens);
$matchedTokens[$matchedTokensIndexes[count($matchedTokensIndexes) - 1]]->setContent($new);
$this->renameConstants($tokens, $className, $old, $new);
}
示例4: getUseStatements
/**
* @param Tokens $tokens
* @param string[]|string $fqcn
*
* @return null|array
*/
protected function getUseStatements(Tokens $tokens, $fqcn)
{
if (false === is_array($fqcn)) {
$fqcn = explode('\\', $fqcn);
}
$sequence = array(array(T_USE));
foreach ($fqcn as $component) {
$sequence = array_merge($sequence, array(array(T_STRING, $component), array(T_NS_SEPARATOR)));
}
$sequence[count($sequence) - 1] = ';';
return $tokens->findSequence($sequence);
}
示例5: fix
/**
* {@inheritdoc}
*/
public function fix(\SplFileInfo $file, Tokens $tokens)
{
foreach ($this->configuration as $methodBefore => $methodAfter) {
for ($index = 0, $limit = $tokens->count(); $index < $limit; ++$index) {
$sequence = $tokens->findSequence(array(array(T_VARIABLE, '$this'), array(T_OBJECT_OPERATOR, '->'), array(T_STRING, $methodBefore), '('), $index);
if (null === $sequence) {
break;
}
$sequenceIndexes = array_keys($sequence);
$tokens[$sequenceIndexes[2]]->setContent($methodAfter);
$index = $sequenceIndexes[3];
}
}
}
示例6: fix
/**
* {@inheritdoc}
*/
public function fix(\SplFileInfo $file, Tokens $tokens)
{
$end = $tokens->count() - 1;
foreach (self::$functions as $map) {
// the sequence is the function name, followed by "(" and a quoted string
$seq = array(array(T_STRING, $map[0]), '(', array(T_CONSTANT_ENCAPSED_STRING));
$currIndex = 0;
while (null !== $currIndex) {
$match = $tokens->findSequence($seq, $currIndex, $end, false);
// did we find a match?
if (null === $match) {
break;
}
// findSequence also returns the tokens, but we're only interested in the indexes, i.e.:
// 0 => function name,
// 1 => bracket "("
// 2 => quoted string passed as 1st parameter
$match = array_keys($match);
// advance tokenizer cursor
$currIndex = $match[2];
// ensure it's a function call (not a method / static call)
$prev = $tokens->getPrevMeaningfulToken($match[0]);
if (null === $prev || $tokens[$prev]->isGivenKind(array(T_OBJECT_OPERATOR, T_DOUBLE_COLON))) {
continue;
}
// ensure the first parameter is just a string (e.g. has nothing appended)
$next = $tokens->getNextMeaningfulToken($match[2]);
if (null === $next || !$tokens[$next]->equalsAny(array(',', ')'))) {
continue;
}
// convert to PCRE
$string = substr($tokens[$match[2]]->getContent(), 1, -1);
$quote = substr($tokens[$match[2]]->getContent(), 0, 1);
$delim = $this->getBestDelimiter($string);
$preg = $delim . addcslashes($string, $delim) . $delim . 'D' . $map[2];
// check if the preg is valid
if (!$this->checkPreg($preg)) {
continue;
}
// modify function and argument
$tokens[$match[2]]->setContent($quote . $preg . $quote);
$tokens[$match[0]]->setContent($map[1]);
}
}
}
示例7: fixAssertSame
private function fixAssertSame(Tokens $tokens, $index)
{
static $map = array('false' => 'assertFalse', 'null' => 'assertNull', 'true' => 'assertTrue');
$sequence = $tokens->findSequence(array(array(T_VARIABLE, '$this'), array(T_OBJECT_OPERATOR, '->'), array(T_STRING, 'assertSame'), '('), $index);
if (null === $sequence) {
return;
}
$sequenceIndexes = array_keys($sequence);
$sequenceIndexes[4] = $tokens->getNextMeaningfulToken($sequenceIndexes[3]);
$firstParameterToken = $tokens[$sequenceIndexes[4]];
if (!$firstParameterToken->isNativeConstant()) {
return;
}
$sequenceIndexes[5] = $tokens->getNextNonWhitespace($sequenceIndexes[4]);
$tokens[$sequenceIndexes[2]]->setContent($map[$firstParameterToken->getContent()]);
$tokens->clearRange($sequenceIndexes[4], $tokens->getNextNonWhitespace($sequenceIndexes[5]) - 1);
return $sequenceIndexes[5];
}
示例8: fixAssert
private function fixAssert(array $map, Tokens $tokens, $index, $method)
{
$sequence = $tokens->findSequence(array(array(T_VARIABLE, '$this'), array(T_OBJECT_OPERATOR, '->'), array(T_STRING, $method), '('), $index);
if (null === $sequence) {
return;
}
$sequenceIndexes = array_keys($sequence);
$sequenceIndexes[4] = $tokens->getNextMeaningfulToken($sequenceIndexes[3]);
$firstParameterToken = $tokens[$sequenceIndexes[4]];
if (!$firstParameterToken->isNativeConstant()) {
return;
}
$sequenceIndexes[5] = $tokens->getNextMeaningfulToken($sequenceIndexes[4]);
if (!$tokens[$sequenceIndexes[5]]->equals(',')) {
return;
}
$tokens[$sequenceIndexes[2]]->setContent($map[$firstParameterToken->getContent()]);
$tokens->clearRange($sequenceIndexes[4], $tokens->getNextNonWhitespace($sequenceIndexes[5]) - 1);
return $sequenceIndexes[5];
}
示例9: fixOptionNames
private function fixOptionNames(Tokens $tokens, $fieldNameTokens, $oldName, $newName, $start = 0)
{
$matchedTokens = $tokens->findSequence(array_merge([[T_OBJECT_OPERATOR], [T_STRING, 'add'], '(', [T_CONSTANT_ENCAPSED_STRING], ','], $fieldNameTokens, [',']), $start);
if (null === $matchedTokens) {
return;
}
$matchedTokenIndexes = array_keys($matchedTokens);
$isArray = $tokens->isArray($index = $tokens->getNextMeaningfulToken(end($matchedTokenIndexes)));
if (!$isArray) {
return;
}
do {
$index = $tokens->getNextMeaningfulToken($index);
$token = $tokens[$index];
if (!$token->isGivenKind(T_CONSTANT_ENCAPSED_STRING)) {
continue;
}
if ("'{$oldName}'" === $token->getContent()) {
$token->setContent("'{$newName}'");
}
} while (!in_array($token->getContent(), [')', ']']));
$this->fixOptionNames($tokens, $fieldNameTokens, $oldName, $newName, $index);
}
示例10: matchTypeName
private function matchTypeName(Tokens $tokens, $name)
{
return $tokens->findSequence([[T_OBJECT_OPERATOR], [T_STRING, 'add'], '(', [T_CONSTANT_ENCAPSED_STRING], ',', [T_CONSTANT_ENCAPSED_STRING, sprintf("'%s'", strtolower($name))]]);
}
示例11: getGetRequestSequence
private function getGetRequestSequence(Tokens $tokens, $start, $end)
{
return $tokens->findSequence([[T_VARIABLE, '$this'], [T_OBJECT_OPERATOR], [T_STRING, 'getRequest'], '(', ')'], $start, $end);
}
示例12: findFunction
/**
* Find a function or method matching a given name within certain bounds.
*
* @param Tokens $tokens the Tokens instance
* @param string $name the function/Method name
* @param int $startIndex the search start index
* @param int $endIndex the search end index
*
* @return array|null An associative array, if a match is found:
*
* - nameIndex (int): The index of the function/method name.
* - startIndex (int): The index of the function/method start.
* - endIndex (int): The index of the function/method end.
* - bodyIndex (int): The index of the function/method body.
* - modifiers (array): The modifiers as array keys and their index as
* the values, e.g. array(T_PUBLIC => 10).
*/
private function findFunction(Tokens $tokens, $name, $startIndex, $endIndex)
{
$function = $tokens->findSequence(array(array(T_FUNCTION), array(T_STRING, $name), '('), $startIndex, $endIndex, false);
if (null === $function) {
return;
}
// keep only the indexes
$function = array_keys($function);
// find previous block, saving method modifiers for later use
$possibleModifiers = array(T_PUBLIC, T_PROTECTED, T_PRIVATE, T_STATIC, T_ABSTRACT, T_FINAL);
$modifiers = array();
$prevBlock = $tokens->getPrevMeaningfulToken($function[0]);
while (null !== $prevBlock && $tokens[$prevBlock]->isGivenKind($possibleModifiers)) {
$modifiers[$tokens[$prevBlock]->getId()] = $prevBlock;
$prevBlock = $tokens->getPrevMeaningfulToken($prevBlock);
}
if (isset($modifiers[T_ABSTRACT])) {
// abstract methods have no body
$bodyStart = null;
$funcEnd = $tokens->getNextTokenOfKind($function[2], array(';'));
} else {
// find method body start and the end of the function definition
$bodyStart = $tokens->getNextTokenOfKind($function[2], array('{'));
$funcEnd = $bodyStart !== null ? $tokens->findBlockEnd(Tokens::BLOCK_TYPE_CURLY_BRACE, $bodyStart) : null;
}
return array('nameIndex' => $function[1], 'startIndex' => $prevBlock + 1, 'endIndex' => $funcEnd, 'bodyIndex' => $bodyStart, 'modifiers' => $modifiers);
}
示例13: findFunction
private function findFunction(Tokens $tokens, $name, $startIndex, $endIndex)
{
$function = $tokens->findSequence(array(
array(T_FUNCTION),
array(T_STRING, $name),
'(',
), $startIndex, $endIndex, false);
if (null === $function) {
return;
}
$function = array_keys($function);
$possibleModifiers = array(T_PUBLIC, T_PROTECTED, T_PRIVATE, T_STATIC, T_ABSTRACT, T_FINAL);
$modifiers = array();
$prevBlock = $tokens->getPrevMeaningfulToken($function[0]);
while (null !== $prevBlock && $tokens[$prevBlock]->isGivenKind($possibleModifiers)) {
$modifiers[$tokens[$prevBlock]->getId()] = $prevBlock;
$prevBlock = $tokens->getPrevMeaningfulToken($prevBlock);
}
if (isset($modifiers[T_ABSTRACT])) {
$bodyStart = null;
$funcEnd = $tokens->getNextTokenOfKind($function[2], array(';'));
} else {
$bodyStart = $tokens->getNextTokenOfKind($function[2], array('{'));
$funcEnd = $bodyStart !== null ? $tokens->findBlockEnd(Tokens::BLOCK_TYPE_CURLY_BRACE, $bodyStart) : null;
}
return array(
'nameIndex' => $function[1],
'startIndex' => $prevBlock + 1,
'endIndex' => $funcEnd,
'bodyIndex' => $bodyStart,
'modifiers' => $modifiers,
);
}
示例14: matchGetNameMethod
private function matchGetNameMethod(Tokens $tokens)
{
return $tokens->findSequence([[T_PUBLIC, 'public'], [T_FUNCTION], [T_STRING, 'getName'], '(', ')']);
}
示例15: matchGetParentMethod
private function matchGetParentMethod(Tokens $tokens, $name)
{
return $tokens->findSequence([[T_PUBLIC, 'public'], [T_FUNCTION], [T_STRING, 'getParent'], '(', ')', '{', [T_RETURN], [T_CONSTANT_ENCAPSED_STRING, sprintf("'%s'", strtolower($name))], ';', '}']);
}