[DeadCode] Remove duplicated RemoveUnusedPrivateConstantRector (#6151)

* [DeadCode] Remove duplicated RemoveUnusedPrivateConstantRector

* [ci-review] Rector Rectify

* [ci-review] Rector Rectify

* [ci-review] Rector Rectify

* [ci-review] Rector Rectify

* [ci-review] Rector Rectify

Co-authored-by: kaizen-ci <info@kaizen-ci.org>
This commit is contained in:
Tomas Votruba 2021-04-15 23:38:44 +02:00 committed by GitHub
parent d3295a7d1e
commit 268b32ff7e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
22 changed files with 39 additions and 177 deletions

View File

@ -960,8 +960,8 @@ PRs and issues are linked, so you can find more about it. Thanks to [ChangelogLi
### Changed
- [#4420] [DeadCode] Make RemoveUnusedPrivateConstantRector skip enum
- [#4419] [DeadCode] Make RemoveUnusedPrivateConstantRector skip enum
- [#4420] [DeadCode] Make RemoveUnusedPrivateClassConstantRector skip enum
- [#4419] [DeadCode] Make RemoveUnusedPrivateClassConstantRector skip enum
## [0.8.26] - 2020-10-15
@ -3698,7 +3698,7 @@ PRs and issues are linked, so you can find more about it. Thanks to [ChangelogLi
### Changed
- [#1748] [CodingStyle] Improve `NewlineBeforeNewAssignSetRector`
- [#1697] [DeadCode] Allow static constant call on `RemoveUnusedPrivateConstantRector`, Thanks to [@ravanscafi]
- [#1697] [DeadCode] Allow static constant call on `RemoveUnusedPrivateClassConstantRector`, Thanks to [@ravanscafi]
- [#1719] Resolve anonymous class return type to object
### Fixed

View File

@ -10,7 +10,7 @@ use Rector\DeadCode\Rector\Assign\RemoveUnusedVariableAssignRector;
use Rector\DeadCode\Rector\BinaryOp\RemoveDuplicatedInstanceOfRector;
use Rector\DeadCode\Rector\BooleanAnd\RemoveAndTrueRector;
use Rector\DeadCode\Rector\Cast\RecastingRemovalRector;
use Rector\DeadCode\Rector\ClassConst\RemoveUnusedPrivateConstantRector;
use Rector\DeadCode\Rector\ClassConst\RemoveUnusedPrivateClassConstantRector;
use Rector\DeadCode\Rector\ClassMethod\RemoveDeadConstructorRector;
use Rector\DeadCode\Rector\ClassMethod\RemoveDelegatingParentCallRector;
use Rector\DeadCode\Rector\ClassMethod\RemoveEmptyClassMethodRector;
@ -62,7 +62,7 @@ return static function (ContainerConfigurator $containerConfigurator): void {
$services->set(SimplifyMirrorAssignRector::class);
$services->set(RemoveOverriddenValuesRector::class);
$services->set(RemoveUnusedPrivatePropertyRector::class);
$services->set(RemoveUnusedPrivateConstantRector::class);
$services->set(RemoveUnusedPrivateClassConstantRector::class);
$services->set(RemoveUnusedPrivateMethodRector::class);
$services->set(RemoveCodeAfterReturnRector::class);
$services->set(RemoveDeadConstructorRector::class);

View File

@ -3634,11 +3634,11 @@ Remove unused class constants
<br>
### RemoveUnusedPrivateConstantRector
### RemoveUnusedPrivateClassConstantRector
Remove unused private constant
- class: [`Rector\DeadCode\Rector\ClassConst\RemoveUnusedPrivateConstantRector`](../rules/DeadCode/Rector/ClassConst/RemoveUnusedPrivateConstantRector.php)
- class: [`Rector\DeadCode\Rector\ClassConst\RemoveUnusedPrivateClassConstantRector`](../rules/DeadCode/Rector/ClassConst/RemoveUnusedPrivateClassConstantRector.php)
```diff
final class SomeController

View File

@ -42,7 +42,7 @@ final class ChangedFilesDetectorTest extends AbstractRectorTestCase
}
/**
* @param string[] $dependantFiles
* @param mixed[]|string[] $dependantFiles
* @dataProvider provideData()
*/
public function testGetDependentFileInfos(string $filePathName, array $dependantFiles): void

View File

@ -526,3 +526,7 @@ parameters:
-
message: '#\$this as argument is not allowed\. Refactor method to service composition#'
path: rules/TypeDeclaration/Rector/ClassMethod/ParamTypeFromStrictTypedPropertyRector.php
-
message: '#Class cognitive complexity is 32, keep it under 30#'
path: rules/Privatization/Rector/Class_/ChangeLocalPropertyToVariableRector.php

View File

@ -1,6 +1,6 @@
<?php
namespace Rector\Tests\DeadCode\Rector\ClassConst\RemoveUnusedPrivateConstantRector\Fixture;
namespace Rector\Tests\DeadCode\Rector\ClassConst\RemoveUnusedPrivateClassConstantRector\Fixture;
final class KeepConstant
{

View File

@ -1,6 +1,6 @@
<?php
namespace Rector\Tests\DeadCode\Rector\ClassConst\RemoveUnusedPrivateConstantRector\Fixture;
namespace Rector\Tests\DeadCode\Rector\ClassConst\RemoveUnusedPrivateClassConstantRector\Fixture;
final class KeepStaticConstant
{

View File

@ -1,6 +1,6 @@
<?php
namespace Rector\Tests\DeadCode\Rector\ClassConst\RemoveUnusedPrivateConstantRector\Fixture;
namespace Rector\Tests\DeadCode\Rector\ClassConst\RemoveUnusedPrivateClassConstantRector\Fixture;
final class Fixture
{
@ -15,7 +15,7 @@ final class Fixture
-----
<?php
namespace Rector\Tests\DeadCode\Rector\ClassConst\RemoveUnusedPrivateConstantRector\Fixture;
namespace Rector\Tests\DeadCode\Rector\ClassConst\RemoveUnusedPrivateClassConstantRector\Fixture;
final class Fixture
{

View File

@ -1,8 +1,8 @@
<?php
namespace Rector\Tests\DeadCode\Rector\ClassConst\RemoveUnusedPrivateConstantRector\Fixture;
namespace Rector\Tests\DeadCode\Rector\ClassConst\RemoveUnusedPrivateClassConstantRector\Fixture;
use Rector\Tests\DeadCode\Rector\ClassConst\RemoveUnusedPrivateConstantRector\Source\AbstractEnum;
use Rector\Tests\DeadCode\Rector\ClassConst\RemoveUnusedPrivateClassConstantRector\Source\AbstractEnum;
class SkipEnum extends AbstractEnum
{

View File

@ -1,6 +1,6 @@
<?php
namespace Rector\Tests\DeadCode\Rector\ClassConst\RemoveUnusedPrivateConstantRector\Fixture;
namespace Rector\Tests\DeadCode\Rector\ClassConst\RemoveUnusedPrivateClassConstantRector\Fixture;
final class SkipUsedInTrait
{

View File

@ -1,33 +0,0 @@
<?php
declare(strict_types=1);
namespace Rector\Tests\DeadCode\Rector\ClassConst\RemoveUnusedPrivateConstantRector;
use Iterator;
use Rector\Testing\PHPUnit\AbstractRectorTestCase;
use Symplify\SmartFileSystem\SmartFileInfo;
final class RemoveUnusedPrivateConstantRectorTest 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';
}
}

View File

@ -1,10 +0,0 @@
<?php
declare(strict_types=1);
namespace Rector\Tests\DeadCode\Rector\ClassConst\RemoveUnusedPrivateConstantRector\Source;
abstract class AbstractEnum
{
}

View File

@ -1,12 +0,0 @@
<?php
declare(strict_types=1);
use Rector\DeadCode\Rector\ClassConst\RemoveUnusedPrivateConstantRector;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
return static function (ContainerConfigurator $containerConfigurator): void {
$services = $containerConfigurator->services();
$services->set(RemoveUnusedPrivateConstantRector::class);
};

View File

@ -78,9 +78,9 @@ final class StmtOrderTest extends AbstractKernelTestCase
/**
* @dataProvider dataProvider
* @param array<int, string> $desiredStmtOrder
* @param array<int, string> $currentStmtOrder
* @param array<int, int> $expected
* @param string[] $desiredStmtOrder
* @param string[] $currentStmtOrder
* @param int[] $expected
*/
public function testCreateOldToNewKeys(array $desiredStmtOrder, array $currentStmtOrder, array $expected): void
{

View File

@ -249,10 +249,10 @@ CODE_SAMPLE
foreach ($exprValues as $exprValue) {
$isReAssign = (bool) $this->betterNodeFinder->findFirst($next, function (Node $node): bool {
$parent = $node->getAttribute(AttributeKey::PARENT_NODE);
$node = $this->mayBeArrayDimFetch($node);
if (! $parent instanceof Assign) {
return false;
}
$node = $this->mayBeArrayDimFetch($node);
return (string) $this->getName($node) === (string) $this->getName($parent->var);
});

View File

@ -121,9 +121,10 @@ final class VarTagRemover
{
if ($varTagValueNode->type instanceof BracketsAwareUnionTypeNode) {
foreach ($varTagValueNode->type->types as $type) {
if ($type instanceof SpacingAwareArrayTypeNode && $this->isArrayOfExistingClassNode($node, $type)) {
return true;
if (! $type instanceof SpacingAwareArrayTypeNode) {
return false;
}
return $this->isArrayOfExistingClassNode($node, $type);
}
}

View File

@ -127,6 +127,10 @@ CODE_SAMPLE
return true;
}
if ($this->classConstManipulator->hasClassConstFetch($classConst)) {
return true;
}
$phpDocInfo = $this->phpDocInfoFactory->createFromNodeOrEmpty($classConst);
if ($phpDocInfo->hasByName('api')) {
return true;

View File

@ -1,91 +0,0 @@
<?php
declare(strict_types=1);
namespace Rector\DeadCode\Rector\ClassConst;
use PhpParser\Node;
use PhpParser\Node\Stmt\ClassConst;
use Rector\Core\NodeManipulator\ClassConstManipulator;
use Rector\Core\Rector\AbstractRector;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
/**
* @see \Rector\Tests\DeadCode\Rector\ClassConst\RemoveUnusedPrivateConstantRector\RemoveUnusedPrivateConstantRectorTest
*/
final class RemoveUnusedPrivateConstantRector extends AbstractRector
{
/**
* @var ClassConstManipulator
*/
private $classConstManipulator;
public function __construct(ClassConstManipulator $classConstManipulator)
{
$this->classConstManipulator = $classConstManipulator;
}
public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition('Remove unused private constant', [
new CodeSample(
<<<'CODE_SAMPLE'
final class SomeController
{
private const SOME_CONSTANT = 5;
public function run()
{
return 5;
}
}
CODE_SAMPLE
,
<<<'CODE_SAMPLE'
final class SomeController
{
public function run()
{
return 5;
}
}
CODE_SAMPLE
),
]);
}
/**
* @return array<class-string<Node>>
*/
public function getNodeTypes(): array
{
return [ClassConst::class];
}
/**
* @param ClassConst $node
*/
public function refactor(Node $node): ?Node
{
if (! $node->isPrivate()) {
return null;
}
if (count($node->consts) !== 1) {
return null;
}
if ($this->classConstManipulator->hasClassConstFetch($node)) {
return null;
}
// skip enum
if ($this->classConstManipulator->isEnum($node)) {
return null;
}
$this->removeNode($node);
return $node;
}
}

View File

@ -11,6 +11,7 @@ use PhpParser\Node\Expr\NullsafeMethodCall;
use PhpParser\Node\Expr\NullsafePropertyFetch;
use PhpParser\Node\Expr\PropertyFetch;
use PhpParser\Node\Expr\Ternary;
use PhpParser\Node\Expr\Variable;
use Rector\Core\Rector\AbstractRector;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Rector\Php70\NodeAnalyzer\VariableNaming;
@ -25,7 +26,7 @@ final class DowngradeNullsafeToTernaryOperatorRector extends AbstractRector
/**
* @var VariableNaming
*/
public $variableNaming;
private $variableNaming;
public function __construct(VariableNaming $variableNaming)
{
@ -67,11 +68,11 @@ CODE_SAMPLE
$node->getAttribute(AttributeKey::SCOPE),
'_'
);
$tempVar = new Node\Expr\Variable($tempVarName);
$variable = new Variable($tempVarName);
$called = $node instanceof NullsafeMethodCall
? new MethodCall($tempVar, $node->name, $node->args)
: new PropertyFetch($tempVar, $node->name);
? new MethodCall($variable, $node->name, $node->args)
: new PropertyFetch($variable, $node->name);
return new Ternary(new Assign($tempVar, $node->var), $called, $this->nodeFactory->createNull());
return new Ternary(new Assign($variable, $node->var), $called, $this->nodeFactory->createNull());
}
}

View File

@ -137,10 +137,9 @@ CODE_SAMPLE
$classConstConstsByValue[$position] = $constantValue;
}
$arrayCountValue = array_count_values($classConstConstsByValue);
// work only with unique constants
foreach ($classConstConstsByValue as $position => $constantValue) {
$arrayCountValue = array_count_values($classConstConstsByValue);
if ($arrayCountValue[$constantValue] > 1) {
unset($classConstConstsByValue[$position]);
}

View File

@ -53,8 +53,7 @@ final class RenameMethodRector extends AbstractRector implements ConfigurableRec
public function __construct(
ClassManipulator $classManipulator,
MethodCallRenameCollector $methodCallRenameCollector
)
{
) {
$this->classManipulator = $classManipulator;
$this->methodCallRenameCollector = $methodCallRenameCollector;
}

View File

@ -28,7 +28,7 @@ final class NodeFactoryTest extends AbstractKernelTestCase
}
/**
* @param mixed[] $inputArray
* @param int[]<string, int> $inputArray
* @dataProvider provideDataForArray()
*/
public function testCreateArray(array $inputArray, Array_ $expectedArrayNode): void