rector/packages/BetterPhpDocParser/PhpDocInfo/TokenIteratorFactory.php
Tomas Votruba 503a6059f8 Updated Rector to commit a8922f7431c9c9188be501107ee7819e0130da4c
a8922f7431 skip temporarily match + throws downagrade in symfony/console, very unlikely to run
2023-06-11 23:01:39 +00:00

49 lines
1.6 KiB
PHP

<?php
declare (strict_types=1);
namespace Rector\BetterPhpDocParser\PhpDocInfo;
use PHPStan\PhpDocParser\Lexer\Lexer;
use PHPStan\PhpDocParser\Parser\TokenIterator;
use Rector\BetterPhpDocParser\ValueObject\Parser\BetterTokenIterator;
use Rector\Core\Util\Reflection\PrivatesAccessor;
final class TokenIteratorFactory
{
/**
* @readonly
* @var \PHPStan\PhpDocParser\Lexer\Lexer
*/
private $lexer;
/**
* @readonly
* @var \Rector\Core\Util\Reflection\PrivatesAccessor
*/
private $privatesAccessor;
/**
* @var string
*/
private const INDEX = 'index';
public function __construct(Lexer $lexer, PrivatesAccessor $privatesAccessor)
{
$this->lexer = $lexer;
$this->privatesAccessor = $privatesAccessor;
}
public function create(string $content) : BetterTokenIterator
{
$tokens = $this->lexer->tokenize($content);
return new BetterTokenIterator($tokens);
}
public function createFromTokenIterator(TokenIterator $tokenIterator) : BetterTokenIterator
{
if ($tokenIterator instanceof BetterTokenIterator) {
return $tokenIterator;
}
$tokens = $this->privatesAccessor->getPrivateProperty($tokenIterator, 'tokens');
$betterTokenIterator = new BetterTokenIterator($tokens);
// keep original position
$currentIndex = $this->privatesAccessor->getPrivateProperty($tokenIterator, self::INDEX);
$this->privatesAccessor->setPrivateProperty($betterTokenIterator, self::INDEX, $currentIndex);
return $betterTokenIterator;
}
}