mirror of
https://github.com/rectorphp/rector.git
synced 2024-05-28 23:10:51 +00:00
[Privatization] Remove MakeUnusedClassesWithChildrenAbstractRector as risky and full context is needed (#6091)
This commit is contained in:
parent
7aecf8138d
commit
26a5b86892
|
@ -5,7 +5,6 @@ declare(strict_types=1);
|
|||
use Rector\Privatization\Rector\Class_\ChangeLocalPropertyToVariableRector;
|
||||
use Rector\Privatization\Rector\Class_\ChangeReadOnlyVariableWithDefaultValueToConstantRector;
|
||||
use Rector\Privatization\Rector\Class_\FinalizeClassesWithoutChildrenRector;
|
||||
use Rector\Privatization\Rector\Class_\MakeUnusedClassesWithChildrenAbstractRector;
|
||||
use Rector\Privatization\Rector\Class_\RepeatedLiteralToClassConstantRector;
|
||||
use Rector\Privatization\Rector\ClassMethod\ChangeGlobalVariablesToPropertiesRector;
|
||||
use Rector\Privatization\Rector\ClassMethod\PrivatizeFinalClassMethodRector;
|
||||
|
@ -18,7 +17,6 @@ use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigura
|
|||
return static function (ContainerConfigurator $containerConfigurator): void {
|
||||
$services = $containerConfigurator->services();
|
||||
|
||||
$services->set(MakeUnusedClassesWithChildrenAbstractRector::class);
|
||||
$services->set(FinalizeClassesWithoutChildrenRector::class);
|
||||
$services->set(ChangeGlobalVariablesToPropertiesRector::class);
|
||||
$services->set(ChangeReadOnlyPropertyWithDefaultValueToConstantRector::class);
|
||||
|
|
File diff suppressed because it is too large
Load Diff
|
@ -511,14 +511,6 @@ final class NodeRepository
|
|||
return $this->parsedNodeCollector->getClasses();
|
||||
}
|
||||
|
||||
/**
|
||||
* @return New_[]
|
||||
*/
|
||||
public function findNewsByClass(string $className): array
|
||||
{
|
||||
return $this->parsedNodeCollector->findNewsByClass($className);
|
||||
}
|
||||
|
||||
public function findClassConstant(string $className, string $constantName): ?ClassConst
|
||||
{
|
||||
return $this->parsedNodeCollector->findClassConstant($className, $constantName);
|
||||
|
|
|
@ -213,24 +213,6 @@ final class ParsedNodeCollector
|
|||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @return New_[]
|
||||
*/
|
||||
public function findNewsByClass(string $className): array
|
||||
{
|
||||
$newsByClass = [];
|
||||
|
||||
foreach ($this->news as $new) {
|
||||
if (! $this->nodeNameResolver->isName($new->class, $className)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$newsByClass[] = $new;
|
||||
}
|
||||
|
||||
return $newsByClass;
|
||||
}
|
||||
|
||||
public function findClassConstByClassConstFetch(ClassConstFetch $classConstFetch): ?ClassConst
|
||||
{
|
||||
$className = $this->nodeNameResolver->getName($classConstFetch->class);
|
||||
|
|
|
@ -1,27 +0,0 @@
|
|||
<?php
|
||||
|
||||
namespace Rector\Tests\Privatization\Rector\Class_\MakeUnusedClassesWithChildrenAbstractRector\Fixture;
|
||||
|
||||
class SomeClass extends PossibleAbstractClass
|
||||
{
|
||||
}
|
||||
|
||||
class PossibleAbstractClass
|
||||
{
|
||||
}
|
||||
|
||||
?>
|
||||
-----
|
||||
<?php
|
||||
|
||||
namespace Rector\Tests\Privatization\Rector\Class_\MakeUnusedClassesWithChildrenAbstractRector\Fixture;
|
||||
|
||||
class SomeClass extends PossibleAbstractClass
|
||||
{
|
||||
}
|
||||
|
||||
abstract class PossibleAbstractClass
|
||||
{
|
||||
}
|
||||
|
||||
?>
|
|
@ -1,26 +0,0 @@
|
|||
<?php
|
||||
|
||||
namespace Rector\Tests\Privatization\Rector\Class_\MakeUnusedClassesWithChildrenAbstractRector\Fixture;
|
||||
|
||||
class SkipMethodCall
|
||||
{
|
||||
public static function run()
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
class ClassCalling
|
||||
{
|
||||
public function go($skipMethodCall)
|
||||
{
|
||||
/** @var SkipMethodCall $skipMethodCall */
|
||||
$skipMethodCall->run();
|
||||
}
|
||||
}
|
||||
|
||||
function localFunction()
|
||||
{
|
||||
// just so the ClassCalling is not made abstract
|
||||
$classCalling = new ClassCalling();
|
||||
}
|
|
@ -1,12 +0,0 @@
|
|||
<?php
|
||||
|
||||
namespace Rector\Tests\Privatization\Rector\Class_\MakeUnusedClassesWithChildrenAbstractRector\Fixture;
|
||||
|
||||
class SkipNew
|
||||
{
|
||||
}
|
||||
|
||||
function runMePlease()
|
||||
{
|
||||
return new SkipNew();
|
||||
}
|
|
@ -1,16 +0,0 @@
|
|||
<?php
|
||||
|
||||
namespace Rector\Tests\Privatization\Rector\Class_\MakeUnusedClassesWithChildrenAbstractRector\Fixture;
|
||||
|
||||
class SkipStaticCall
|
||||
{
|
||||
public static function run()
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
function run()
|
||||
{
|
||||
SkipStaticCall::run();
|
||||
}
|
|
@ -1,33 +0,0 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Rector\Tests\Privatization\Rector\Class_\MakeUnusedClassesWithChildrenAbstractRector;
|
||||
|
||||
use Iterator;
|
||||
use Rector\Testing\PHPUnit\AbstractRectorTestCase;
|
||||
use Symplify\SmartFileSystem\SmartFileInfo;
|
||||
|
||||
final class MakeUnusedClassesWithChildrenAbstractRectorTest 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';
|
||||
}
|
||||
}
|
|
@ -1,12 +0,0 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
use Rector\Privatization\Rector\Class_\MakeUnusedClassesWithChildrenAbstractRector;
|
||||
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
|
||||
|
||||
return static function (ContainerConfigurator $containerConfigurator): void {
|
||||
$services = $containerConfigurator->services();
|
||||
|
||||
$services->set(MakeUnusedClassesWithChildrenAbstractRector::class);
|
||||
};
|
|
@ -1,92 +0,0 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Rector\Privatization\Rector\Class_;
|
||||
|
||||
use PhpParser\Node;
|
||||
use PhpParser\Node\Stmt\Class_;
|
||||
use Rector\Core\Rector\AbstractRector;
|
||||
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
|
||||
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
|
||||
|
||||
/**
|
||||
* @see \Rector\Tests\Privatization\Rector\Class_\MakeUnusedClassesWithChildrenAbstractRector\MakeUnusedClassesWithChildrenAbstractRectorTest
|
||||
*/
|
||||
final class MakeUnusedClassesWithChildrenAbstractRector extends AbstractRector
|
||||
{
|
||||
public function getRuleDefinition(): RuleDefinition
|
||||
{
|
||||
return new RuleDefinition(
|
||||
'Classes that have no children nor are used, should have abstract',
|
||||
[
|
||||
new CodeSample(
|
||||
<<<'CODE_SAMPLE'
|
||||
class SomeClass extends PossibleAbstractClass
|
||||
{
|
||||
}
|
||||
|
||||
class PossibleAbstractClass
|
||||
{
|
||||
}
|
||||
CODE_SAMPLE
|
||||
,
|
||||
<<<'CODE_SAMPLE'
|
||||
class SomeClass extends PossibleAbstractClass
|
||||
{
|
||||
}
|
||||
|
||||
abstract class PossibleAbstractClass
|
||||
{
|
||||
}
|
||||
CODE_SAMPLE
|
||||
),
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<class-string<Node>>
|
||||
*/
|
||||
public function getNodeTypes(): array
|
||||
{
|
||||
return [Class_::class];
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Class_ $node
|
||||
*/
|
||||
public function refactor(Node $node): ?Node
|
||||
{
|
||||
$className = $this->getName($node);
|
||||
if ($className === null) {
|
||||
return null;
|
||||
}
|
||||
$nodeRepositoryFindMethodCallsOnClass = $this->nodeRepository->findMethodCallsOnClass($className);
|
||||
|
||||
// 1. is in static call?
|
||||
if ($nodeRepositoryFindMethodCallsOnClass !== []) {
|
||||
return null;
|
||||
}
|
||||
$parsedNodeCollectorFindNewsByClass = $this->nodeRepository->findNewsByClass($className);
|
||||
|
||||
// 2. is in new?
|
||||
if ($parsedNodeCollectorFindNewsByClass !== []) {
|
||||
return null;
|
||||
}
|
||||
$nodeRepositoryFindChildrenOfClass = $this->nodeRepository->findChildrenOfClass($className);
|
||||
|
||||
// 3. does it have any children
|
||||
if ($nodeRepositoryFindChildrenOfClass === []) {
|
||||
return null;
|
||||
}
|
||||
|
||||
// is abstract!
|
||||
if ($node->isAbstract()) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$this->visibilityManipulator->makeAbstract($node);
|
||||
|
||||
return $node;
|
||||
}
|
||||
}
|
|
@ -101,6 +101,7 @@ CODE_SAMPLE
|
|||
}
|
||||
|
||||
$scope = $classMethod->getAttribute(AttributeKey::SCOPE);
|
||||
|
||||
$classReflection = $scope->getClassReflection();
|
||||
if (! $classReflection instanceof ClassReflection) {
|
||||
return null;
|
||||
|
|
Loading…
Reference in New Issue
Block a user