[Visibility] Remove ChangePropertyVisibilityRector, not used and rather refactoring scope (#366)

This commit is contained in:
Tomas Votruba 2021-07-02 23:31:50 +02:00 committed by GitHub
parent ba2888f667
commit e06a3c5213
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 0 additions and 262 deletions

View File

@ -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';
}
}

View File

@ -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;
}
?>

View File

@ -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;
}
?>

View File

@ -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;
}
?>

View File

@ -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;
}

View File

@ -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,
],
],
]]);
};

View File

@ -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] ?? [];
}
}