Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@ class AlreadyExistingItemsCount
public function run($items, \stdClass $someObject)
{
$itemsCount = 500000;
$itemsCount2 = count($someObject->getItems() + 10);
$counter = count($someObject->getItems() + 10);

for ($i = 5; $i <= $itemsCount2; $i++) {
for ($i = 5; $i <= $counter; $i++) {
echo $items[$i];
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ class FallbackForComplex
{
public function run($items, \stdClass $someObject)
{
$itemsCount = count($someObject->getItems() + 10);
for ($i = 5; $i <= $itemsCount; $i++) {
$counter = count($someObject->getItems() + 10);
for ($i = 5; $i <= $counter; $i++) {
echo $items[$i];
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ class Fixture
{
public function run($items)
{
$itemsCount = count($items);
for ($i = 5; $i <= $itemsCount; $i++) {
$counter = count($items);
for ($i = 5; $i <= $counter; $i++) {
echo $items[$i];
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ class MethodCallCount
{
public function run($items, \stdClass $someObject)
{
$getItemsCount = count($someObject->getItems());
for ($i = 5; $i <= $getItemsCount; $i++) {
$counter = count($someObject->getItems());
for ($i = 5; $i <= $counter; $i++) {
echo $items[$i];
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,19 @@
use PhpParser\Node\Stmt;
use PhpParser\Node\Stmt\Expression;
use PhpParser\Node\Stmt\For_;
use PHPStan\Analyser\Scope;
use Rector\Core\Rector\AbstractScopeAwareRector;
use Rector\Naming\Naming\VariableNaming;
use Rector\Core\Rector\AbstractRector;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;

/**
* @see \Rector\Tests\CodeQuality\Rector\For_\ForRepeatedCountToOwnVariableRector\ForRepeatedCountToOwnVariableRectorTest
*/
final class ForRepeatedCountToOwnVariableRector extends AbstractScopeAwareRector
final class ForRepeatedCountToOwnVariableRector extends AbstractRector
{
public function __construct(
private readonly VariableNaming $variableNaming
) {
}
/**
* @var string
*/
private const COUNTER_NAME = 'counter';

public function getRuleDefinition(): RuleDefinition
{
Expand Down Expand Up @@ -77,10 +75,10 @@ public function getNodeTypes(): array
* @param For_ $node
* @return Stmt[]|null
*/
public function refactorWithScope(Node $node, Scope $scope): ?array
public function refactor(Node $node): ?array
{
$variableName = null;
$countInCond = null;
$counterVariable = new Variable(self::COUNTER_NAME);

foreach ($node->cond as $condExpr) {
if (! $condExpr instanceof Smaller && ! $condExpr instanceof SmallerOrEqual) {
Expand All @@ -96,23 +94,16 @@ public function refactorWithScope(Node $node, Scope $scope): ?array
continue;
}

$variableName = $this->variableNaming->resolveFromFuncCallFirstArgumentWithSuffix(
$funcCall,
'Count',
'itemsCount',
$scope
);

$countInCond = $condExpr->right;

$condExpr->right = new Variable($variableName);
$condExpr->right = $counterVariable;
}

if (! is_string($variableName) || ! $countInCond instanceof Expr) {
if (! $countInCond instanceof Expr) {
return null;
}

$countAssign = new Assign(new Variable($variableName), $countInCond);
$countAssign = new Assign($counterVariable, $countInCond);

return [new Expression($countAssign), $node];
}
}
39 changes: 0 additions & 39 deletions rules/Naming/Naming/VariableNaming.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,16 +88,6 @@ public function createCountedValueName(string $valueName, ?Scope $scope): string
return $valueName;
}

public function resolveFromFuncCallFirstArgumentWithSuffix(
FuncCall $funcCall,
string $suffix,
string $fallbackName,
?Scope $scope
): string {
$bareName = $this->resolveBareFuncCallArgumentName($funcCall, $fallbackName, $suffix);
return $this->createCountedValueName($bareName, $scope);
}

private function resolveFromNodeAndType(Node $node, Type $type): ?string
{
$variableName = $this->resolveBareFromNode($node);
Expand Down Expand Up @@ -188,33 +178,4 @@ private function unwrapNode(Node $node): ?Node

return $node;
}

private function resolveBareFuncCallArgumentName(
FuncCall $funcCall,
string $fallbackName,
string $suffix
): string {
if ($funcCall->isFirstClassCallable()) {
return '';
}

if (! isset($funcCall->getArgs()[0])) {
return '';
}

$argumentValue = $funcCall->getArgs()[0]
->value;

if ($argumentValue instanceof MethodCall || $argumentValue instanceof StaticCall) {
$name = $this->nodeNameResolver->getName($argumentValue->name);
} else {
$name = $this->nodeNameResolver->getName($argumentValue);
}

if ($name === null) {
return $fallbackName;
}

return $name . $suffix;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ for ($i = 0; $i < sizeof($psychiatry); $i++) {
namespace Rector\Core\Tests\Issues\IndentationCrash\Fixture;

$psychiatry = [1, 2, 3];
$psychiatryCount = count($psychiatry);
$counter = count($psychiatry);

for ($i = 0; $i < $psychiatryCount; $i++) {
for ($i = 0; $i < $counter; $i++) {
$psychiatry[$i]["sufficient_cancellation_notice"] = mt_rand(1, 3) ? 1 : 0;
}

Expand Down
2 changes: 1 addition & 1 deletion tests/Issues/IssueTraitUseKey/config/configured_rule.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

declare(strict_types=1);

use Rector\Config\RectorConfig;
use Rector\CodingStyle\Rector\Use_\SeparateMultiUseImportsRector;
use Rector\Config\RectorConfig;
use Rector\Php80\Rector\Class_\ClassPropertyAssignToConstructorPromotionRector;
use Rector\Privatization\Rector\Class_\FinalizeClassesWithoutChildrenRector;

Expand Down