本文整理汇总了PHP中PhpCsFixer\Tokenizer\Tokens::getPrevMeaningfulToken方法的典型用法代码示例。如果您正苦于以下问题:PHP Tokens::getPrevMeaningfulToken方法的具体用法?PHP Tokens::getPrevMeaningfulToken怎么用?PHP Tokens::getPrevMeaningfulToken使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类PhpCsFixer\Tokenizer\Tokens
的用法示例。
在下文中一共展示了Tokens::getPrevMeaningfulToken方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: findStart
/**
* @param Tokens $tokens
* @param int $index
*
* @return int
*/
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;
}
示例2: fix
/**
* {@inheritdoc}
*/
public function fix(\SplFileInfo $file, Tokens $tokens)
{
/** @var $token \PhpCsFixer\Tokenizer\Token */
foreach ($tokens->findGivenKind(T_STRING) as $index => $token) {
// skip expressions without parameters list
$nextToken = $tokens[$tokens->getNextMeaningfulToken($index)];
if (!$nextToken->equals('(')) {
continue;
}
// skip expressions which are not function reference
$prevTokenIndex = $tokens->getPrevMeaningfulToken($index);
$prevToken = $tokens[$prevTokenIndex];
if ($prevToken->isGivenKind(array(T_DOUBLE_COLON, T_NEW, T_OBJECT_OPERATOR, T_FUNCTION))) {
continue;
}
// handle function reference with namespaces
if ($prevToken->isGivenKind(array(T_NS_SEPARATOR))) {
$twicePrevTokenIndex = $tokens->getPrevMeaningfulToken($prevTokenIndex);
$twicePrevToken = $tokens[$twicePrevTokenIndex];
if ($twicePrevToken->isGivenKind(array(T_DOUBLE_COLON, T_NEW, T_OBJECT_OPERATOR, T_FUNCTION, T_STRING, CT::T_NAMESPACE_OPERATOR))) {
continue;
}
}
// check mapping hit
$tokenContent = strtolower($token->getContent());
if (!isset(self::$aliases[$tokenContent])) {
continue;
}
$token->setContent(self::$aliases[$tokenContent]);
}
}
示例3: fix
/**
* {@inheritdoc}
*/
public function fix(\SplFileInfo $file, Tokens $tokens)
{
static $nativeFunctionNames = null;
if (null === $nativeFunctionNames) {
$nativeFunctionNames = $this->getNativeFunctionNames();
}
for ($index = 0, $count = $tokens->count(); $index < $count; ++$index) {
// test if we are at a function all
if (!$tokens[$index]->isGivenKind(T_STRING)) {
continue;
}
$next = $tokens->getNextMeaningfulToken($index);
if (!$tokens[$next]->equals('(')) {
$index = $next;
continue;
}
$functionNamePrefix = $tokens->getPrevMeaningfulToken($index);
if ($tokens[$functionNamePrefix]->isGivenKind(array(T_DOUBLE_COLON, T_NEW, T_OBJECT_OPERATOR, T_FUNCTION))) {
continue;
}
// do not though the function call if it is to a function in a namespace other than the default
if ($tokens[$functionNamePrefix]->isGivenKind(T_NS_SEPARATOR) && $tokens[$tokens->getPrevMeaningfulToken($functionNamePrefix)]->isGivenKind(T_STRING)) {
continue;
}
// test if the function call is to a native PHP function
$lower = strtolower($tokens[$index]->getContent());
if (!array_key_exists($lower, $nativeFunctionNames)) {
continue;
}
$tokens[$index]->setContent($nativeFunctionNames[$lower]);
$index = $next;
}
}
示例4: 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');
}
}
}
示例5: find
/**
* Looks up Tokens sequence for suitable candidates and delivers boundaries information,
* which can be supplied by other methods in this abstract class.
*
* @param string $functionNameToSearch
* @param Tokens $tokens
* @param int $start
* @param int|null $end
*
* @return int[]|null returns $functionName, $openParenthesis, $closeParenthesis packed into array
*/
protected function find($functionNameToSearch, Tokens $tokens, $start = 0, $end = null)
{
// make interface consistent with findSequence
$end = null === $end ? $tokens->count() : $end;
// find raw sequence which we can analyse for context
$candidateSequence = array(array(T_STRING, $functionNameToSearch), '(');
$matches = $tokens->findSequence($candidateSequence, $start, $end, false);
if (null === $matches) {
// not found, simply return without further attempts
return;
}
// translate results for humans
list($functionName, $openParenthesis) = array_keys($matches);
// first criteria check: shall look like function call
$functionNamePrefix = $tokens->getPrevMeaningfulToken($functionName);
$functionNamePrecedingToken = $tokens[$functionNamePrefix];
if ($functionNamePrecedingToken->isGivenKind(array(T_DOUBLE_COLON, T_NEW, T_OBJECT_OPERATOR, T_FUNCTION))) {
// this expression is differs from expected, resume
return $this->find($functionNameToSearch, $tokens, $openParenthesis, $end);
}
// second criteria check: ensure namespace is the root one
if ($functionNamePrecedingToken->isGivenKind(T_NS_SEPARATOR)) {
$namespaceCandidate = $tokens->getPrevMeaningfulToken($functionNamePrefix);
$namespaceCandidateToken = $tokens[$namespaceCandidate];
if ($namespaceCandidateToken->isGivenKind(array(T_NEW, T_STRING, CT::T_NAMESPACE_OPERATOR))) {
// here can be added complete namespace scan
// this expression is differs from expected, resume
return $this->find($functionNameToSearch, $tokens, $openParenthesis, $end);
}
}
// final step: find closing parenthesis
$closeParenthesis = $tokens->findBlockEnd(Tokens::BLOCK_TYPE_PARENTHESIS_BRACE, $openParenthesis);
return array($functionName, $openParenthesis, $closeParenthesis);
}
示例6: fix
/**
* {@inheritdoc}
*/
public function fix(\SplFileInfo $file, Tokens $tokens)
{
for ($index = $tokens->count() - 3; $index > 0; --$index) {
$token = $tokens[$index];
if (!$token->isGivenKind(T_NEW)) {
continue;
}
$nextIndex = $tokens->getNextTokenOfKind($index, array(':', ';', ',', '(', ')', '[', ']', array(T_CLOSE_TAG), 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->isWhitespace()) {
$nextIndex = $tokens->getNextNonWhitespace($nextIndex);
$nextToken = $tokens[$nextIndex];
}
// new statement with () - nothing to do
if ($nextToken->equals('(')) {
continue;
}
$meaningBeforeNextIndex = $tokens->getPrevMeaningfulToken($nextIndex);
$tokens->insertAt($meaningBeforeNextIndex + 1, array(new Token('('), new Token(')')));
}
}
示例7: 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);
}
}
}
示例8: process
/**
* {@inheritdoc}
*/
public function process(Tokens $tokens, Token $token, $index)
{
if (!$token->equals('|')) {
return;
}
$prevIndex = $tokens->getPrevMeaningfulToken($index);
$prevToken = $tokens[$prevIndex];
if (!$prevToken->isGivenKind(T_STRING)) {
return;
}
$prevIndex = $tokens->getPrevMeaningfulToken($prevIndex);
$prevToken = $tokens[$prevIndex];
if (!$prevToken->equalsAny(array('(', array(CT::T_TYPE_ALTERNATION)))) {
return;
}
$token->override(array(CT::T_TYPE_ALTERNATION, '|'));
}
示例9: process
/**
* {@inheritdoc}
*/
public function process(Tokens $tokens, Token $token, $index)
{
if (!$token->isGivenKind(array(T_CONST, T_FUNCTION))) {
return;
}
$prevToken = $tokens[$tokens->getPrevMeaningfulToken($index)];
if ($prevToken->isGivenKind(T_USE)) {
$token->override(array($token->isGivenKind(T_FUNCTION) ? CT_FUNCTION_IMPORT : CT_CONST_IMPORT, $token->getContent()));
}
}
示例10: fix
/**
* {@inheritdoc}
*/
public function fix(\SplFileInfo $file, Tokens $tokens)
{
foreach ($tokens as $index => $token) {
if (!$token->equals('(')) {
continue;
}
$prevIndex = $tokens->getPrevMeaningfulToken($index);
// ignore parenthesis for T_ARRAY
if (null !== $prevIndex && $tokens[$prevIndex]->isGivenKind(T_ARRAY)) {
continue;
}
$endIndex = $tokens->findBlockEnd(Tokens::BLOCK_TYPE_PARENTHESIS_BRACE, $index);
// remove space after opening `(`
$this->removeSpaceAroundToken($tokens[$index + 1]);
// remove space before closing `)` if it is not `list($a, $b, )` case
if (!$tokens[$tokens->getPrevMeaningfulToken($endIndex)]->equals(',')) {
$this->removeSpaceAroundToken($tokens[$endIndex - 1]);
}
}
}
示例11: process
/**
* {@inheritdoc}
*/
public function process(Tokens $tokens, Token $token, $index)
{
if (!$token->isGivenKind(T_CLASS)) {
return;
}
$prevIndex = $tokens->getPrevMeaningfulToken($index);
$prevToken = $tokens[$prevIndex];
if ($prevToken->isGivenKind(T_DOUBLE_COLON)) {
$token->override(array(CT_CLASS_CONSTANT, $token->getContent()));
}
}
示例12: process
/**
* {@inheritdoc}
*/
public function process(Tokens $tokens, Token $token, $index)
{
if (!$token->equalsAny(array(array(T_CLASS, 'class'), array(T_STRING, 'class')), false)) {
return;
}
$prevIndex = $tokens->getPrevMeaningfulToken($index);
$prevToken = $tokens[$prevIndex];
if ($prevToken->isGivenKind(T_DOUBLE_COLON)) {
$token->override(array(CT::T_CLASS_CONSTANT, $token->getContent()));
}
}
示例13: process
/**
* {@inheritdoc}
*/
public function process(Tokens $tokens, Token $token, $index)
{
if (!$token->equals('?')) {
return;
}
$prevIndex = $tokens->getPrevMeaningfulToken($index);
$prevToken = $tokens[$prevIndex];
if ($prevToken->equalsAny(array('(', ',', array(CT::T_TYPE_COLON)))) {
$token->override(array(CT::T_NULLABLE_TYPE, '?'));
}
}
示例14: process
/**
* {@inheritdoc}
*/
public function process(Tokens $tokens, Token $token, $index)
{
if (!$token->equals(':')) {
return;
}
$endIndex = $tokens->getPrevMeaningfulToken($index);
if (!$tokens[$endIndex]->equals(')')) {
return;
}
$startIndex = $tokens->findBlockEnd(Tokens::BLOCK_TYPE_PARENTHESIS_BRACE, $endIndex, false);
$prevIndex = $tokens->getPrevMeaningfulToken($startIndex);
$prevToken = $tokens[$prevIndex];
// if this could be a function name we need to take one more step
if ($prevToken->isGivenKind(T_STRING)) {
$prevIndex = $tokens->getPrevMeaningfulToken($prevIndex);
$prevToken = $tokens[$prevIndex];
}
if ($prevToken->isGivenKind(array(T_FUNCTION, CT::T_RETURN_REF, CT::T_USE_LAMBDA))) {
$token->override(array(CT::T_TYPE_COLON, ':'));
}
}
示例15: fix
/**
* {@inheritdoc}
*/
public function fix(\SplFileInfo $file, Tokens $tokens)
{
$tokensAnalyzer = new TokensAnalyzer($tokens);
for ($index = $tokens->count() - 1; $index >= 0; --$index) {
if (!$tokensAnalyzer->isBinaryOperator($index)) {
continue;
}
// skip `declare(foo ==bar)`
$prevMeaningfulIndex = $tokens->getPrevMeaningfulToken($index);
if ($tokens[$prevMeaningfulIndex]->isGivenKind(T_STRING)) {
$prevMeaningfulIndex = $tokens->getPrevMeaningfulToken($prevMeaningfulIndex);
if ($tokens[$prevMeaningfulIndex]->equals('(')) {
$prevMeaningfulIndex = $tokens->getPrevMeaningfulToken($prevMeaningfulIndex);
if ($tokens[$prevMeaningfulIndex]->isGivenKind(T_DECLARE)) {
continue;
}
}
}
// fix white space after operator
if ($tokens[$index + 1]->isWhitespace()) {
$content = $tokens[$index + 1]->getContent();
if (' ' !== $content && false === strpos($content, "\n") && !$tokens[$tokens->getNextNonWhitespace($index + 1)]->isComment()) {
$tokens[$index + 1]->setContent(' ');
}
} else {
$tokens->insertAt($index + 1, new Token(array(T_WHITESPACE, ' ')));
}
// fix white space before operator
if ($tokens[$index - 1]->isWhitespace()) {
$content = $tokens[$index - 1]->getContent();
if (' ' !== $content && false === strpos($content, "\n") && !$tokens[$tokens->getPrevNonWhitespace($index - 1)]->isComment()) {
$tokens[$index - 1]->setContent(' ');
}
} else {
$tokens->insertAt($index, new Token(array(T_WHITESPACE, ' ')));
}
--$index;
// skip check for binary operator on the whitespace token that is fixed.
}
}