Add Visibility consts (#4981)

This commit is contained in:
Tomas Votruba 2020-12-24 23:01:30 +01:00 committed by GitHub
parent d03fb14c3f
commit 5a47e36b72
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
24 changed files with 160 additions and 128 deletions

View File

@ -124,18 +124,18 @@ final class AutoloadIncluder
$this->loadIfExistsAndNotLoadedYet($fileToAutoload); $this->loadIfExistsAndNotLoadedYet($fileToAutoload);
} }
private function loadIfExistsAndNotLoadedYet(string $file): void private function loadIfExistsAndNotLoadedYet(string $filePath): void
{ {
if (! file_exists($file)) { if (! file_exists($filePath)) {
return; return;
} }
if (in_array($file, $this->alreadyLoadedAutoloadFiles, true)) { if (in_array($filePath, $this->alreadyLoadedAutoloadFiles, true)) {
return; return;
} }
$this->alreadyLoadedAutoloadFiles[] = realpath($file); $this->alreadyLoadedAutoloadFiles[] = realpath($filePath);
require_once $file; require_once $filePath;
} }
} }

View File

@ -11,8 +11,8 @@
"bin": ["bin/rector"], "bin": ["bin/rector"],
"require": { "require": {
"php": "^7.3|^8.0", "php": "^7.3|^8.0",
"ext-json": "*",
"ext-dom": "*", "ext-dom": "*",
"ext-json": "*",
"composer/xdebug-handler": "^1.4", "composer/xdebug-handler": "^1.4",
"doctrine/annotations": "^1.11", "doctrine/annotations": "^1.11",
"doctrine/inflector": "^2.0", "doctrine/inflector": "^2.0",
@ -31,13 +31,13 @@
"symfony/finder": "^4.4.8|^5.1", "symfony/finder": "^4.4.8|^5.1",
"symfony/http-kernel": "^4.4.8|^5.1", "symfony/http-kernel": "^4.4.8|^5.1",
"symplify/autowire-array-parameter": "^9.0.14", "symplify/autowire-array-parameter": "^9.0.14",
"symplify/skipper": "^9.0.14", "symplify/console-color-diff": "^9.0.14",
"symplify/package-builder": "^9.0.14", "symplify/package-builder": "^9.0.14",
"symplify/simple-php-doc-parser": "^9.0.14",
"symplify/smart-file-system": "^9.0.14",
"symplify/rule-doc-generator": "^9.0.14", "symplify/rule-doc-generator": "^9.0.14",
"symplify/set-config-resolver": "^9.0.14", "symplify/set-config-resolver": "^9.0.14",
"symplify/console-color-diff": "^9.0.14", "symplify/simple-php-doc-parser": "^9.0.14",
"symplify/skipper": "^9.0.14",
"symplify/smart-file-system": "^9.0.14",
"symplify/symfony-php-config": "^9.0.14", "symplify/symfony-php-config": "^9.0.14",
"webmozart/assert": "^1.9" "webmozart/assert": "^1.9"
}, },
@ -174,8 +174,7 @@
"rules/cakephp/tests/Rector/Namespace_/AppUsesStaticCallToUseStatementRector/Source", "rules/cakephp/tests/Rector/Namespace_/AppUsesStaticCallToUseStatementRector/Source",
"rules/psr4/tests/Rector/Namespace_/MultipleClassFileToPsr4ClassesRector/Source", "rules/psr4/tests/Rector/Namespace_/MultipleClassFileToPsr4ClassesRector/Source",
"rules/renaming/tests/Rector/FileWithoutNamespace/PseudoNamespaceToNamespaceRector/Source", "rules/renaming/tests/Rector/FileWithoutNamespace/PseudoNamespaceToNamespaceRector/Source",
"rules/symfony4/tests/Rector/MethodCall/ContainerGetToConstructorInjectionRector/Source", "rules/symfony4/tests/Rector/MethodCall/ContainerGetToConstructorInjectionRector/Source"
"tests/Source"
], ],
"files": [ "files": [
"rules/coding-style/tests/Rector/Namespace_/ImportFullyQualifiedNamesRector/Source/AnotherClass.php", "rules/coding-style/tests/Rector/Namespace_/ImportFullyQualifiedNamesRector/Source/AnotherClass.php",
@ -324,8 +323,7 @@
"rector": "bin/rector process --config rector-ci.php --ansi" "rector": "bin/rector process --config rector-ci.php --ansi"
}, },
"config": { "config": {
"sort-packages": true, "sort-packages": true
"process-timeout": 0
}, },
"minimum-stability": "dev", "minimum-stability": "dev",
"prefer-stable": true "prefer-stable": true

View File

@ -4,6 +4,7 @@ declare(strict_types=1);
use Rector\CakePHP\Rector\MethodCall\ModalToGetSetRector; use Rector\CakePHP\Rector\MethodCall\ModalToGetSetRector;
use Rector\CakePHP\ValueObject\ModalToGetSet; use Rector\CakePHP\ValueObject\ModalToGetSet;
use Rector\Core\ValueObject\Visibility;
use Rector\Generic\Rector\ClassMethod\ChangeMethodVisibilityRector; use Rector\Generic\Rector\ClassMethod\ChangeMethodVisibilityRector;
use Rector\Generic\Rector\ClassMethod\NormalToFluentRector; use Rector\Generic\Rector\ClassMethod\NormalToFluentRector;
use Rector\Generic\ValueObject\ChangeMethodVisibility; use Rector\Generic\ValueObject\ChangeMethodVisibility;
@ -223,8 +224,8 @@ return static function (ContainerConfigurator $containerConfigurator): void {
$services->set(ChangeMethodVisibilityRector::class) $services->set(ChangeMethodVisibilityRector::class)
->call('configure', [[ ->call('configure', [[
ChangeMethodVisibilityRector::METHOD_VISIBILITIES => ValueObjectInliner::inline([ ChangeMethodVisibilityRector::METHOD_VISIBILITIES => ValueObjectInliner::inline([
new ChangeMethodVisibility('Cake\Mailer\MailerAwareTrait', 'getMailer', 'protected'), new ChangeMethodVisibility('Cake\Mailer\MailerAwareTrait', 'getMailer', Visibility::PROTECTED),
new ChangeMethodVisibility('Cake\View\CellTrait', 'cell', 'protected'), new ChangeMethodVisibility('Cake\View\CellTrait', 'cell', Visibility::PROTECTED),
]), ]),
]]); ]]);

View File

@ -2,6 +2,7 @@
declare(strict_types=1); declare(strict_types=1);
use Rector\Core\ValueObject\Visibility;
use Rector\Generic\Rector\ClassMethod\ChangeMethodVisibilityRector; use Rector\Generic\Rector\ClassMethod\ChangeMethodVisibilityRector;
use Rector\Generic\ValueObject\ChangeMethodVisibility; use Rector\Generic\ValueObject\ChangeMethodVisibility;
use Rector\NetteToSymfony\Rector\MethodCall\WrapTransParameterNameRector; use Rector\NetteToSymfony\Rector\MethodCall\WrapTransParameterNameRector;
@ -17,7 +18,7 @@ return static function (ContainerConfigurator $containerConfigurator): void {
$services->set(ChangeMethodVisibilityRector::class) $services->set(ChangeMethodVisibilityRector::class)
->call('configure', [[ ->call('configure', [[
ChangeMethodVisibilityRector::METHOD_VISIBILITIES => ValueObjectInliner::inline([ ChangeMethodVisibilityRector::METHOD_VISIBILITIES => ValueObjectInliner::inline([
new ChangeMethodVisibility('Kdyby\Events\Subscriber', 'getSubscribedEvents', 'static'), new ChangeMethodVisibility('Kdyby\Events\Subscriber', 'getSubscribedEvents', Visibility::STATIC),
]), ]),
]]); ]]);

View File

@ -2,6 +2,7 @@
declare(strict_types=1); declare(strict_types=1);
use Rector\Core\ValueObject\Visibility;
use Rector\Generic\Rector\ClassMethod\ChangeMethodVisibilityRector; use Rector\Generic\Rector\ClassMethod\ChangeMethodVisibilityRector;
use Rector\Generic\ValueObject\ChangeMethodVisibility; use Rector\Generic\ValueObject\ChangeMethodVisibility;
use Rector\Renaming\Rector\MethodCall\RenameMethodRector; use Rector\Renaming\Rector\MethodCall\RenameMethodRector;
@ -33,9 +34,9 @@ return static function (ContainerConfigurator $containerConfigurator): void {
$services->set(ChangeMethodVisibilityRector::class) $services->set(ChangeMethodVisibilityRector::class)
->call('configure', [[ ->call('configure', [[
ChangeMethodVisibilityRector::METHOD_VISIBILITIES => ValueObjectInliner::inline([ ChangeMethodVisibilityRector::METHOD_VISIBILITIES => ValueObjectInliner::inline([
new ChangeMethodVisibility('Illuminate\Routing\Router', 'addRoute', 'public'), new ChangeMethodVisibility('Illuminate\Routing\Router', 'addRoute', Visibility::PUBLIC),
new ChangeMethodVisibility('Illuminate\Contracts\Auth\Access\Gate', 'raw', 'public'), new ChangeMethodVisibility('Illuminate\Contracts\Auth\Access\Gate', 'raw', Visibility::PUBLIC),
new ChangeMethodVisibility('Illuminate\Database\Grammar', 'getDateFormat', 'public'), new ChangeMethodVisibility('Illuminate\Database\Grammar', 'getDateFormat', Visibility::PUBLIC),
]), ]),
]]); ]]);
}; };

View File

@ -2,6 +2,7 @@
declare(strict_types=1); declare(strict_types=1);
use Rector\Core\ValueObject\Visibility;
use Rector\Generic\Rector\ClassMethod\ArgumentAdderRector; use Rector\Generic\Rector\ClassMethod\ArgumentAdderRector;
use Rector\Generic\Rector\ClassMethod\ArgumentRemoverRector; use Rector\Generic\Rector\ClassMethod\ArgumentRemoverRector;
use Rector\Generic\Rector\ClassMethod\ChangeMethodVisibilityRector; use Rector\Generic\Rector\ClassMethod\ChangeMethodVisibilityRector;
@ -21,8 +22,8 @@ return static function (ContainerConfigurator $containerConfigurator): void {
$services = $containerConfigurator->services(); $services = $containerConfigurator->services();
$services->set(ChangeMethodVisibilityRector::class)->call('configure', [[ $services->set(ChangeMethodVisibilityRector::class)->call('configure', [[
ChangeMethodVisibilityRector::METHOD_VISIBILITIES => ValueObjectInliner::inline([ ChangeMethodVisibilityRector::METHOD_VISIBILITIES => ValueObjectInliner::inline([
new ChangeMethodVisibility('Illuminate\Routing\Router', 'addRoute', 'public'), new ChangeMethodVisibility('Illuminate\Routing\Router', 'addRoute', Visibility::PUBLIC),
new ChangeMethodVisibility('Illuminate\Contracts\Auth\Access\Gate', 'raw', 'public'), new ChangeMethodVisibility('Illuminate\Contracts\Auth\Access\Gate', 'raw', Visibility::PUBLIC),
]), ]),
]]); ]]);
$services->set(ArgumentAdderRector::class)->call('configure', [[ $services->set(ArgumentAdderRector::class)->call('configure', [[

View File

@ -2,6 +2,7 @@
declare(strict_types=1); declare(strict_types=1);
use Rector\Core\ValueObject\Visibility;
use Rector\Generic\Rector\ClassMethod\ArgumentAdderRector; use Rector\Generic\Rector\ClassMethod\ArgumentAdderRector;
use Rector\Generic\Rector\ClassMethod\ChangeMethodVisibilityRector; use Rector\Generic\Rector\ClassMethod\ChangeMethodVisibilityRector;
use Rector\Generic\Rector\Expression\MethodCallToReturnRector; use Rector\Generic\Rector\Expression\MethodCallToReturnRector;
@ -86,7 +87,11 @@ return static function (ContainerConfigurator $containerConfigurator): void {
$services->set(ChangeMethodVisibilityRector::class) $services->set(ChangeMethodVisibilityRector::class)
->call('configure', [[ ->call('configure', [[
ChangeMethodVisibilityRector::METHOD_VISIBILITIES => ValueObjectInliner::inline([ ChangeMethodVisibilityRector::METHOD_VISIBILITIES => ValueObjectInliner::inline([
new ChangeMethodVisibility('Illuminate\Foundation\Http\FormRequest', 'validationData', 'public'), new ChangeMethodVisibility(
'Illuminate\Foundation\Http\FormRequest',
'validationData',
Visibility::PUBLIC
),
]), ]),
]]); ]]);

View File

@ -5,6 +5,7 @@ declare(strict_types=1);
use PHPStan\Type\IterableType; use PHPStan\Type\IterableType;
use PHPStan\Type\MixedType; use PHPStan\Type\MixedType;
use Rector\Core\ValueObject\Visibility;
use Rector\Generic\Rector\ClassMethod\ArgumentAdderRector; use Rector\Generic\Rector\ClassMethod\ArgumentAdderRector;
use Rector\Generic\Rector\ClassMethod\ArgumentDefaultValueReplacerRector; use Rector\Generic\Rector\ClassMethod\ArgumentDefaultValueReplacerRector;
use Rector\Generic\Rector\ClassMethod\ArgumentRemoverRector; use Rector\Generic\Rector\ClassMethod\ArgumentRemoverRector;
@ -182,7 +183,7 @@ return static function (ContainerConfigurator $containerConfigurator): void {
new ChangeMethodVisibility( new ChangeMethodVisibility(
'Symfony\Component\Form\AbstractTypeExtension', 'Symfony\Component\Form\AbstractTypeExtension',
'getExtendedTypes', 'getExtendedTypes',
'static' Visibility::STATIC
), ),
]), ]),
]]); ]]);

View File

@ -23,6 +23,11 @@ final class RectorSetProvider extends AbstractSetProvider
*/ */
private const DASH_NUMBER_REGEX = '#\-(\d+)#'; private const DASH_NUMBER_REGEX = '#\-(\d+)#';
/**
* @var string[]
*/
private const SET_LIST_CLASSES = [SetList::class, DowngradeSetList::class];
/** /**
* @var Set[] * @var Set[]
*/ */
@ -30,8 +35,7 @@ final class RectorSetProvider extends AbstractSetProvider
public function __construct() public function __construct()
{ {
$setListClasses = [SetList::class, DowngradeSetList::class]; foreach (self::SET_LIST_CLASSES as $setListClass) {
foreach ($setListClasses as $setListClass) {
$setListReflectionClass = new ReflectionClass($setListClass); $setListReflectionClass = new ReflectionClass($setListClass);
$this->hydrateSetsFromConstants($setListReflectionClass); $this->hydrateSetsFromConstants($setListReflectionClass);
} }

View File

@ -707,4 +707,6 @@ parameters:
message: '#Use "\$this\-\><method\>\(\)" instead of "self\:\:<method\>\(\)" to call local method#' message: '#Use "\$this\-\><method\>\(\)" instead of "self\:\:<method\>\(\)" to call local method#'
paths: paths:
- src/PhpParser/Printer/BetterStandardPrinter.php #237 - src/PhpParser/Printer/BetterStandardPrinter.php #237
- src/PhpParser/Printer/BetterStandardPrinter.php #244 - src/PhpParser/Printer/BetterStandardPrinter.php #244
- '#Content of method "mapToPhpParserNode\(\)" is duplicated with method in "Rector\\PHPStanStaticTypeMapper\\TypeMapper\\ClosureTypeMapper" class\. Use unique content or abstract service instead#'

View File

@ -8,6 +8,7 @@ use PhpParser\Node;
use PhpParser\Node\Stmt\ClassConst; use PhpParser\Node\Stmt\ClassConst;
use Rector\Core\Contract\Rector\ConfigurableRectorInterface; use Rector\Core\Contract\Rector\ConfigurableRectorInterface;
use Rector\Core\Rector\AbstractRector; use Rector\Core\Rector\AbstractRector;
use Rector\Core\ValueObject\Visibility;
use Rector\Generic\ValueObject\ClassConstantVisibilityChange; use Rector\Generic\ValueObject\ClassConstantVisibilityChange;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\ConfiguredCodeSample; use Symplify\RuleDocGenerator\ValueObject\CodeSample\ConfiguredCodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition; use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
@ -60,7 +61,11 @@ CODE_SAMPLE
, ,
[ [
self::CLASS_CONSTANT_VISIBILITY_CHANGES => [ self::CLASS_CONSTANT_VISIBILITY_CHANGES => [
new ClassConstantVisibilityChange('ParentObject', 'SOME_CONSTANT', 'protected'), new ClassConstantVisibilityChange(
'ParentObject',
'SOME_CONSTANT',
Visibility::PROTECTED
),
], ],
] ]
), ),

View File

@ -8,6 +8,7 @@ use PhpParser\Node;
use PhpParser\Node\Stmt\ClassMethod; use PhpParser\Node\Stmt\ClassMethod;
use Rector\Core\Contract\Rector\ConfigurableRectorInterface; use Rector\Core\Contract\Rector\ConfigurableRectorInterface;
use Rector\Core\Rector\AbstractRector; use Rector\Core\Rector\AbstractRector;
use Rector\Core\ValueObject\Visibility;
use Rector\Generic\ValueObject\ChangeMethodVisibility; use Rector\Generic\ValueObject\ChangeMethodVisibility;
use Rector\NodeTypeResolver\Node\AttributeKey; use Rector\NodeTypeResolver\Node\AttributeKey;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\ConfiguredCodeSample; use Symplify\RuleDocGenerator\ValueObject\CodeSample\ConfiguredCodeSample;
@ -69,7 +70,11 @@ CODE_SAMPLE
, ,
[ [
self::METHOD_VISIBILITIES => [ self::METHOD_VISIBILITIES => [
new ChangeMethodVisibility('FrameworkClass', 'someMethod', 'protected'), new ChangeMethodVisibility(
'FrameworkClass',
'someMethod',
Visibility::PROTECTED
),
], ],
] ]
), ),

View File

@ -8,6 +8,7 @@ use PhpParser\Node;
use PhpParser\Node\Stmt\Property; use PhpParser\Node\Stmt\Property;
use Rector\Core\Contract\Rector\ConfigurableRectorInterface; use Rector\Core\Contract\Rector\ConfigurableRectorInterface;
use Rector\Core\Rector\AbstractRector; use Rector\Core\Rector\AbstractRector;
use Rector\Core\ValueObject\Visibility;
use Rector\NodeTypeResolver\Node\AttributeKey; use Rector\NodeTypeResolver\Node\AttributeKey;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\ConfiguredCodeSample; use Symplify\RuleDocGenerator\ValueObject\CodeSample\ConfiguredCodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition; use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
@ -20,10 +21,10 @@ final class ChangePropertyVisibilityRector extends AbstractRector implements Con
/** /**
* @var string * @var string
*/ */
public const PROPERTY_TO_VISIBILITY_BY_CLASS = 'propertyToVisibilityByClass'; public const PROPERTY_TO_VISIBILITY_BY_CLASS = 'property_to_visibility_by_class';
/** /**
* @var string[][] { class => [ property name => visibility ] } * @var array<string, array<string, int>>
*/ */
private $propertyToVisibilityByClass = []; private $propertyToVisibilityByClass = [];
@ -60,7 +61,7 @@ CODE_SAMPLE
[ [
self::PROPERTY_TO_VISIBILITY_BY_CLASS => [ self::PROPERTY_TO_VISIBILITY_BY_CLASS => [
'FrameworkClass' => [ 'FrameworkClass' => [
'someProperty' => 'protected', 'someProperty' => Visibility::PROTECTED,
], ],
], ],
] ]

View File

@ -17,11 +17,11 @@ final class ChangeMethodVisibility
private $method; private $method;
/** /**
* @var string * @var int
*/ */
private $visibility; private $visibility;
public function __construct(string $class, string $method, string $visibility) public function __construct(string $class, string $method, int $visibility)
{ {
$this->class = $class; $this->class = $class;
$this->method = $method; $this->method = $method;
@ -38,7 +38,7 @@ final class ChangeMethodVisibility
return $this->method; return $this->method;
} }
public function getVisibility(): string public function getVisibility(): int
{ {
return $this->visibility; return $this->visibility;
} }

View File

@ -17,11 +17,11 @@ final class ClassConstantVisibilityChange
private $constant; private $constant;
/** /**
* @var string * @var int
*/ */
private $visibility; private $visibility;
public function __construct(string $class, string $constant, string $visibility) public function __construct(string $class, string $constant, int $visibility)
{ {
$this->class = $class; $this->class = $class;
$this->constant = $constant; $this->constant = $constant;
@ -38,7 +38,7 @@ final class ClassConstantVisibilityChange
return $this->constant; return $this->constant;
} }
public function getVisibility(): string public function getVisibility(): int
{ {
return $this->visibility; return $this->visibility;
} }

View File

@ -5,6 +5,7 @@ declare(strict_types=1);
namespace Rector\Generic\Tests\Rector\ClassConst\ChangeConstantVisibilityRector; namespace Rector\Generic\Tests\Rector\ClassConst\ChangeConstantVisibilityRector;
use Iterator; use Iterator;
use Rector\Core\ValueObject\Visibility;
use Rector\Generic\Rector\ClassConst\ChangeConstantVisibilityRector; use Rector\Generic\Rector\ClassConst\ChangeConstantVisibilityRector;
use Rector\Generic\Tests\Rector\ClassConst\ChangeConstantVisibilityRector\Source\ParentObject; use Rector\Generic\Tests\Rector\ClassConst\ChangeConstantVisibilityRector\Source\ParentObject;
use Rector\Generic\ValueObject\ClassConstantVisibilityChange; use Rector\Generic\ValueObject\ClassConstantVisibilityChange;
@ -34,13 +35,21 @@ final class ChangeConstantVisibilityRectorTest extends AbstractRectorTestCase
return [ return [
ChangeConstantVisibilityRector::class => [ ChangeConstantVisibilityRector::class => [
ChangeConstantVisibilityRector::CLASS_CONSTANT_VISIBILITY_CHANGES => [ ChangeConstantVisibilityRector::CLASS_CONSTANT_VISIBILITY_CHANGES => [
new ClassConstantVisibilityChange(ParentObject::class, 'TO_BE_PUBLIC_CONSTANT', 'public'), new ClassConstantVisibilityChange(ParentObject::class, 'TO_BE_PUBLIC_CONSTANT', Visibility::PUBLIC),
new ClassConstantVisibilityChange(ParentObject::class, 'TO_BE_PROTECTED_CONSTANT', 'protected'), new ClassConstantVisibilityChange(
new ClassConstantVisibilityChange(ParentObject::class, 'TO_BE_PRIVATE_CONSTANT', 'private'), ParentObject::class,
'TO_BE_PROTECTED_CONSTANT',
Visibility::PROTECTED
),
new ClassConstantVisibilityChange(
ParentObject::class,
'TO_BE_PRIVATE_CONSTANT',
Visibility::PRIVATE
),
new ClassConstantVisibilityChange( new ClassConstantVisibilityChange(
'Rector\Generic\Tests\Rector\ClassConst\ChangeConstantVisibilityRector\Fixture\Fixture2', 'Rector\Generic\Tests\Rector\ClassConst\ChangeConstantVisibilityRector\Fixture\Fixture2',
'TO_BE_PRIVATE_CONSTANT', 'TO_BE_PRIVATE_CONSTANT',
'private' Visibility::PRIVATE
), ),
], ],
], ],

View File

@ -5,6 +5,7 @@ declare(strict_types=1);
namespace Rector\Generic\Tests\Rector\ClassMethod\ChangeMethodVisibilityRector; namespace Rector\Generic\Tests\Rector\ClassMethod\ChangeMethodVisibilityRector;
use Iterator; use Iterator;
use Rector\Core\ValueObject\Visibility;
use Rector\Generic\Rector\ClassMethod\ChangeMethodVisibilityRector; use Rector\Generic\Rector\ClassMethod\ChangeMethodVisibilityRector;
use Rector\Generic\Tests\Rector\ClassMethod\ChangeMethodVisibilityRector\Source\ParentObject; use Rector\Generic\Tests\Rector\ClassMethod\ChangeMethodVisibilityRector\Source\ParentObject;
use Rector\Generic\ValueObject\ChangeMethodVisibility; use Rector\Generic\ValueObject\ChangeMethodVisibility;
@ -34,10 +35,10 @@ final class ChangeMethodVisibilityRectorTest extends AbstractRectorTestCase
return [ return [
ChangeMethodVisibilityRector::class => [ ChangeMethodVisibilityRector::class => [
ChangeMethodVisibilityRector::METHOD_VISIBILITIES => [ ChangeMethodVisibilityRector::METHOD_VISIBILITIES => [
new ChangeMethodVisibility(ParentObject::class, 'toBePublicMethod', 'public'), new ChangeMethodVisibility(ParentObject::class, 'toBePublicMethod', Visibility::PUBLIC),
new ChangeMethodVisibility(ParentObject::class, 'toBeProtectedMethod', 'protected'), new ChangeMethodVisibility(ParentObject::class, 'toBeProtectedMethod', Visibility::PROTECTED),
new ChangeMethodVisibility(ParentObject::class, 'toBePrivateMethod', 'private'), new ChangeMethodVisibility(ParentObject::class, 'toBePrivateMethod', Visibility::PRIVATE),
new ChangeMethodVisibility(ParentObject::class, 'toBePublicStaticMethod', 'public'), new ChangeMethodVisibility(ParentObject::class, 'toBePublicStaticMethod', Visibility::PUBLIC),
], ],
], ],
]; ];

View File

@ -5,6 +5,7 @@ declare(strict_types=1);
namespace Rector\Generic\Tests\Rector\Property\ChangePropertyVisibilityRector; namespace Rector\Generic\Tests\Rector\Property\ChangePropertyVisibilityRector;
use Iterator; use Iterator;
use Rector\Core\ValueObject\Visibility;
use Rector\Generic\Rector\Property\ChangePropertyVisibilityRector; use Rector\Generic\Rector\Property\ChangePropertyVisibilityRector;
use Rector\Generic\Tests\Rector\Property\ChangePropertyVisibilityRector\Source\ParentObject; use Rector\Generic\Tests\Rector\Property\ChangePropertyVisibilityRector\Source\ParentObject;
use Rector\Testing\PHPUnit\AbstractRectorTestCase; use Rector\Testing\PHPUnit\AbstractRectorTestCase;
@ -34,13 +35,13 @@ final class ChangePropertyVisibilityRectorTest extends AbstractRectorTestCase
ChangePropertyVisibilityRector::class => [ ChangePropertyVisibilityRector::class => [
ChangePropertyVisibilityRector::PROPERTY_TO_VISIBILITY_BY_CLASS => [ ChangePropertyVisibilityRector::PROPERTY_TO_VISIBILITY_BY_CLASS => [
ParentObject::class => [ ParentObject::class => [
'toBePublicProperty' => 'public', 'toBePublicProperty' => Visibility::PUBLIC,
'toBeProtectedProperty' => 'protected', 'toBeProtectedProperty' => Visibility::PROTECTED,
'toBePrivateProperty' => 'private', 'toBePrivateProperty' => Visibility::PRIVATE,
'toBePublicStaticProperty' => 'public', 'toBePublicStaticProperty' => Visibility::PUBLIC,
], ],
'Rector\Generic\Tests\Rector\Property\ChangePropertyVisibilityRector\Fixture\Fixture3' => [ 'Rector\Generic\Tests\Rector\Property\ChangePropertyVisibilityRector\Fixture\Fixture3' => [
'toBePublicStaticProperty' => 'public', 'toBePublicStaticProperty' => Visibility::PUBLIC,
], ],
], ],
], ],

View File

@ -9,6 +9,7 @@ use PhpParser\Node\Stmt\Class_;
use PhpParser\Node\Stmt\ClassLike; use PhpParser\Node\Stmt\ClassLike;
use PhpParser\Node\Stmt\Property; use PhpParser\Node\Stmt\Property;
use PhpParser\Node\Stmt\Trait_; use PhpParser\Node\Stmt\Trait_;
use Rector\Core\ValueObject\Visibility;
use Rector\Order\PropertyRanker; use Rector\Order\PropertyRanker;
use Rector\Order\Rector\AbstractConstantPropertyMethodOrderRector; use Rector\Order\Rector\AbstractConstantPropertyMethodOrderRector;
use Rector\Order\ValueObject\PropertyNameRankAndPosition; use Rector\Order\ValueObject\PropertyNameRankAndPosition;
@ -137,7 +138,7 @@ CODE_SAMPLE
/** /**
* @param Class_|Trait_ $classLike * @param Class_|Trait_ $classLike
* @return array<string, Property[]> * @return array<int, Property[]>
*/ */
private function resolvePropertyByVisibilityByPosition(ClassLike $classLike): array private function resolvePropertyByVisibilityByPosition(ClassLike $classLike): array
{ {
@ -147,7 +148,7 @@ CODE_SAMPLE
continue; continue;
} }
$visibility = $this->getVisibilityAsString($classStmt); $visibility = $this->getVisibilityAsConstant($classStmt);
$propertyByVisibilityByPosition[$visibility][$position] = $classStmt; $propertyByVisibilityByPosition[$visibility][$position] = $classStmt;
} }
@ -177,16 +178,16 @@ CODE_SAMPLE
return $propertyNames; return $propertyNames;
} }
private function getVisibilityAsString(Property $property): string private function getVisibilityAsConstant(Property $property): int
{ {
if ($property->isPrivate()) { if ($property->isPrivate()) {
return 'private'; return Visibility::PRIVATE;
} }
if ($property->isProtected()) { if ($property->isProtected()) {
return 'protected'; return Visibility::PROTECTED;
} }
return 'public'; return Visibility::PUBLIC;
} }
} }

View File

@ -73,7 +73,8 @@ class SomeClass
} }
CODE_SAMPLE CODE_SAMPLE
, ,
$configuration), $configuration
),
]); ]);
} }

View File

@ -11,6 +11,7 @@ use PhpParser\Node\Stmt\ClassMethod;
use PhpParser\Node\Stmt\Property; use PhpParser\Node\Stmt\Property;
use Rector\Core\Exception\InvalidNodeTypeException; use Rector\Core\Exception\InvalidNodeTypeException;
use Rector\Core\Exception\ShouldNotHappenException; use Rector\Core\Exception\ShouldNotHappenException;
use Rector\Core\ValueObject\Visibility;
final class VisibilityManipulator final class VisibilityManipulator
{ {
@ -20,31 +21,21 @@ final class VisibilityManipulator
private const ALLOWED_NODE_TYPES = [ClassMethod::class, Property::class, ClassConst::class, Class_::class]; private const ALLOWED_NODE_TYPES = [ClassMethod::class, Property::class, ClassConst::class, Class_::class];
/** /**
* @var string * @var int[]
*/ */
private const STATIC = 'static'; private const ALLOWED_VISIBILITIES = [
Visibility::PUBLIC,
/** Visibility::PROTECTED,
* @var string Visibility::PRIVATE,
*/ Visibility::STATIC,
private const FINAL = 'final'; ];
/**
* @var string
*/
private const ABSTRACT = 'abstract';
/**
* @var string[]
*/
private const ALLOWED_VISIBILITIES = ['public', 'protected', 'private', 'static'];
/** /**
* @param ClassMethod|Property|ClassConst $node * @param ClassMethod|Property|ClassConst $node
*/ */
public function makeStatic(Node $node): void public function makeStatic(Node $node): void
{ {
$this->addVisibilityFlag($node, self::STATIC); $this->addVisibilityFlag($node, Visibility::STATIC);
} }
/** /**
@ -52,7 +43,7 @@ final class VisibilityManipulator
*/ */
public function makeAbstract(Node $node): void public function makeAbstract(Node $node): void
{ {
$this->addVisibilityFlag($node, self::ABSTRACT); $this->addVisibilityFlag($node, Visibility::ABSTRACT);
} }
/** /**
@ -72,7 +63,7 @@ final class VisibilityManipulator
*/ */
public function makeFinal(Node $node): void public function makeFinal(Node $node): void
{ {
$this->addVisibilityFlag($node, self::FINAL); $this->addVisibilityFlag($node, Visibility::FINAL);
} }
/** /**
@ -117,15 +108,15 @@ final class VisibilityManipulator
/** /**
* @param ClassMethod|Property|ClassConst $node * @param ClassMethod|Property|ClassConst $node
*/ */
public function changeNodeVisibility(Node $node, string $visibility): void public function changeNodeVisibility(Node $node, int $visibility): void
{ {
if ($visibility === 'public') { if ($visibility === Visibility::PUBLIC) {
$this->makePublic($node); $this->makePublic($node);
} elseif ($visibility === 'protected') { } elseif ($visibility === Visibility::PROTECTED) {
$this->makeProtected($node); $this->makeProtected($node);
} elseif ($visibility === 'private') { } elseif ($visibility === Visibility::PRIVATE) {
$this->makePrivate($node); $this->makePrivate($node);
} elseif ($visibility === 'static') { } elseif ($visibility === Visibility::STATIC) {
$this->makeStatic($node); $this->makeStatic($node);
} else { } else {
throw new ShouldNotHappenException(sprintf( throw new ShouldNotHappenException(sprintf(
@ -140,7 +131,7 @@ final class VisibilityManipulator
*/ */
public function makePublic(Node $node): void public function makePublic(Node $node): void
{ {
$this->replaceVisibilityFlag($node, 'public'); $this->replaceVisibilityFlag($node, Visibility::PUBLIC);
} }
/** /**
@ -148,7 +139,7 @@ final class VisibilityManipulator
*/ */
public function makeProtected(Node $node): void public function makeProtected(Node $node): void
{ {
$this->replaceVisibilityFlag($node, 'protected'); $this->replaceVisibilityFlag($node, Visibility::PROTECTED);
} }
/** /**
@ -156,39 +147,16 @@ final class VisibilityManipulator
*/ */
public function makePrivate(Node $node): void public function makePrivate(Node $node): void
{ {
$this->replaceVisibilityFlag($node, 'private'); $this->replaceVisibilityFlag($node, Visibility::PRIVATE);
} }
/** /**
* @param Class_|ClassMethod|Property|ClassConst $node * @param Class_|ClassMethod|Property|ClassConst $node
*/ */
private function addVisibilityFlag(Node $node, string $visibility): void private function addVisibilityFlag(Node $node, int $visibility): void
{ {
$this->ensureIsClassMethodOrProperty($node, __METHOD__); $this->ensureIsClassMethodOrProperty($node, __METHOD__);
$node->flags |= $visibility;
if ($visibility === 'public') {
$node->flags |= Class_::MODIFIER_PUBLIC;
}
if ($visibility === 'protected') {
$node->flags |= Class_::MODIFIER_PROTECTED;
}
if ($visibility === 'private') {
$node->flags |= Class_::MODIFIER_PRIVATE;
}
if ($visibility === self::STATIC) {
$node->flags |= Class_::MODIFIER_STATIC;
}
if ($visibility === self::ABSTRACT) {
$node->flags |= Class_::MODIFIER_ABSTRACT;
}
if ($visibility === self::FINAL) {
$node->flags |= Class_::MODIFIER_FINAL;
}
} }
private function ensureIsClassMethodOrProperty(Node $node, string $location): void private function ensureIsClassMethodOrProperty(Node $node, string $location): void
@ -210,16 +178,14 @@ final class VisibilityManipulator
/** /**
* @param ClassMethod|Property|ClassConst $node * @param ClassMethod|Property|ClassConst $node
*/ */
private function replaceVisibilityFlag(Node $node, string $visibility): void private function replaceVisibilityFlag(Node $node, int $visibility): void
{ {
$visibility = strtolower($visibility);
$isStatic = $node instanceof ClassMethod && $node->isStatic(); $isStatic = $node instanceof ClassMethod && $node->isStatic();
if ($isStatic) { if ($isStatic) {
$this->removeOriginalVisibilityFromFlags($node); $this->removeOriginalVisibilityFromFlags($node);
} }
if ($visibility !== self::STATIC && $visibility !== self::ABSTRACT && $visibility !== self::FINAL) { if ($visibility !== Visibility::STATIC && $visibility !== Visibility::ABSTRACT && $visibility !== Visibility::FINAL) {
$this->removeOriginalVisibilityFromFlags($node); $this->removeOriginalVisibilityFromFlags($node);
} }

View File

@ -33,7 +33,7 @@ trait VisibilityTrait
/** /**
* @param ClassMethod|Property|ClassConst $node * @param ClassMethod|Property|ClassConst $node
*/ */
public function changeNodeVisibility(Node $node, string $visibility): void public function changeNodeVisibility(Node $node, int $visibility): void
{ {
$this->visibilityManipulator->changeNodeVisibility($node, $visibility); $this->visibilityManipulator->changeNodeVisibility($node, $visibility);
} }

View File

@ -0,0 +1,40 @@
<?php
declare(strict_types=1);
namespace Rector\Core\ValueObject;
use PhpParser\Node\Stmt\Class_;
final class Visibility
{
/**
* @var int
*/
public const PUBLIC = Class_::MODIFIER_PUBLIC;
/**
* @var int
*/
public const PROTECTED = Class_::MODIFIER_PROTECTED;
/**
* @var int
*/
public const PRIVATE = Class_::MODIFIER_PRIVATE;
/**
* @var int
*/
public const STATIC = Class_::MODIFIER_STATIC;
/**
* @var int
*/
public const ABSTRACT = Class_::MODIFIER_ABSTRACT;
/**
* @var int
*/
public const FINAL = Class_::MODIFIER_FINAL;
}

View File

@ -1,12 +0,0 @@
<?php
declare(strict_types=1);
// class that is used all over the code
// autoloaded manually via composer to keep the rest of code clean
namespace SomeNamespace;
final class SomeClass
{
}