mirror of
https://github.com/rectorphp/rector.git
synced 2025-01-17 13:28:18 +01:00
Updated Rector to commit 25ae1d85be40243e03c950de3ac15af7b5b5d3b2
25ae1d85be
Remove TokenGetAllToObjectRector, as rare to use and leaky to handle, better handle in controller manual way (#4001)
This commit is contained in:
parent
82f764337c
commit
99ac9186fd
@ -19,7 +19,6 @@ use Rector\Php80\Rector\ClassMethod\FinalPrivateToPrivateVisibilityRector;
|
||||
use Rector\Php80\Rector\ClassMethod\SetStateToStaticRector;
|
||||
use Rector\Php80\Rector\FuncCall\ClassOnObjectRector;
|
||||
use Rector\Php80\Rector\FuncCall\Php8ResourceReturnToObjectRector;
|
||||
use Rector\Php80\Rector\FuncCall\TokenGetAllToObjectRector;
|
||||
use Rector\Php80\Rector\FunctionLike\MixedTypeRector;
|
||||
use Rector\Php80\Rector\Identical\StrEndsWithRector;
|
||||
use Rector\Php80\Rector\Identical\StrStartsWithRector;
|
||||
@ -34,13 +33,7 @@ return static function (RectorConfig $rectorConfig) : void {
|
||||
$rectorConfig->rule(StrStartsWithRector::class);
|
||||
$rectorConfig->rule(StrEndsWithRector::class);
|
||||
$rectorConfig->ruleWithConfiguration(StaticCallToFuncCallRector::class, [new StaticCallToFuncCall('Nette\\Utils\\Strings', 'startsWith', 'str_starts_with'), new StaticCallToFuncCall('Nette\\Utils\\Strings', 'endsWith', 'str_ends_with'), new StaticCallToFuncCall('Nette\\Utils\\Strings', 'contains', 'str_contains')]);
|
||||
$rectorConfig->rule(StringableForToStringRector::class);
|
||||
$rectorConfig->rule(ClassOnObjectRector::class);
|
||||
$rectorConfig->rule(GetDebugTypeRector::class);
|
||||
$rectorConfig->rule(TokenGetAllToObjectRector::class);
|
||||
$rectorConfig->rule(RemoveUnusedVariableInCatchRector::class);
|
||||
$rectorConfig->rule(ClassPropertyAssignToConstructorPromotionRector::class);
|
||||
$rectorConfig->rule(ChangeSwitchToMatchRector::class);
|
||||
$rectorConfig->rules([StringableForToStringRector::class, ClassOnObjectRector::class, GetDebugTypeRector::class, RemoveUnusedVariableInCatchRector::class, ClassPropertyAssignToConstructorPromotionRector::class, ChangeSwitchToMatchRector::class]);
|
||||
// nette\utils and Strings::replace()
|
||||
$rectorConfig->ruleWithConfiguration(ArgumentAdderRector::class, [new ArgumentAdder('Nette\\Utils\\Strings', 'replace', 2, 'replacement', '')]);
|
||||
$rectorConfig->rule(RemoveParentCallWithoutParentRector::class);
|
||||
|
@ -17,10 +17,7 @@ final class SimpleCallableNodeTraverser
|
||||
*/
|
||||
public function traverseNodesWithCallable($node, callable $callable) : void
|
||||
{
|
||||
if ($node === null) {
|
||||
return;
|
||||
}
|
||||
if ($node === []) {
|
||||
if ($node === null || $node === []) {
|
||||
return;
|
||||
}
|
||||
$nodeTraverser = new NodeTraverser();
|
||||
|
@ -21,9 +21,6 @@ final class CallableNodeVisitor extends NodeVisitorAbstract
|
||||
{
|
||||
$this->callable = $callable;
|
||||
}
|
||||
/**
|
||||
* @return int|\PhpParser\Node|null
|
||||
*/
|
||||
public function enterNode(Node $node)
|
||||
{
|
||||
$originalNode = $node;
|
||||
|
@ -60,6 +60,9 @@ final class NodesToRemoveCollector implements NodeCollectorInterface
|
||||
$this->nodeComparator = $nodeComparator;
|
||||
$this->currentFileProvider = $currentFileProvider;
|
||||
}
|
||||
/**
|
||||
* @deprecated Use direct return of changes Stmt instead
|
||||
*/
|
||||
public function addNodeToRemove(Node $node) : void
|
||||
{
|
||||
/** Node|null $parentNode */
|
||||
|
@ -1,343 +0,0 @@
|
||||
<?php
|
||||
|
||||
declare (strict_types=1);
|
||||
namespace Rector\Php80\NodeManipulator;
|
||||
|
||||
use PhpParser\Node;
|
||||
use PhpParser\Node\Arg;
|
||||
use PhpParser\Node\Expr;
|
||||
use PhpParser\Node\Expr\ArrayDimFetch;
|
||||
use PhpParser\Node\Expr\Assign;
|
||||
use PhpParser\Node\Expr\BinaryOp;
|
||||
use PhpParser\Node\Expr\BinaryOp\Identical;
|
||||
use PhpParser\Node\Expr\BinaryOp\NotIdentical;
|
||||
use PhpParser\Node\Expr\BooleanNot;
|
||||
use PhpParser\Node\Expr\ConstFetch;
|
||||
use PhpParser\Node\Expr\FuncCall;
|
||||
use PhpParser\Node\Expr\MethodCall;
|
||||
use PhpParser\Node\Expr\PropertyFetch;
|
||||
use PhpParser\Node\Expr\Ternary;
|
||||
use PhpParser\Node\Expr\Variable;
|
||||
use PhpParser\Node\Stmt\If_;
|
||||
use Rector\Core\PhpParser\Comparing\NodeComparator;
|
||||
use Rector\Core\PhpParser\Node\BetterNodeFinder;
|
||||
use Rector\Core\PhpParser\Node\Value\ValueResolver;
|
||||
use Rector\Core\Util\StringUtils;
|
||||
use Rector\NodeNameResolver\NodeNameResolver;
|
||||
use Rector\NodeTypeResolver\Node\AttributeKey;
|
||||
use Rector\NodeTypeResolver\NodeTypeResolver;
|
||||
use Rector\Php80\ValueObject\ArrayDimFetchAndConstFetch;
|
||||
use Rector\PhpDocParser\NodeTraverser\SimpleCallableNodeTraverser;
|
||||
use Rector\PostRector\Collector\NodesToRemoveCollector;
|
||||
final class TokenManipulator
|
||||
{
|
||||
/**
|
||||
* @var \PhpParser\Node\Expr|null
|
||||
*/
|
||||
private $assignedNameExpr;
|
||||
/**
|
||||
* @readonly
|
||||
* @var \Rector\PhpDocParser\NodeTraverser\SimpleCallableNodeTraverser
|
||||
*/
|
||||
private $simpleCallableNodeTraverser;
|
||||
/**
|
||||
* @readonly
|
||||
* @var \Rector\NodeNameResolver\NodeNameResolver
|
||||
*/
|
||||
private $nodeNameResolver;
|
||||
/**
|
||||
* @readonly
|
||||
* @var \Rector\NodeTypeResolver\NodeTypeResolver
|
||||
*/
|
||||
private $nodeTypeResolver;
|
||||
/**
|
||||
* @readonly
|
||||
* @var \Rector\PostRector\Collector\NodesToRemoveCollector
|
||||
*/
|
||||
private $nodesToRemoveCollector;
|
||||
/**
|
||||
* @readonly
|
||||
* @var \Rector\Core\PhpParser\Node\Value\ValueResolver
|
||||
*/
|
||||
private $valueResolver;
|
||||
/**
|
||||
* @readonly
|
||||
* @var \Rector\Core\PhpParser\Comparing\NodeComparator
|
||||
*/
|
||||
private $nodeComparator;
|
||||
/**
|
||||
* @readonly
|
||||
* @var \Rector\Core\PhpParser\Node\BetterNodeFinder
|
||||
*/
|
||||
private $betterNodeFinder;
|
||||
public function __construct(SimpleCallableNodeTraverser $simpleCallableNodeTraverser, NodeNameResolver $nodeNameResolver, NodeTypeResolver $nodeTypeResolver, NodesToRemoveCollector $nodesToRemoveCollector, ValueResolver $valueResolver, NodeComparator $nodeComparator, BetterNodeFinder $betterNodeFinder)
|
||||
{
|
||||
$this->simpleCallableNodeTraverser = $simpleCallableNodeTraverser;
|
||||
$this->nodeNameResolver = $nodeNameResolver;
|
||||
$this->nodeTypeResolver = $nodeTypeResolver;
|
||||
$this->nodesToRemoveCollector = $nodesToRemoveCollector;
|
||||
$this->valueResolver = $valueResolver;
|
||||
$this->nodeComparator = $nodeComparator;
|
||||
$this->betterNodeFinder = $betterNodeFinder;
|
||||
}
|
||||
/**
|
||||
* @param Node[] $nodes
|
||||
*/
|
||||
public function refactorArrayToken(array $nodes, Variable $singleTokenVariable) : void
|
||||
{
|
||||
$this->replaceTokenDimFetchZeroWithGetTokenName($nodes, $singleTokenVariable);
|
||||
// replace "$token[1]"; with "$token->value"
|
||||
$this->simpleCallableNodeTraverser->traverseNodesWithCallable($nodes, function (Node $node) : ?PropertyFetch {
|
||||
if (!$node instanceof ArrayDimFetch) {
|
||||
return null;
|
||||
}
|
||||
if (!$this->isArrayDimFetchWithDimIntegerValue($node, 1)) {
|
||||
return null;
|
||||
}
|
||||
$tokenStaticType = $this->nodeTypeResolver->getType($node->var);
|
||||
if (!$tokenStaticType->isArray()->yes()) {
|
||||
return null;
|
||||
}
|
||||
return new PropertyFetch($node->var, 'text');
|
||||
});
|
||||
}
|
||||
/**
|
||||
* @param Node[] $nodes
|
||||
*/
|
||||
public function refactorNonArrayToken(array $nodes, Variable $singleTokenVariable) : void
|
||||
{
|
||||
// replace "$content = $token;" → "$content = $token->text;"
|
||||
$this->simpleCallableNodeTraverser->traverseNodesWithCallable($nodes, function (Node $node) use($singleTokenVariable) {
|
||||
if (!$node instanceof Assign) {
|
||||
return null;
|
||||
}
|
||||
if (!$this->nodeComparator->areNodesEqual($node->expr, $singleTokenVariable)) {
|
||||
return null;
|
||||
}
|
||||
$tokenStaticType = $this->nodeTypeResolver->getType($node->expr);
|
||||
if ($tokenStaticType->isArray()->yes()) {
|
||||
return null;
|
||||
}
|
||||
$node->expr = new PropertyFetch($singleTokenVariable, 'text');
|
||||
});
|
||||
// replace "$name = null;" → "$name = $token->getTokenName();"
|
||||
$this->simpleCallableNodeTraverser->traverseNodesWithCallable($nodes, function (Node $node) use($singleTokenVariable) : ?Assign {
|
||||
if (!$node instanceof Assign) {
|
||||
return null;
|
||||
}
|
||||
$tokenStaticType = $this->nodeTypeResolver->getType($node->expr);
|
||||
if ($tokenStaticType->isArray()->yes()) {
|
||||
return null;
|
||||
}
|
||||
if (!$this->assignedNameExpr instanceof Expr) {
|
||||
return null;
|
||||
}
|
||||
if (!$this->nodeComparator->areNodesEqual($node->var, $this->assignedNameExpr)) {
|
||||
return null;
|
||||
}
|
||||
if (!$this->valueResolver->isValue($node->expr, 'null')) {
|
||||
return null;
|
||||
}
|
||||
$node->expr = new MethodCall($singleTokenVariable, 'getTokenName');
|
||||
return $node;
|
||||
});
|
||||
}
|
||||
/**
|
||||
* @param Node[] $nodes
|
||||
*/
|
||||
public function refactorTokenIsKind(array $nodes, Variable $singleTokenVariable) : void
|
||||
{
|
||||
$this->simpleCallableNodeTraverser->traverseNodesWithCallable($nodes, function (Node $node) use($singleTokenVariable) {
|
||||
if (!$this->isIdenticalOrNotIdentical($node)) {
|
||||
return null;
|
||||
}
|
||||
/** @var Identical|NotIdentical $node */
|
||||
$arrayDimFetchAndConstFetch = $this->matchArrayDimFetchAndConstFetch($node);
|
||||
if (!$arrayDimFetchAndConstFetch instanceof ArrayDimFetchAndConstFetch) {
|
||||
return null;
|
||||
}
|
||||
if (!$this->isArrayDimFetchWithDimIntegerValue($arrayDimFetchAndConstFetch->getArrayDimFetch(), 0)) {
|
||||
return null;
|
||||
}
|
||||
$arrayDimFetch = $arrayDimFetchAndConstFetch->getArrayDimFetch();
|
||||
$constFetch = $arrayDimFetchAndConstFetch->getConstFetch();
|
||||
if (!$this->nodeComparator->areNodesEqual($arrayDimFetch->var, $singleTokenVariable)) {
|
||||
return null;
|
||||
}
|
||||
$constName = (string) $this->nodeNameResolver->getName($constFetch);
|
||||
if (!StringUtils::isMatch($constName, '#^T_#')) {
|
||||
return null;
|
||||
}
|
||||
$isTConstTypeMethodCall = $this->createIsTConstTypeMethodCall($arrayDimFetch, $arrayDimFetchAndConstFetch->getConstFetch());
|
||||
if ($node instanceof Identical) {
|
||||
return $isTConstTypeMethodCall;
|
||||
}
|
||||
return new BooleanNot($isTConstTypeMethodCall);
|
||||
});
|
||||
}
|
||||
/**
|
||||
* @param Node[] $nodes
|
||||
*/
|
||||
public function removeIsArray(array $nodes, Variable $singleTokenVariable) : void
|
||||
{
|
||||
$this->simpleCallableNodeTraverser->traverseNodesWithCallable($nodes, function (Node $node) use($singleTokenVariable) : ?FuncCall {
|
||||
if (!$node instanceof FuncCall) {
|
||||
return null;
|
||||
}
|
||||
if (!$this->nodeNameResolver->isName($node, 'is_array')) {
|
||||
return null;
|
||||
}
|
||||
if ($node->isFirstClassCallable()) {
|
||||
return null;
|
||||
}
|
||||
$firstArg = $node->getArgs()[0];
|
||||
if (!$this->nodeComparator->areNodesEqual($firstArg->value, $singleTokenVariable)) {
|
||||
return null;
|
||||
}
|
||||
if ($this->shouldSkipNodeRemovalForPartOfIf($node)) {
|
||||
return null;
|
||||
}
|
||||
// remove correct node
|
||||
$nodeToRemove = $this->matchParentNodeInCaseOfIdenticalTrue($node);
|
||||
$parentNode = $nodeToRemove->getAttribute(AttributeKey::PARENT_NODE);
|
||||
if ($parentNode instanceof Ternary) {
|
||||
$this->replaceTernary($parentNode);
|
||||
return $node;
|
||||
}
|
||||
if (!$parentNode instanceof BooleanNot) {
|
||||
$this->nodesToRemoveCollector->addNodeToRemove($nodeToRemove);
|
||||
return $node;
|
||||
}
|
||||
$parentOfParentNode = $parentNode->getAttribute(AttributeKey::PARENT_NODE);
|
||||
if ($parentOfParentNode instanceof BinaryOp) {
|
||||
$this->nodesToRemoveCollector->addNodeToRemove($parentNode);
|
||||
return $node;
|
||||
}
|
||||
$this->nodesToRemoveCollector->addNodeToRemove($nodeToRemove);
|
||||
return $node;
|
||||
});
|
||||
}
|
||||
private function isIdenticalOrNotIdentical(Node $node) : bool
|
||||
{
|
||||
return $node instanceof Identical || $node instanceof NotIdentical;
|
||||
}
|
||||
private function replaceTernary(Ternary $ternary) : void
|
||||
{
|
||||
$currentStmt = $this->betterNodeFinder->resolveCurrentStatement($ternary);
|
||||
$this->simpleCallableNodeTraverser->traverseNodesWithCallable($currentStmt, static function (Node $subNode) use($ternary) : ?Expr {
|
||||
if ($subNode === $ternary) {
|
||||
return $ternary->if;
|
||||
}
|
||||
return null;
|
||||
});
|
||||
}
|
||||
/**
|
||||
* Replace $token[0] with $token->getTokenName() call
|
||||
*
|
||||
* @param Node[] $nodes
|
||||
*/
|
||||
private function replaceTokenDimFetchZeroWithGetTokenName(array $nodes, Variable $singleTokenVariable) : void
|
||||
{
|
||||
$this->simpleCallableNodeTraverser->traverseNodesWithCallable($nodes, function (Node $node) use($singleTokenVariable) : ?MethodCall {
|
||||
if (!$node instanceof FuncCall) {
|
||||
return null;
|
||||
}
|
||||
if (!$this->nodeNameResolver->isName($node, 'token_name')) {
|
||||
return null;
|
||||
}
|
||||
$firstArg = $node->getArgs()[0] ?? null;
|
||||
if (!$firstArg instanceof Arg) {
|
||||
return null;
|
||||
}
|
||||
$possibleTokenArray = $firstArg->value;
|
||||
if (!$possibleTokenArray instanceof ArrayDimFetch) {
|
||||
return null;
|
||||
}
|
||||
$tokenStaticType = $this->nodeTypeResolver->getType($possibleTokenArray->var);
|
||||
if (!$tokenStaticType->isArray()->yes()) {
|
||||
return null;
|
||||
}
|
||||
if (!$possibleTokenArray->dim instanceof Expr) {
|
||||
return null;
|
||||
}
|
||||
if (!$this->valueResolver->isValue($possibleTokenArray->dim, 0)) {
|
||||
return null;
|
||||
}
|
||||
if (!$this->nodeComparator->areNodesEqual($possibleTokenArray->var, $singleTokenVariable)) {
|
||||
return null;
|
||||
}
|
||||
// save token variable name for later
|
||||
$parentNode = $node->getAttribute(AttributeKey::PARENT_NODE);
|
||||
if ($parentNode instanceof Assign) {
|
||||
$this->assignedNameExpr = $parentNode->var;
|
||||
}
|
||||
return new MethodCall($singleTokenVariable, 'getTokenName');
|
||||
});
|
||||
}
|
||||
private function isArrayDimFetchWithDimIntegerValue(ArrayDimFetch $arrayDimFetch, int $value) : bool
|
||||
{
|
||||
if (!$arrayDimFetch->dim instanceof Expr) {
|
||||
return \false;
|
||||
}
|
||||
return $this->valueResolver->isValue($arrayDimFetch->dim, $value);
|
||||
}
|
||||
/**
|
||||
* @param \PhpParser\Node\Expr\BinaryOp\Identical|\PhpParser\Node\Expr\BinaryOp\NotIdentical $identical
|
||||
*/
|
||||
private function matchArrayDimFetchAndConstFetch($identical) : ?ArrayDimFetchAndConstFetch
|
||||
{
|
||||
if ($identical->left instanceof ArrayDimFetch && $identical->right instanceof ConstFetch) {
|
||||
return new ArrayDimFetchAndConstFetch($identical->left, $identical->right);
|
||||
}
|
||||
if (!$identical->right instanceof ArrayDimFetch) {
|
||||
return null;
|
||||
}
|
||||
if (!$identical->left instanceof ConstFetch) {
|
||||
return null;
|
||||
}
|
||||
return new ArrayDimFetchAndConstFetch($identical->right, $identical->left);
|
||||
}
|
||||
private function createIsTConstTypeMethodCall(ArrayDimFetch $arrayDimFetch, ConstFetch $constFetch) : MethodCall
|
||||
{
|
||||
return new MethodCall($arrayDimFetch->var, 'is', [new Arg($constFetch)]);
|
||||
}
|
||||
private function shouldSkipNodeRemovalForPartOfIf(FuncCall $funcCall) : bool
|
||||
{
|
||||
$parentNode = $funcCall->getAttribute(AttributeKey::PARENT_NODE);
|
||||
// cannot remove x from if(x)
|
||||
if ($parentNode instanceof If_ && $parentNode->cond === $funcCall) {
|
||||
return \true;
|
||||
}
|
||||
if (!$parentNode instanceof BooleanNot) {
|
||||
return \false;
|
||||
}
|
||||
$parentParentNode = $parentNode->getAttribute(AttributeKey::PARENT_NODE);
|
||||
if ($parentParentNode instanceof If_) {
|
||||
$parentParentNode->cond = $parentNode;
|
||||
return \true;
|
||||
}
|
||||
return \false;
|
||||
}
|
||||
/**
|
||||
* @return \PhpParser\Node\Expr\BinaryOp\Identical|\PhpParser\Node\Expr\FuncCall
|
||||
*/
|
||||
private function matchParentNodeInCaseOfIdenticalTrue(FuncCall $funcCall)
|
||||
{
|
||||
$parentNode = $funcCall->getAttribute(AttributeKey::PARENT_NODE);
|
||||
if (!$parentNode instanceof Identical) {
|
||||
return $funcCall;
|
||||
}
|
||||
$isRightValueTrue = $this->valueResolver->isValue($parentNode->right, \true);
|
||||
if ($parentNode->left === $funcCall && $isRightValueTrue) {
|
||||
return $parentNode;
|
||||
}
|
||||
$isLeftValueTrue = $this->valueResolver->isValue($parentNode->left, \true);
|
||||
if ($parentNode->right !== $funcCall) {
|
||||
return $funcCall;
|
||||
}
|
||||
if (!$isLeftValueTrue) {
|
||||
return $funcCall;
|
||||
}
|
||||
return $parentNode;
|
||||
}
|
||||
}
|
@ -1,180 +0,0 @@
|
||||
<?php
|
||||
|
||||
declare (strict_types=1);
|
||||
namespace Rector\Php80\Rector\FuncCall;
|
||||
|
||||
use PhpParser\Node;
|
||||
use PhpParser\Node\Expr;
|
||||
use PhpParser\Node\Expr\Assign;
|
||||
use PhpParser\Node\Expr\FuncCall;
|
||||
use PhpParser\Node\Expr\Variable;
|
||||
use PhpParser\Node\Stmt;
|
||||
use PhpParser\Node\Stmt\ClassMethod;
|
||||
use PhpParser\Node\Stmt\Else_;
|
||||
use PhpParser\Node\Stmt\Foreach_;
|
||||
use PhpParser\Node\Stmt\Function_;
|
||||
use PhpParser\Node\Stmt\If_;
|
||||
use PhpToken;
|
||||
use Rector\Core\Rector\AbstractRector;
|
||||
use Rector\Core\ValueObject\PhpVersionFeature;
|
||||
use Rector\NodeTypeResolver\Node\AttributeKey;
|
||||
use Rector\Php80\NodeManipulator\TokenManipulator;
|
||||
use Rector\PostRector\Collector\NodesToAddCollector;
|
||||
use Rector\VersionBonding\Contract\MinPhpVersionInterface;
|
||||
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
|
||||
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
|
||||
/**
|
||||
* @changelog https://wiki.php.net/rfc/token_as_object
|
||||
*
|
||||
* @see \Rector\Tests\Php80\Rector\FuncCall\TokenGetAllToObjectRector\TokenGetAllToObjectRectorTest
|
||||
*/
|
||||
final class TokenGetAllToObjectRector extends AbstractRector implements MinPhpVersionInterface
|
||||
{
|
||||
/**
|
||||
* @readonly
|
||||
* @var \Rector\Php80\NodeManipulator\TokenManipulator
|
||||
*/
|
||||
private $tokenManipulator;
|
||||
/**
|
||||
* @readonly
|
||||
* @var \Rector\PostRector\Collector\NodesToAddCollector
|
||||
*/
|
||||
private $nodesToAddCollector;
|
||||
public function __construct(TokenManipulator $tokenManipulator, NodesToAddCollector $nodesToAddCollector)
|
||||
{
|
||||
$this->tokenManipulator = $tokenManipulator;
|
||||
$this->nodesToAddCollector = $nodesToAddCollector;
|
||||
}
|
||||
public function provideMinPhpVersion() : int
|
||||
{
|
||||
return PhpVersionFeature::PHP_TOKEN;
|
||||
}
|
||||
public function getRuleDefinition() : RuleDefinition
|
||||
{
|
||||
return new RuleDefinition('Convert `token_get_all` to `' . PhpToken::class . '::tokenize`', [new CodeSample(<<<'CODE_SAMPLE'
|
||||
final class SomeClass
|
||||
{
|
||||
public function run()
|
||||
{
|
||||
$tokens = token_get_all($code);
|
||||
foreach ($tokens as $token) {
|
||||
if (is_array($token)) {
|
||||
$name = token_name($token[0]);
|
||||
$text = $token[1];
|
||||
} else {
|
||||
$name = null;
|
||||
$text = $token;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
CODE_SAMPLE
|
||||
, <<<'CODE_SAMPLE'
|
||||
final class SomeClass
|
||||
{
|
||||
public function run()
|
||||
{
|
||||
$tokens = \PhpToken::tokenize($code);
|
||||
foreach ($tokens as $phpToken) {
|
||||
$name = $phpToken->getTokenName();
|
||||
$text = $phpToken->text;
|
||||
}
|
||||
}
|
||||
}
|
||||
CODE_SAMPLE
|
||||
)]);
|
||||
}
|
||||
/**
|
||||
* @return array<class-string<Node>>
|
||||
*/
|
||||
public function getNodeTypes() : array
|
||||
{
|
||||
return [Assign::class];
|
||||
}
|
||||
/**
|
||||
* @param Assign $node
|
||||
*/
|
||||
public function refactor(Node $node) : ?Node
|
||||
{
|
||||
if (!$node->expr instanceof FuncCall) {
|
||||
return null;
|
||||
}
|
||||
$funcCall = $node->expr;
|
||||
if (!$this->nodeNameResolver->isName($funcCall, 'token_get_all')) {
|
||||
return null;
|
||||
}
|
||||
$this->refactorTokensVariable($funcCall, $node->var);
|
||||
$node->expr = $this->nodeFactory->createStaticCall('PhpToken', 'tokenize', $funcCall->getArgs());
|
||||
return $node;
|
||||
}
|
||||
private function refactorTokensVariable(FuncCall $funcCall, Expr $assignedToExpr) : void
|
||||
{
|
||||
/** @var ClassMethod|Function_|null $classMethodOrFunction */
|
||||
$classMethodOrFunction = $this->betterNodeFinder->findParentByTypes($funcCall, [ClassMethod::class, Function_::class]);
|
||||
if ($classMethodOrFunction === null) {
|
||||
return;
|
||||
}
|
||||
// dummy approach, improve when needed
|
||||
$this->replaceGetNameOrGetValue($classMethodOrFunction, $assignedToExpr);
|
||||
}
|
||||
/**
|
||||
* @param \PhpParser\Node\Stmt\ClassMethod|\PhpParser\Node\Stmt\Function_ $functionLike
|
||||
*/
|
||||
private function replaceGetNameOrGetValue($functionLike, Expr $assignedExpr) : void
|
||||
{
|
||||
$tokensForeaches = $this->findForeachesOverTokenVariable($functionLike, $assignedExpr);
|
||||
foreach ($tokensForeaches as $tokenForeach) {
|
||||
$this->refactorTokenInForeach($tokenForeach);
|
||||
}
|
||||
}
|
||||
/**
|
||||
* @return Foreach_[]
|
||||
* @param \PhpParser\Node\Stmt\ClassMethod|\PhpParser\Node\Stmt\Function_ $functionLike
|
||||
*/
|
||||
private function findForeachesOverTokenVariable($functionLike, Expr $assignedExpr) : array
|
||||
{
|
||||
return $this->betterNodeFinder->find((array) $functionLike->stmts, function (Node $node) use($assignedExpr) : bool {
|
||||
if (!$node instanceof Foreach_) {
|
||||
return \false;
|
||||
}
|
||||
return $this->nodeComparator->areNodesEqual($node->expr, $assignedExpr);
|
||||
});
|
||||
}
|
||||
private function refactorTokenInForeach(Foreach_ $tokensForeach) : void
|
||||
{
|
||||
$singleToken = $tokensForeach->valueVar;
|
||||
if (!$singleToken instanceof Variable) {
|
||||
return;
|
||||
}
|
||||
$this->traverseNodesWithCallable($tokensForeach, function (Node $node) use($singleToken) {
|
||||
$this->tokenManipulator->refactorArrayToken([$node], $singleToken);
|
||||
$this->tokenManipulator->refactorNonArrayToken([$node], $singleToken);
|
||||
$this->tokenManipulator->refactorTokenIsKind([$node], $singleToken);
|
||||
$this->tokenManipulator->removeIsArray([$node], $singleToken);
|
||||
// drop if "If_" node not needed
|
||||
if ($node instanceof If_ && $node->else instanceof Else_) {
|
||||
if (!$this->nodeComparator->areNodesEqual($node->stmts, $node->else->stmts)) {
|
||||
return null;
|
||||
}
|
||||
$this->unwrapStmts($node->stmts, $node);
|
||||
$this->removeNode($node);
|
||||
}
|
||||
});
|
||||
}
|
||||
/**
|
||||
* @param Stmt[] $stmts
|
||||
*/
|
||||
private function unwrapStmts(array $stmts, If_ $if) : void
|
||||
{
|
||||
// move /* */ doc block from if to first element to keep it
|
||||
$currentPhpDocInfo = $this->phpDocInfoFactory->createFromNodeOrEmpty($if);
|
||||
foreach ($stmts as $key => $stmt) {
|
||||
if ($key === 0) {
|
||||
$stmt->setAttribute(AttributeKey::PHP_DOC_INFO, $currentPhpDocInfo);
|
||||
// move // comments
|
||||
$stmt->setAttribute(AttributeKey::COMMENTS, $if->getComments());
|
||||
}
|
||||
$this->nodesToAddCollector->addNodeAfterNode($stmt, $if);
|
||||
}
|
||||
}
|
||||
}
|
@ -1,33 +0,0 @@
|
||||
<?php
|
||||
|
||||
declare (strict_types=1);
|
||||
namespace Rector\Php80\ValueObject;
|
||||
|
||||
use PhpParser\Node\Expr\ArrayDimFetch;
|
||||
use PhpParser\Node\Expr\ConstFetch;
|
||||
final class ArrayDimFetchAndConstFetch
|
||||
{
|
||||
/**
|
||||
* @readonly
|
||||
* @var \PhpParser\Node\Expr\ArrayDimFetch
|
||||
*/
|
||||
private $arrayDimFetch;
|
||||
/**
|
||||
* @readonly
|
||||
* @var \PhpParser\Node\Expr\ConstFetch
|
||||
*/
|
||||
private $constFetch;
|
||||
public function __construct(ArrayDimFetch $arrayDimFetch, ConstFetch $constFetch)
|
||||
{
|
||||
$this->arrayDimFetch = $arrayDimFetch;
|
||||
$this->constFetch = $constFetch;
|
||||
}
|
||||
public function getArrayDimFetch() : ArrayDimFetch
|
||||
{
|
||||
return $this->arrayDimFetch;
|
||||
}
|
||||
public function getConstFetch() : ConstFetch
|
||||
{
|
||||
return $this->constFetch;
|
||||
}
|
||||
}
|
@ -19,12 +19,12 @@ final class VersionResolver
|
||||
* @api
|
||||
* @var string
|
||||
*/
|
||||
public const PACKAGE_VERSION = 'ec2893301809a41a4629aabc3888b968a9613f5e';
|
||||
public const PACKAGE_VERSION = '25ae1d85be40243e03c950de3ac15af7b5b5d3b2';
|
||||
/**
|
||||
* @api
|
||||
* @var string
|
||||
*/
|
||||
public const RELEASE_DATE = '2023-05-28 21:36:04';
|
||||
public const RELEASE_DATE = '2023-05-28 17:06:49';
|
||||
/**
|
||||
* @var int
|
||||
*/
|
||||
|
2
vendor/autoload.php
vendored
2
vendor/autoload.php
vendored
@ -22,4 +22,4 @@ if (PHP_VERSION_ID < 50600) {
|
||||
|
||||
require_once __DIR__ . '/composer/autoload_real.php';
|
||||
|
||||
return ComposerAutoloaderInit6076ce74d9e96414cbbb1ab6950f98c9::getLoader();
|
||||
return ComposerAutoloaderInita41c3b6146b1bbab2ff5b5e0492ac87d::getLoader();
|
||||
|
3
vendor/composer/autoload_classmap.php
vendored
3
vendor/composer/autoload_classmap.php
vendored
@ -2261,7 +2261,6 @@ return array(
|
||||
'Rector\\Php80\\NodeFactory\\StrStartsWithFuncCallFactory' => $baseDir . '/rules/Php80/NodeFactory/StrStartsWithFuncCallFactory.php',
|
||||
'Rector\\Php80\\NodeManipulator\\AttributeGroupNamedArgumentManipulator' => $baseDir . '/rules/Php80/NodeManipulator/AttributeGroupNamedArgumentManipulator.php',
|
||||
'Rector\\Php80\\NodeManipulator\\ResourceReturnToObject' => $baseDir . '/rules/Php80/NodeManipulator/ResourceReturnToObject.php',
|
||||
'Rector\\Php80\\NodeManipulator\\TokenManipulator' => $baseDir . '/rules/Php80/NodeManipulator/TokenManipulator.php',
|
||||
'Rector\\Php80\\NodeResolver\\ArgumentSorter' => $baseDir . '/rules/Php80/NodeResolver/ArgumentSorter.php',
|
||||
'Rector\\Php80\\NodeResolver\\RequireOptionalParamResolver' => $baseDir . '/rules/Php80/NodeResolver/RequireOptionalParamResolver.php',
|
||||
'Rector\\Php80\\NodeResolver\\SwitchExprsResolver' => $baseDir . '/rules/Php80/NodeResolver/SwitchExprsResolver.php',
|
||||
@ -2276,7 +2275,6 @@ return array(
|
||||
'Rector\\Php80\\Rector\\Class_\\StringableForToStringRector' => $baseDir . '/rules/Php80/Rector/Class_/StringableForToStringRector.php',
|
||||
'Rector\\Php80\\Rector\\FuncCall\\ClassOnObjectRector' => $baseDir . '/rules/Php80/Rector/FuncCall/ClassOnObjectRector.php',
|
||||
'Rector\\Php80\\Rector\\FuncCall\\Php8ResourceReturnToObjectRector' => $baseDir . '/rules/Php80/Rector/FuncCall/Php8ResourceReturnToObjectRector.php',
|
||||
'Rector\\Php80\\Rector\\FuncCall\\TokenGetAllToObjectRector' => $baseDir . '/rules/Php80/Rector/FuncCall/TokenGetAllToObjectRector.php',
|
||||
'Rector\\Php80\\Rector\\FunctionLike\\MixedTypeRector' => $baseDir . '/rules/Php80/Rector/FunctionLike/MixedTypeRector.php',
|
||||
'Rector\\Php80\\Rector\\FunctionLike\\UnionTypesRector' => $baseDir . '/rules/Php80/Rector/FunctionLike/UnionTypesRector.php',
|
||||
'Rector\\Php80\\Rector\\Identical\\StrEndsWithRector' => $baseDir . '/rules/Php80/Rector/Identical/StrEndsWithRector.php',
|
||||
@ -2288,7 +2286,6 @@ return array(
|
||||
'Rector\\Php80\\ValueObjectFactory\\StrStartsWithFactory' => $baseDir . '/rules/Php80/ValueObjectFactory/StrStartsWithFactory.php',
|
||||
'Rector\\Php80\\ValueObject\\AnnotationPropertyToAttributeClass' => $baseDir . '/rules/Php80/ValueObject/AnnotationPropertyToAttributeClass.php',
|
||||
'Rector\\Php80\\ValueObject\\AnnotationToAttribute' => $baseDir . '/rules/Php80/ValueObject/AnnotationToAttribute.php',
|
||||
'Rector\\Php80\\ValueObject\\ArrayDimFetchAndConstFetch' => $baseDir . '/rules/Php80/ValueObject/ArrayDimFetchAndConstFetch.php',
|
||||
'Rector\\Php80\\ValueObject\\ClassNameAndTagValueNode' => $baseDir . '/rules/Php80/ValueObject/ClassNameAndTagValueNode.php',
|
||||
'Rector\\Php80\\ValueObject\\CondAndExpr' => $baseDir . '/rules/Php80/ValueObject/CondAndExpr.php',
|
||||
'Rector\\Php80\\ValueObject\\DoctrineTagAndAnnotationToAttribute' => $baseDir . '/rules/Php80/ValueObject/DoctrineTagAndAnnotationToAttribute.php',
|
||||
|
10
vendor/composer/autoload_real.php
vendored
10
vendor/composer/autoload_real.php
vendored
@ -2,7 +2,7 @@
|
||||
|
||||
// autoload_real.php @generated by Composer
|
||||
|
||||
class ComposerAutoloaderInit6076ce74d9e96414cbbb1ab6950f98c9
|
||||
class ComposerAutoloaderInita41c3b6146b1bbab2ff5b5e0492ac87d
|
||||
{
|
||||
private static $loader;
|
||||
|
||||
@ -22,17 +22,17 @@ class ComposerAutoloaderInit6076ce74d9e96414cbbb1ab6950f98c9
|
||||
return self::$loader;
|
||||
}
|
||||
|
||||
spl_autoload_register(array('ComposerAutoloaderInit6076ce74d9e96414cbbb1ab6950f98c9', 'loadClassLoader'), true, true);
|
||||
spl_autoload_register(array('ComposerAutoloaderInita41c3b6146b1bbab2ff5b5e0492ac87d', 'loadClassLoader'), true, true);
|
||||
self::$loader = $loader = new \Composer\Autoload\ClassLoader(\dirname(__DIR__));
|
||||
spl_autoload_unregister(array('ComposerAutoloaderInit6076ce74d9e96414cbbb1ab6950f98c9', 'loadClassLoader'));
|
||||
spl_autoload_unregister(array('ComposerAutoloaderInita41c3b6146b1bbab2ff5b5e0492ac87d', 'loadClassLoader'));
|
||||
|
||||
require __DIR__ . '/autoload_static.php';
|
||||
call_user_func(\Composer\Autoload\ComposerStaticInit6076ce74d9e96414cbbb1ab6950f98c9::getInitializer($loader));
|
||||
call_user_func(\Composer\Autoload\ComposerStaticInita41c3b6146b1bbab2ff5b5e0492ac87d::getInitializer($loader));
|
||||
|
||||
$loader->setClassMapAuthoritative(true);
|
||||
$loader->register(true);
|
||||
|
||||
$filesToLoad = \Composer\Autoload\ComposerStaticInit6076ce74d9e96414cbbb1ab6950f98c9::$files;
|
||||
$filesToLoad = \Composer\Autoload\ComposerStaticInita41c3b6146b1bbab2ff5b5e0492ac87d::$files;
|
||||
$requireFile = \Closure::bind(static function ($fileIdentifier, $file) {
|
||||
if (empty($GLOBALS['__composer_autoload_files'][$fileIdentifier])) {
|
||||
$GLOBALS['__composer_autoload_files'][$fileIdentifier] = true;
|
||||
|
11
vendor/composer/autoload_static.php
vendored
11
vendor/composer/autoload_static.php
vendored
@ -4,7 +4,7 @@
|
||||
|
||||
namespace Composer\Autoload;
|
||||
|
||||
class ComposerStaticInit6076ce74d9e96414cbbb1ab6950f98c9
|
||||
class ComposerStaticInita41c3b6146b1bbab2ff5b5e0492ac87d
|
||||
{
|
||||
public static $files = array (
|
||||
'ad155f8f1cf0d418fe49e248db8c661b' => __DIR__ . '/..' . '/react/promise/src/functions_include.php',
|
||||
@ -2503,7 +2503,6 @@ class ComposerStaticInit6076ce74d9e96414cbbb1ab6950f98c9
|
||||
'Rector\\Php80\\NodeFactory\\StrStartsWithFuncCallFactory' => __DIR__ . '/../..' . '/rules/Php80/NodeFactory/StrStartsWithFuncCallFactory.php',
|
||||
'Rector\\Php80\\NodeManipulator\\AttributeGroupNamedArgumentManipulator' => __DIR__ . '/../..' . '/rules/Php80/NodeManipulator/AttributeGroupNamedArgumentManipulator.php',
|
||||
'Rector\\Php80\\NodeManipulator\\ResourceReturnToObject' => __DIR__ . '/../..' . '/rules/Php80/NodeManipulator/ResourceReturnToObject.php',
|
||||
'Rector\\Php80\\NodeManipulator\\TokenManipulator' => __DIR__ . '/../..' . '/rules/Php80/NodeManipulator/TokenManipulator.php',
|
||||
'Rector\\Php80\\NodeResolver\\ArgumentSorter' => __DIR__ . '/../..' . '/rules/Php80/NodeResolver/ArgumentSorter.php',
|
||||
'Rector\\Php80\\NodeResolver\\RequireOptionalParamResolver' => __DIR__ . '/../..' . '/rules/Php80/NodeResolver/RequireOptionalParamResolver.php',
|
||||
'Rector\\Php80\\NodeResolver\\SwitchExprsResolver' => __DIR__ . '/../..' . '/rules/Php80/NodeResolver/SwitchExprsResolver.php',
|
||||
@ -2518,7 +2517,6 @@ class ComposerStaticInit6076ce74d9e96414cbbb1ab6950f98c9
|
||||
'Rector\\Php80\\Rector\\Class_\\StringableForToStringRector' => __DIR__ . '/../..' . '/rules/Php80/Rector/Class_/StringableForToStringRector.php',
|
||||
'Rector\\Php80\\Rector\\FuncCall\\ClassOnObjectRector' => __DIR__ . '/../..' . '/rules/Php80/Rector/FuncCall/ClassOnObjectRector.php',
|
||||
'Rector\\Php80\\Rector\\FuncCall\\Php8ResourceReturnToObjectRector' => __DIR__ . '/../..' . '/rules/Php80/Rector/FuncCall/Php8ResourceReturnToObjectRector.php',
|
||||
'Rector\\Php80\\Rector\\FuncCall\\TokenGetAllToObjectRector' => __DIR__ . '/../..' . '/rules/Php80/Rector/FuncCall/TokenGetAllToObjectRector.php',
|
||||
'Rector\\Php80\\Rector\\FunctionLike\\MixedTypeRector' => __DIR__ . '/../..' . '/rules/Php80/Rector/FunctionLike/MixedTypeRector.php',
|
||||
'Rector\\Php80\\Rector\\FunctionLike\\UnionTypesRector' => __DIR__ . '/../..' . '/rules/Php80/Rector/FunctionLike/UnionTypesRector.php',
|
||||
'Rector\\Php80\\Rector\\Identical\\StrEndsWithRector' => __DIR__ . '/../..' . '/rules/Php80/Rector/Identical/StrEndsWithRector.php',
|
||||
@ -2530,7 +2528,6 @@ class ComposerStaticInit6076ce74d9e96414cbbb1ab6950f98c9
|
||||
'Rector\\Php80\\ValueObjectFactory\\StrStartsWithFactory' => __DIR__ . '/../..' . '/rules/Php80/ValueObjectFactory/StrStartsWithFactory.php',
|
||||
'Rector\\Php80\\ValueObject\\AnnotationPropertyToAttributeClass' => __DIR__ . '/../..' . '/rules/Php80/ValueObject/AnnotationPropertyToAttributeClass.php',
|
||||
'Rector\\Php80\\ValueObject\\AnnotationToAttribute' => __DIR__ . '/../..' . '/rules/Php80/ValueObject/AnnotationToAttribute.php',
|
||||
'Rector\\Php80\\ValueObject\\ArrayDimFetchAndConstFetch' => __DIR__ . '/../..' . '/rules/Php80/ValueObject/ArrayDimFetchAndConstFetch.php',
|
||||
'Rector\\Php80\\ValueObject\\ClassNameAndTagValueNode' => __DIR__ . '/../..' . '/rules/Php80/ValueObject/ClassNameAndTagValueNode.php',
|
||||
'Rector\\Php80\\ValueObject\\CondAndExpr' => __DIR__ . '/../..' . '/rules/Php80/ValueObject/CondAndExpr.php',
|
||||
'Rector\\Php80\\ValueObject\\DoctrineTagAndAnnotationToAttribute' => __DIR__ . '/../..' . '/rules/Php80/ValueObject/DoctrineTagAndAnnotationToAttribute.php',
|
||||
@ -3089,9 +3086,9 @@ class ComposerStaticInit6076ce74d9e96414cbbb1ab6950f98c9
|
||||
public static function getInitializer(ClassLoader $loader)
|
||||
{
|
||||
return \Closure::bind(function () use ($loader) {
|
||||
$loader->prefixLengthsPsr4 = ComposerStaticInit6076ce74d9e96414cbbb1ab6950f98c9::$prefixLengthsPsr4;
|
||||
$loader->prefixDirsPsr4 = ComposerStaticInit6076ce74d9e96414cbbb1ab6950f98c9::$prefixDirsPsr4;
|
||||
$loader->classMap = ComposerStaticInit6076ce74d9e96414cbbb1ab6950f98c9::$classMap;
|
||||
$loader->prefixLengthsPsr4 = ComposerStaticInita41c3b6146b1bbab2ff5b5e0492ac87d::$prefixLengthsPsr4;
|
||||
$loader->prefixDirsPsr4 = ComposerStaticInita41c3b6146b1bbab2ff5b5e0492ac87d::$prefixDirsPsr4;
|
||||
$loader->classMap = ComposerStaticInita41c3b6146b1bbab2ff5b5e0492ac87d::$classMap;
|
||||
|
||||
}, null, ClassLoader::class);
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user