mirror of
https://github.com/rectorphp/rector.git
synced 2024-06-13 22:52:23 +00:00
* re-enable rectify and ecs * [Rectify] [Php81] Enable Rectify on Readonly Property only * comment * [ci-review] Rector Rectify * [ci-review] Rector Rectify * [ci-review] Rector Rectify * [ci-review] Rector Rectify * [ci-review] Rector Rectify Co-authored-by: GitHub Action <action@github.com>
47 lines
1.3 KiB
PHP
47 lines
1.3 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 Symplify\PackageBuilder\Reflection\PrivatesAccessor;
|
|
|
|
final class TokenIteratorFactory
|
|
{
|
|
/**
|
|
* @var string
|
|
*/
|
|
private const INDEX = 'index';
|
|
|
|
public function __construct(
|
|
private readonly Lexer $lexer,
|
|
private readonly 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;
|
|
}
|
|
}
|