rector/rules/Order/StmtOrder.php

104 lines
2.6 KiB
PHP
Raw Normal View History

<?php
declare(strict_types=1);
namespace Rector\Order;
2021-03-21 19:41:17 +01:00
use PhpParser\Node;
use PhpParser\Node\Stmt\ClassLike;
use Rector\NodeNameResolver\NodeNameResolver;
/**
* @see \Rector\Tests\Order\StmtOrderTest
*/
final class StmtOrder
{
/**
* @var NodeNameResolver
*/
private $nodeNameResolver;
public function __construct(NodeNameResolver $nodeNameResolver)
{
$this->nodeNameResolver = $nodeNameResolver;
}
/**
* @param array<int, string> $desiredStmtOrder
* @param array<int, string> $currentStmtOrder
* @return array<int, int>
*/
public function createOldToNewKeys(array $desiredStmtOrder, array $currentStmtOrder): array
{
$newKeys = [];
[Naming] Register RenameForeachValueVariableToMatchExprVariableRector to naming config set (#5696) * [Naming] Register RenameForeachValueVariableToMatchExprVariableRector to naming config set * fix property fetch not from this * [ci-review] Rector Rectify * [ci-review] Rector Rectify * [ci-review] Rector Rectify * fix * [ci-review] Rector Rectify * [ci-review] Rector Rectify * fixture fix * phpstan * [ci-review] Rector Rectify * phpstan * extract to separate method for collect assigns by name * adding InflectorSingularResolver service * skip single prefix and length >= 40 * add failing fixture to skip plural camel case * use regex to get camel cases * implemented singularize camel cased * [ci-review] Rector Rectify * [ci-review] Rector Rectify * [ci-review] Rector Rectify * phpstan * handle singular verb news -> new * [ci-review] Rector Rectify * fixture fix * handle has * [ci-review] Rector Rectify * [ci-review] Rector Rectify * [ci-review] Rector Rectify * phpstan * phpstan * handle left side By * [ci-review] Rector Rectify * re-use resolve call * [ci-review] Rector Rectify * phpstan * [ci-review] Rector Rectify * final touch * final touch * [ci-review] Rector Rectify * [ci-review] Rector Rectify * use previous By in the middle * update $childClassReflection->hasProperty($propertyName) * [ci-review] Rector Rectify * catchKeys * regex fix * fixture * [ci-review] Rector Rectify * try use check array * Revert "try use check array" This reverts commit adb9f767f20ea2544e5ccfc9cfe361ecc929912a. * use files Co-authored-by: kaizen-ci <info@kaizen-ci.org>
2021-03-05 17:55:40 +07:00
foreach ($desiredStmtOrder as $singleDesiredStmtOrder) {
foreach ($currentStmtOrder as $currentKey => $classMethodName) {
[Naming] Register RenameForeachValueVariableToMatchExprVariableRector to naming config set (#5696) * [Naming] Register RenameForeachValueVariableToMatchExprVariableRector to naming config set * fix property fetch not from this * [ci-review] Rector Rectify * [ci-review] Rector Rectify * [ci-review] Rector Rectify * fix * [ci-review] Rector Rectify * [ci-review] Rector Rectify * fixture fix * phpstan * [ci-review] Rector Rectify * phpstan * extract to separate method for collect assigns by name * adding InflectorSingularResolver service * skip single prefix and length >= 40 * add failing fixture to skip plural camel case * use regex to get camel cases * implemented singularize camel cased * [ci-review] Rector Rectify * [ci-review] Rector Rectify * [ci-review] Rector Rectify * phpstan * handle singular verb news -> new * [ci-review] Rector Rectify * fixture fix * handle has * [ci-review] Rector Rectify * [ci-review] Rector Rectify * [ci-review] Rector Rectify * phpstan * phpstan * handle left side By * [ci-review] Rector Rectify * re-use resolve call * [ci-review] Rector Rectify * phpstan * [ci-review] Rector Rectify * final touch * final touch * [ci-review] Rector Rectify * [ci-review] Rector Rectify * use previous By in the middle * update $childClassReflection->hasProperty($propertyName) * [ci-review] Rector Rectify * catchKeys * regex fix * fixture * [ci-review] Rector Rectify * try use check array * Revert "try use check array" This reverts commit adb9f767f20ea2544e5ccfc9cfe361ecc929912a. * use files Co-authored-by: kaizen-ci <info@kaizen-ci.org>
2021-03-05 17:55:40 +07:00
if ($classMethodName === $singleDesiredStmtOrder) {
$newKeys[] = $currentKey;
}
}
}
$oldKeys = array_values($newKeys);
sort($oldKeys);
/** @var array<int, int> $oldToNewKeys */
$oldToNewKeys = array_combine($oldKeys, $newKeys);
return $oldToNewKeys;
}
/**
* @param array<int, int> $oldToNewKeys
*/
public function reorderClassStmtsByOldToNewKeys(ClassLike $classLike, array $oldToNewKeys): void
{
$reorderedStmts = [];
$stmtCount = count($classLike->stmts);
foreach ($classLike->stmts as $key => $stmt) {
if (! array_key_exists($key, $oldToNewKeys)) {
$reorderedStmts[$key] = $stmt;
continue;
}
// reorder here
$newKey = $oldToNewKeys[$key];
$reorderedStmts[$key] = $classLike->stmts[$newKey];
}
for ($i = 0; $i < $stmtCount; ++$i) {
if (! array_key_exists($i, $reorderedStmts)) {
continue;
}
$classLike->stmts[$i] = $reorderedStmts[$i];
}
}
/**
2021-03-21 19:41:17 +01:00
* @param class-string<Node> $type
2021-01-20 00:29:52 +01:00
* @return array<int, string>
*/
public function getStmtsOfTypeOrder(ClassLike $classLike, string $type): array
{
$stmtsByPosition = [];
foreach ($classLike->stmts as $position => $classStmt) {
if (! is_a($classStmt, $type)) {
continue;
}
$name = $this->nodeNameResolver->getName($classStmt);
if ($name === null) {
continue;
}
$stmtsByPosition[$position] = $name;
}
return $stmtsByPosition;
}
}