Skip to content

Commit

Permalink
[Privatization] Skip ChangeReadOnlyVariableWithDefaultValueToConstant…
Browse files Browse the repository at this point in the history
…Rector when local variable never used (#1693)

* [Privatization] Skip ChangeReadOnlyVariableWithDefaultValueToConstantRector when local variable never used

* [ci-review] Rector Rectify

* Fixed 🎉

* [ci-review] Rector Rectify

* [ci-review] Rector Rectify

* phpstan

* final touch: add test for combine ChangeReadOnlyVariableWithDefaultValueToConstantRector+RemoveUnusedVariableAssignRector

* final touch: using array_filter like in other method

Co-authored-by: GitHub Action <action@github.com>
  • Loading branch information
samsonasik and actions-user committed Jan 18, 2022
1 parent bf2f6dd commit 1c7460b
Show file tree
Hide file tree
Showing 6 changed files with 112 additions and 3 deletions.
4 changes: 2 additions & 2 deletions packages/NodeTypeResolver/TypeComparator/TypeComparator.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ public function arePhpParserAndPhpStanPhpDocTypesEqual(
return false;
}

return $this->isThisTypeInFinalClass($phpStanDocType, $phpParserNodeType, $node);
return $this->isThisTypeInFinalClass($phpStanDocType, $phpParserNodeType);
}

public function isSubtype(Type $checkedType, Type $mainType): bool
Expand Down Expand Up @@ -267,7 +267,7 @@ private function areTypesSameWithLiteralTypeInPhpDoc(
->yes();
}

private function isThisTypeInFinalClass(Type $phpStanDocType, Type $phpParserNodeType, Node $node): bool
private function isThisTypeInFinalClass(Type $phpStanDocType, Type $phpParserNodeType): bool
{

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

namespace Rector\Tests\Privatization\Rector\Class_\ChangeReadOnlyVariableWithDefaultValueToConstantRector\Fixture;

class SkipDefinedNeverUsed
{
public function run()
{
$replacements = [
'PHPUnit\Framework\TestCase\Notice' => 'expectNotice',
'PHPUnit\Framework\TestCase\Deprecated' => 'expectDeprecation',
];
}
}
22 changes: 21 additions & 1 deletion src/NodeManipulator/ClassMethodAssignManipulator.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
use Rector\Core\PhpParser\Node\BetterNodeFinder;
use Rector\Core\PhpParser\Node\NodeFactory;
use Rector\Core\Reflection\ReflectionResolver;
use Rector\DeadCode\NodeAnalyzer\ExprUsedInNextNodeAnalyzer;
use Rector\NodeNameResolver\NodeNameResolver;
use Rector\NodeTypeResolver\Node\AttributeKey;

Expand All @@ -41,7 +42,8 @@ public function __construct(
private readonly VariableManipulator $variableManipulator,
private readonly NodeComparator $nodeComparator,
private readonly ReflectionResolver $reflectionResolver,
private readonly ArrayDestructVariableFilter $arrayDestructVariableFilter
private readonly ArrayDestructVariableFilter $arrayDestructVariableFilter,
private readonly ExprUsedInNextNodeAnalyzer $exprUsedInNextNodeAnalyzer
) {
}

Expand All @@ -64,6 +66,12 @@ public function collectReadyOnlyAssignScalarVariables(ClassMethod $classMethod):
$readOnlyVariableAssigns = $this->filterOutMultiAssigns($readOnlyVariableAssigns);
$readOnlyVariableAssigns = $this->filterOutForeachVariables($readOnlyVariableAssigns);

/**
* Remove unused variable assign is task of RemoveUnusedVariableAssignRector
* so no need to move to constant early
*/
$readOnlyVariableAssigns = $this->filterOutNeverUsedNext($readOnlyVariableAssigns);

return $this->variableManipulator->filterOutChangedVariables($readOnlyVariableAssigns, $classMethod);
}

Expand All @@ -84,6 +92,18 @@ public function addParameterAndAssignToMethod(
$this->alreadyAddedClassMethodNames[$classMethodHash][] = $name;
}

/**
* @param Assign[] $readOnlyVariableAssigns
* @return Assign[]
*/
private function filterOutNeverUsedNext(array $readOnlyVariableAssigns): array
{
return array_filter(
$readOnlyVariableAssigns,
fn (Assign $assign): bool => $this->exprUsedInNextNodeAnalyzer->isUsed($assign->var)
);
}

/**
* @param Assign[] $variableAssigns
* @return Assign[]
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

namespace Rector\Core\Tests\Issues\RemoveReadonlyLocalVariable\Fixture;

final class RemoveVariableAssignOnly
{
public function run()
{
$replacements = [
'PHPUnit\Framework\TestCase\Notice' => 'expectNotice',
'PHPUnit\Framework\TestCase\Deprecated' => 'expectDeprecation',
];
}
}

?>
-----
<?php

namespace Rector\Core\Tests\Issues\RemoveReadonlyLocalVariable\Fixture;

final class RemoveVariableAssignOnly
{
public function run()
{
}
}

?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

declare(strict_types=1);

namespace Rector\Core\Tests\Issues\RemoveReadonlyLocalVariable;

use Iterator;
use Rector\Testing\PHPUnit\AbstractRectorTestCase;
use Symplify\SmartFileSystem\SmartFileInfo;

final class RemoveReadonlyLocalVariableTest extends AbstractRectorTestCase
{
/**
* @dataProvider provideData()
*/
public function test(SmartFileInfo $fileInfo): void
{
$this->doTestFileInfo($fileInfo);
}

/**
* @return Iterator<SmartFileInfo>
*/
public function provideData(): Iterator
{
return $this->yieldFilesFromDirectory(__DIR__ . '/Fixture');
}

public function provideConfigFilePath(): string
{
return __DIR__ . '/config/configured_rule.php';
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

declare(strict_types=1);

use Rector\DeadCode\Rector\Assign\RemoveUnusedVariableAssignRector;
use Rector\Privatization\Rector\Class_\ChangeReadOnlyVariableWithDefaultValueToConstantRector;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;

return static function (ContainerConfigurator $containerConfigurator): void {
$services = $containerConfigurator->services();
$services->set(ChangeReadOnlyVariableWithDefaultValueToConstantRector::class);
$services->set(RemoveUnusedVariableAssignRector::class);
};

0 comments on commit 1c7460b

Please sign in to comment.