mirror of
https://github.com/rectorphp/rector.git
synced 2024-06-08 20:22:24 +00:00
[Visibility] Remove ChangePropertyVisibilityRector, not used and rather refactoring scope (#366)
This commit is contained in:
parent
ba2888f667
commit
e06a3c5213
|
@ -1,33 +0,0 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Rector\Tests\Visibility\Rector\Property\ChangePropertyVisibilityRector;
|
||||
|
||||
use Iterator;
|
||||
use Rector\Testing\PHPUnit\AbstractRectorTestCase;
|
||||
use Symplify\SmartFileSystem\SmartFileInfo;
|
||||
|
||||
final class ChangePropertyVisibilityRectorTest 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,29 +0,0 @@
|
|||
<?php
|
||||
|
||||
namespace Rector\Tests\Visibility\Rector\Property\ChangePropertyVisibilityRector\Fixture;
|
||||
|
||||
use Rector\Tests\Visibility\Rector\Property\ChangePropertyVisibilityRector\Source\ParentObject;
|
||||
|
||||
class ClassThatInherits extends ParentObject
|
||||
{
|
||||
public $toBeProtectedProperty;
|
||||
|
||||
protected $toBePrivateProperty;
|
||||
}
|
||||
|
||||
?>
|
||||
-----
|
||||
<?php
|
||||
|
||||
namespace Rector\Tests\Visibility\Rector\Property\ChangePropertyVisibilityRector\Fixture;
|
||||
|
||||
use Rector\Tests\Visibility\Rector\Property\ChangePropertyVisibilityRector\Source\ParentObject;
|
||||
|
||||
class ClassThatInherits extends ParentObject
|
||||
{
|
||||
protected $toBeProtectedProperty;
|
||||
|
||||
private $toBePrivateProperty;
|
||||
}
|
||||
|
||||
?>
|
|
@ -1,25 +0,0 @@
|
|||
<?php
|
||||
|
||||
namespace Rector\Tests\Visibility\Rector\Property\ChangePropertyVisibilityRector\Fixture;
|
||||
|
||||
use Rector\Tests\Visibility\Rector\Property\ChangePropertyVisibilityRector\Source\ParentObject;
|
||||
|
||||
class ClassThatInheritsAgain extends ParentObject
|
||||
{
|
||||
private static $toBePublicStaticProperty;
|
||||
}
|
||||
|
||||
?>
|
||||
-----
|
||||
<?php
|
||||
|
||||
namespace Rector\Tests\Visibility\Rector\Property\ChangePropertyVisibilityRector\Fixture;
|
||||
|
||||
use Rector\Tests\Visibility\Rector\Property\ChangePropertyVisibilityRector\Source\ParentObject;
|
||||
|
||||
class ClassThatInheritsAgain extends ParentObject
|
||||
{
|
||||
public static $toBePublicStaticProperty;
|
||||
}
|
||||
|
||||
?>
|
|
@ -1,21 +0,0 @@
|
|||
<?php
|
||||
|
||||
namespace Rector\Tests\Visibility\Rector\Property\ChangePropertyVisibilityRector\Fixture;
|
||||
|
||||
class Fixture3
|
||||
{
|
||||
private static $toBePublicStaticProperty;
|
||||
}
|
||||
|
||||
?>
|
||||
-----
|
||||
<?php
|
||||
|
||||
namespace Rector\Tests\Visibility\Rector\Property\ChangePropertyVisibilityRector\Fixture;
|
||||
|
||||
class Fixture3
|
||||
{
|
||||
public static $toBePublicStaticProperty;
|
||||
}
|
||||
|
||||
?>
|
|
@ -1,12 +0,0 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Rector\Tests\Visibility\Rector\Property\ChangePropertyVisibilityRector\Source;
|
||||
|
||||
class ParentObject
|
||||
{
|
||||
public $toBePublicProperty;
|
||||
protected $toBeProtectedProperty;
|
||||
private $toBePrivateProperty;
|
||||
}
|
|
@ -1,26 +0,0 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
use Rector\Core\ValueObject\Visibility;
|
||||
use Rector\Tests\Visibility\Rector\Property\ChangePropertyVisibilityRector\Source\ParentObject;
|
||||
use Rector\Visibility\Rector\Property\ChangePropertyVisibilityRector;
|
||||
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
|
||||
|
||||
return static function (ContainerConfigurator $containerConfigurator): void {
|
||||
$services = $containerConfigurator->services();
|
||||
$services->set(ChangePropertyVisibilityRector::class)
|
||||
->call('configure', [[
|
||||
ChangePropertyVisibilityRector::PROPERTY_TO_VISIBILITY_BY_CLASS => [
|
||||
ParentObject::class => [
|
||||
'toBePublicProperty' => Visibility::PUBLIC,
|
||||
'toBeProtectedProperty' => Visibility::PROTECTED,
|
||||
'toBePrivateProperty' => Visibility::PRIVATE,
|
||||
'toBePublicStaticProperty' => Visibility::PUBLIC,
|
||||
],
|
||||
'Rector\Tests\Visibility\Rector\Property\ChangePropertyVisibilityRector\Fixture\Fixture3' => [
|
||||
'toBePublicStaticProperty' => Visibility::PUBLIC,
|
||||
],
|
||||
],
|
||||
]]);
|
||||
};
|
|
@ -1,116 +0,0 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Rector\Visibility\Rector\Property;
|
||||
|
||||
use PhpParser\Node;
|
||||
use PhpParser\Node\Stmt\ClassLike;
|
||||
use PhpParser\Node\Stmt\Property;
|
||||
use PHPStan\Type\ObjectType;
|
||||
use Rector\Core\Contract\Rector\ConfigurableRectorInterface;
|
||||
use Rector\Core\Rector\AbstractRector;
|
||||
use Rector\Core\ValueObject\Visibility;
|
||||
use Rector\NodeTypeResolver\Node\AttributeKey;
|
||||
use Symplify\RuleDocGenerator\ValueObject\CodeSample\ConfiguredCodeSample;
|
||||
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
|
||||
|
||||
/**
|
||||
* @see \Rector\Tests\Visibility\Rector\Property\ChangePropertyVisibilityRector\ChangePropertyVisibilityRectorTest
|
||||
*/
|
||||
final class ChangePropertyVisibilityRector extends AbstractRector implements ConfigurableRectorInterface
|
||||
{
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
public const PROPERTY_TO_VISIBILITY_BY_CLASS = 'property_to_visibility_by_class';
|
||||
|
||||
/**
|
||||
* @var array<string, array<string, int>>
|
||||
*/
|
||||
private array $propertyToVisibilityByClass = [];
|
||||
|
||||
public function getRuleDefinition(): RuleDefinition
|
||||
{
|
||||
return new RuleDefinition(
|
||||
'Change visibility of property from parent class.',
|
||||
[
|
||||
new ConfiguredCodeSample(
|
||||
<<<'CODE_SAMPLE'
|
||||
class FrameworkClass
|
||||
{
|
||||
protected $someProperty;
|
||||
}
|
||||
|
||||
class MyClass extends FrameworkClass
|
||||
{
|
||||
public $someProperty;
|
||||
}
|
||||
CODE_SAMPLE
|
||||
,
|
||||
<<<'CODE_SAMPLE'
|
||||
class FrameworkClass
|
||||
{
|
||||
protected $someProperty;
|
||||
}
|
||||
|
||||
class MyClass extends FrameworkClass
|
||||
{
|
||||
protected $someProperty;
|
||||
}
|
||||
CODE_SAMPLE
|
||||
,
|
||||
[
|
||||
self::PROPERTY_TO_VISIBILITY_BY_CLASS => [
|
||||
'FrameworkClass' => [
|
||||
'someProperty' => Visibility::PROTECTED,
|
||||
],
|
||||
],
|
||||
]
|
||||
),
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<class-string<Node>>
|
||||
*/
|
||||
public function getNodeTypes(): array
|
||||
{
|
||||
return [Property::class];
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Property $node
|
||||
*/
|
||||
public function refactor(Node $node): ?Node
|
||||
{
|
||||
$classLike = $node->getAttribute(AttributeKey::CLASS_NODE);
|
||||
if (! $classLike instanceof ClassLike) {
|
||||
return null;
|
||||
}
|
||||
|
||||
foreach ($this->propertyToVisibilityByClass as $type => $propertyToVisibility) {
|
||||
if (! $this->isObjectType($classLike, new ObjectType($type))) {
|
||||
continue;
|
||||
}
|
||||
|
||||
foreach ($propertyToVisibility as $property => $visibility) {
|
||||
if (! $this->isName($node, $property)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$this->visibilityManipulator->changeNodeVisibility($node, $visibility);
|
||||
|
||||
return $node;
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public function configure(array $configuration): void
|
||||
{
|
||||
$this->propertyToVisibilityByClass = $configuration[self::PROPERTY_TO_VISIBILITY_BY_CLASS] ?? [];
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user