mirror of
https://github.com/rectorphp/rector.git
synced 2025-01-17 05:18:18 +01:00
Add Visibility consts (#4981)
This commit is contained in:
parent
d03fb14c3f
commit
5a47e36b72
@ -124,18 +124,18 @@ final class AutoloadIncluder
|
||||
$this->loadIfExistsAndNotLoadedYet($fileToAutoload);
|
||||
}
|
||||
|
||||
private function loadIfExistsAndNotLoadedYet(string $file): void
|
||||
private function loadIfExistsAndNotLoadedYet(string $filePath): void
|
||||
{
|
||||
if (! file_exists($file)) {
|
||||
if (! file_exists($filePath)) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (in_array($file, $this->alreadyLoadedAutoloadFiles, true)) {
|
||||
if (in_array($filePath, $this->alreadyLoadedAutoloadFiles, true)) {
|
||||
return;
|
||||
}
|
||||
|
||||
$this->alreadyLoadedAutoloadFiles[] = realpath($file);
|
||||
$this->alreadyLoadedAutoloadFiles[] = realpath($filePath);
|
||||
|
||||
require_once $file;
|
||||
require_once $filePath;
|
||||
}
|
||||
}
|
||||
|
@ -11,8 +11,8 @@
|
||||
"bin": ["bin/rector"],
|
||||
"require": {
|
||||
"php": "^7.3|^8.0",
|
||||
"ext-json": "*",
|
||||
"ext-dom": "*",
|
||||
"ext-json": "*",
|
||||
"composer/xdebug-handler": "^1.4",
|
||||
"doctrine/annotations": "^1.11",
|
||||
"doctrine/inflector": "^2.0",
|
||||
@ -31,13 +31,13 @@
|
||||
"symfony/finder": "^4.4.8|^5.1",
|
||||
"symfony/http-kernel": "^4.4.8|^5.1",
|
||||
"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/simple-php-doc-parser": "^9.0.14",
|
||||
"symplify/smart-file-system": "^9.0.14",
|
||||
"symplify/rule-doc-generator": "^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",
|
||||
"webmozart/assert": "^1.9"
|
||||
},
|
||||
@ -174,8 +174,7 @@
|
||||
"rules/cakephp/tests/Rector/Namespace_/AppUsesStaticCallToUseStatementRector/Source",
|
||||
"rules/psr4/tests/Rector/Namespace_/MultipleClassFileToPsr4ClassesRector/Source",
|
||||
"rules/renaming/tests/Rector/FileWithoutNamespace/PseudoNamespaceToNamespaceRector/Source",
|
||||
"rules/symfony4/tests/Rector/MethodCall/ContainerGetToConstructorInjectionRector/Source",
|
||||
"tests/Source"
|
||||
"rules/symfony4/tests/Rector/MethodCall/ContainerGetToConstructorInjectionRector/Source"
|
||||
],
|
||||
"files": [
|
||||
"rules/coding-style/tests/Rector/Namespace_/ImportFullyQualifiedNamesRector/Source/AnotherClass.php",
|
||||
@ -324,8 +323,7 @@
|
||||
"rector": "bin/rector process --config rector-ci.php --ansi"
|
||||
},
|
||||
"config": {
|
||||
"sort-packages": true,
|
||||
"process-timeout": 0
|
||||
"sort-packages": true
|
||||
},
|
||||
"minimum-stability": "dev",
|
||||
"prefer-stable": true
|
||||
|
@ -4,6 +4,7 @@ declare(strict_types=1);
|
||||
|
||||
use Rector\CakePHP\Rector\MethodCall\ModalToGetSetRector;
|
||||
use Rector\CakePHP\ValueObject\ModalToGetSet;
|
||||
use Rector\Core\ValueObject\Visibility;
|
||||
use Rector\Generic\Rector\ClassMethod\ChangeMethodVisibilityRector;
|
||||
use Rector\Generic\Rector\ClassMethod\NormalToFluentRector;
|
||||
use Rector\Generic\ValueObject\ChangeMethodVisibility;
|
||||
@ -223,8 +224,8 @@ return static function (ContainerConfigurator $containerConfigurator): void {
|
||||
$services->set(ChangeMethodVisibilityRector::class)
|
||||
->call('configure', [[
|
||||
ChangeMethodVisibilityRector::METHOD_VISIBILITIES => ValueObjectInliner::inline([
|
||||
new ChangeMethodVisibility('Cake\Mailer\MailerAwareTrait', 'getMailer', 'protected'),
|
||||
new ChangeMethodVisibility('Cake\View\CellTrait', 'cell', 'protected'),
|
||||
new ChangeMethodVisibility('Cake\Mailer\MailerAwareTrait', 'getMailer', Visibility::PROTECTED),
|
||||
new ChangeMethodVisibility('Cake\View\CellTrait', 'cell', Visibility::PROTECTED),
|
||||
]),
|
||||
]]);
|
||||
|
||||
|
@ -2,6 +2,7 @@
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
use Rector\Core\ValueObject\Visibility;
|
||||
use Rector\Generic\Rector\ClassMethod\ChangeMethodVisibilityRector;
|
||||
use Rector\Generic\ValueObject\ChangeMethodVisibility;
|
||||
use Rector\NetteToSymfony\Rector\MethodCall\WrapTransParameterNameRector;
|
||||
@ -17,7 +18,7 @@ return static function (ContainerConfigurator $containerConfigurator): void {
|
||||
$services->set(ChangeMethodVisibilityRector::class)
|
||||
->call('configure', [[
|
||||
ChangeMethodVisibilityRector::METHOD_VISIBILITIES => ValueObjectInliner::inline([
|
||||
new ChangeMethodVisibility('Kdyby\Events\Subscriber', 'getSubscribedEvents', 'static'),
|
||||
new ChangeMethodVisibility('Kdyby\Events\Subscriber', 'getSubscribedEvents', Visibility::STATIC),
|
||||
]),
|
||||
]]);
|
||||
|
||||
|
@ -2,6 +2,7 @@
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
use Rector\Core\ValueObject\Visibility;
|
||||
use Rector\Generic\Rector\ClassMethod\ChangeMethodVisibilityRector;
|
||||
use Rector\Generic\ValueObject\ChangeMethodVisibility;
|
||||
use Rector\Renaming\Rector\MethodCall\RenameMethodRector;
|
||||
@ -33,9 +34,9 @@ return static function (ContainerConfigurator $containerConfigurator): void {
|
||||
$services->set(ChangeMethodVisibilityRector::class)
|
||||
->call('configure', [[
|
||||
ChangeMethodVisibilityRector::METHOD_VISIBILITIES => ValueObjectInliner::inline([
|
||||
new ChangeMethodVisibility('Illuminate\Routing\Router', 'addRoute', 'public'),
|
||||
new ChangeMethodVisibility('Illuminate\Contracts\Auth\Access\Gate', 'raw', 'public'),
|
||||
new ChangeMethodVisibility('Illuminate\Database\Grammar', 'getDateFormat', 'public'),
|
||||
new ChangeMethodVisibility('Illuminate\Routing\Router', 'addRoute', Visibility::PUBLIC),
|
||||
new ChangeMethodVisibility('Illuminate\Contracts\Auth\Access\Gate', 'raw', Visibility::PUBLIC),
|
||||
new ChangeMethodVisibility('Illuminate\Database\Grammar', 'getDateFormat', Visibility::PUBLIC),
|
||||
]),
|
||||
]]);
|
||||
};
|
||||
|
@ -2,6 +2,7 @@
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
use Rector\Core\ValueObject\Visibility;
|
||||
use Rector\Generic\Rector\ClassMethod\ArgumentAdderRector;
|
||||
use Rector\Generic\Rector\ClassMethod\ArgumentRemoverRector;
|
||||
use Rector\Generic\Rector\ClassMethod\ChangeMethodVisibilityRector;
|
||||
@ -21,8 +22,8 @@ return static function (ContainerConfigurator $containerConfigurator): void {
|
||||
$services = $containerConfigurator->services();
|
||||
$services->set(ChangeMethodVisibilityRector::class)->call('configure', [[
|
||||
ChangeMethodVisibilityRector::METHOD_VISIBILITIES => ValueObjectInliner::inline([
|
||||
new ChangeMethodVisibility('Illuminate\Routing\Router', 'addRoute', 'public'),
|
||||
new ChangeMethodVisibility('Illuminate\Contracts\Auth\Access\Gate', 'raw', 'public'),
|
||||
new ChangeMethodVisibility('Illuminate\Routing\Router', 'addRoute', Visibility::PUBLIC),
|
||||
new ChangeMethodVisibility('Illuminate\Contracts\Auth\Access\Gate', 'raw', Visibility::PUBLIC),
|
||||
]),
|
||||
]]);
|
||||
$services->set(ArgumentAdderRector::class)->call('configure', [[
|
||||
|
@ -2,6 +2,7 @@
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
use Rector\Core\ValueObject\Visibility;
|
||||
use Rector\Generic\Rector\ClassMethod\ArgumentAdderRector;
|
||||
use Rector\Generic\Rector\ClassMethod\ChangeMethodVisibilityRector;
|
||||
use Rector\Generic\Rector\Expression\MethodCallToReturnRector;
|
||||
@ -86,7 +87,11 @@ return static function (ContainerConfigurator $containerConfigurator): void {
|
||||
$services->set(ChangeMethodVisibilityRector::class)
|
||||
->call('configure', [[
|
||||
ChangeMethodVisibilityRector::METHOD_VISIBILITIES => ValueObjectInliner::inline([
|
||||
new ChangeMethodVisibility('Illuminate\Foundation\Http\FormRequest', 'validationData', 'public'),
|
||||
new ChangeMethodVisibility(
|
||||
'Illuminate\Foundation\Http\FormRequest',
|
||||
'validationData',
|
||||
Visibility::PUBLIC
|
||||
),
|
||||
]),
|
||||
]]);
|
||||
|
||||
|
@ -5,6 +5,7 @@ declare(strict_types=1);
|
||||
use PHPStan\Type\IterableType;
|
||||
use PHPStan\Type\MixedType;
|
||||
|
||||
use Rector\Core\ValueObject\Visibility;
|
||||
use Rector\Generic\Rector\ClassMethod\ArgumentAdderRector;
|
||||
use Rector\Generic\Rector\ClassMethod\ArgumentDefaultValueReplacerRector;
|
||||
use Rector\Generic\Rector\ClassMethod\ArgumentRemoverRector;
|
||||
@ -182,7 +183,7 @@ return static function (ContainerConfigurator $containerConfigurator): void {
|
||||
new ChangeMethodVisibility(
|
||||
'Symfony\Component\Form\AbstractTypeExtension',
|
||||
'getExtendedTypes',
|
||||
'static'
|
||||
Visibility::STATIC
|
||||
),
|
||||
]),
|
||||
]]);
|
||||
|
@ -23,6 +23,11 @@ final class RectorSetProvider extends AbstractSetProvider
|
||||
*/
|
||||
private const DASH_NUMBER_REGEX = '#\-(\d+)#';
|
||||
|
||||
/**
|
||||
* @var string[]
|
||||
*/
|
||||
private const SET_LIST_CLASSES = [SetList::class, DowngradeSetList::class];
|
||||
|
||||
/**
|
||||
* @var Set[]
|
||||
*/
|
||||
@ -30,8 +35,7 @@ final class RectorSetProvider extends AbstractSetProvider
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
$setListClasses = [SetList::class, DowngradeSetList::class];
|
||||
foreach ($setListClasses as $setListClass) {
|
||||
foreach (self::SET_LIST_CLASSES as $setListClass) {
|
||||
$setListReflectionClass = new ReflectionClass($setListClass);
|
||||
$this->hydrateSetsFromConstants($setListReflectionClass);
|
||||
}
|
||||
|
@ -707,4 +707,6 @@ parameters:
|
||||
message: '#Use "\$this\-\><method\>\(\)" instead of "self\:\:<method\>\(\)" to call local method#'
|
||||
paths:
|
||||
- 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#'
|
||||
|
@ -8,6 +8,7 @@ use PhpParser\Node;
|
||||
use PhpParser\Node\Stmt\ClassConst;
|
||||
use Rector\Core\Contract\Rector\ConfigurableRectorInterface;
|
||||
use Rector\Core\Rector\AbstractRector;
|
||||
use Rector\Core\ValueObject\Visibility;
|
||||
use Rector\Generic\ValueObject\ClassConstantVisibilityChange;
|
||||
use Symplify\RuleDocGenerator\ValueObject\CodeSample\ConfiguredCodeSample;
|
||||
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
|
||||
@ -60,7 +61,11 @@ CODE_SAMPLE
|
||||
,
|
||||
[
|
||||
self::CLASS_CONSTANT_VISIBILITY_CHANGES => [
|
||||
new ClassConstantVisibilityChange('ParentObject', 'SOME_CONSTANT', 'protected'),
|
||||
new ClassConstantVisibilityChange(
|
||||
'ParentObject',
|
||||
'SOME_CONSTANT',
|
||||
Visibility::PROTECTED
|
||||
),
|
||||
],
|
||||
]
|
||||
),
|
||||
|
@ -8,6 +8,7 @@ use PhpParser\Node;
|
||||
use PhpParser\Node\Stmt\ClassMethod;
|
||||
use Rector\Core\Contract\Rector\ConfigurableRectorInterface;
|
||||
use Rector\Core\Rector\AbstractRector;
|
||||
use Rector\Core\ValueObject\Visibility;
|
||||
use Rector\Generic\ValueObject\ChangeMethodVisibility;
|
||||
use Rector\NodeTypeResolver\Node\AttributeKey;
|
||||
use Symplify\RuleDocGenerator\ValueObject\CodeSample\ConfiguredCodeSample;
|
||||
@ -69,7 +70,11 @@ CODE_SAMPLE
|
||||
,
|
||||
[
|
||||
self::METHOD_VISIBILITIES => [
|
||||
new ChangeMethodVisibility('FrameworkClass', 'someMethod', 'protected'),
|
||||
new ChangeMethodVisibility(
|
||||
'FrameworkClass',
|
||||
'someMethod',
|
||||
Visibility::PROTECTED
|
||||
),
|
||||
],
|
||||
]
|
||||
),
|
||||
|
@ -8,6 +8,7 @@ use PhpParser\Node;
|
||||
use PhpParser\Node\Stmt\Property;
|
||||
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;
|
||||
@ -20,10 +21,10 @@ final class ChangePropertyVisibilityRector extends AbstractRector implements Con
|
||||
/**
|
||||
* @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 = [];
|
||||
|
||||
@ -60,7 +61,7 @@ CODE_SAMPLE
|
||||
[
|
||||
self::PROPERTY_TO_VISIBILITY_BY_CLASS => [
|
||||
'FrameworkClass' => [
|
||||
'someProperty' => 'protected',
|
||||
'someProperty' => Visibility::PROTECTED,
|
||||
],
|
||||
],
|
||||
]
|
||||
|
@ -17,11 +17,11 @@ final class ChangeMethodVisibility
|
||||
private $method;
|
||||
|
||||
/**
|
||||
* @var string
|
||||
* @var int
|
||||
*/
|
||||
private $visibility;
|
||||
|
||||
public function __construct(string $class, string $method, string $visibility)
|
||||
public function __construct(string $class, string $method, int $visibility)
|
||||
{
|
||||
$this->class = $class;
|
||||
$this->method = $method;
|
||||
@ -38,7 +38,7 @@ final class ChangeMethodVisibility
|
||||
return $this->method;
|
||||
}
|
||||
|
||||
public function getVisibility(): string
|
||||
public function getVisibility(): int
|
||||
{
|
||||
return $this->visibility;
|
||||
}
|
||||
|
@ -17,11 +17,11 @@ final class ClassConstantVisibilityChange
|
||||
private $constant;
|
||||
|
||||
/**
|
||||
* @var string
|
||||
* @var int
|
||||
*/
|
||||
private $visibility;
|
||||
|
||||
public function __construct(string $class, string $constant, string $visibility)
|
||||
public function __construct(string $class, string $constant, int $visibility)
|
||||
{
|
||||
$this->class = $class;
|
||||
$this->constant = $constant;
|
||||
@ -38,7 +38,7 @@ final class ClassConstantVisibilityChange
|
||||
return $this->constant;
|
||||
}
|
||||
|
||||
public function getVisibility(): string
|
||||
public function getVisibility(): int
|
||||
{
|
||||
return $this->visibility;
|
||||
}
|
||||
|
@ -5,6 +5,7 @@ declare(strict_types=1);
|
||||
namespace Rector\Generic\Tests\Rector\ClassConst\ChangeConstantVisibilityRector;
|
||||
|
||||
use Iterator;
|
||||
use Rector\Core\ValueObject\Visibility;
|
||||
use Rector\Generic\Rector\ClassConst\ChangeConstantVisibilityRector;
|
||||
use Rector\Generic\Tests\Rector\ClassConst\ChangeConstantVisibilityRector\Source\ParentObject;
|
||||
use Rector\Generic\ValueObject\ClassConstantVisibilityChange;
|
||||
@ -34,13 +35,21 @@ final class ChangeConstantVisibilityRectorTest extends AbstractRectorTestCase
|
||||
return [
|
||||
ChangeConstantVisibilityRector::class => [
|
||||
ChangeConstantVisibilityRector::CLASS_CONSTANT_VISIBILITY_CHANGES => [
|
||||
new ClassConstantVisibilityChange(ParentObject::class, 'TO_BE_PUBLIC_CONSTANT', 'public'),
|
||||
new ClassConstantVisibilityChange(ParentObject::class, 'TO_BE_PROTECTED_CONSTANT', 'protected'),
|
||||
new ClassConstantVisibilityChange(ParentObject::class, 'TO_BE_PRIVATE_CONSTANT', 'private'),
|
||||
new ClassConstantVisibilityChange(ParentObject::class, 'TO_BE_PUBLIC_CONSTANT', Visibility::PUBLIC),
|
||||
new ClassConstantVisibilityChange(
|
||||
ParentObject::class,
|
||||
'TO_BE_PROTECTED_CONSTANT',
|
||||
Visibility::PROTECTED
|
||||
),
|
||||
new ClassConstantVisibilityChange(
|
||||
ParentObject::class,
|
||||
'TO_BE_PRIVATE_CONSTANT',
|
||||
Visibility::PRIVATE
|
||||
),
|
||||
new ClassConstantVisibilityChange(
|
||||
'Rector\Generic\Tests\Rector\ClassConst\ChangeConstantVisibilityRector\Fixture\Fixture2',
|
||||
'TO_BE_PRIVATE_CONSTANT',
|
||||
'private'
|
||||
Visibility::PRIVATE
|
||||
),
|
||||
],
|
||||
],
|
||||
|
@ -5,6 +5,7 @@ declare(strict_types=1);
|
||||
namespace Rector\Generic\Tests\Rector\ClassMethod\ChangeMethodVisibilityRector;
|
||||
|
||||
use Iterator;
|
||||
use Rector\Core\ValueObject\Visibility;
|
||||
use Rector\Generic\Rector\ClassMethod\ChangeMethodVisibilityRector;
|
||||
use Rector\Generic\Tests\Rector\ClassMethod\ChangeMethodVisibilityRector\Source\ParentObject;
|
||||
use Rector\Generic\ValueObject\ChangeMethodVisibility;
|
||||
@ -34,10 +35,10 @@ final class ChangeMethodVisibilityRectorTest extends AbstractRectorTestCase
|
||||
return [
|
||||
ChangeMethodVisibilityRector::class => [
|
||||
ChangeMethodVisibilityRector::METHOD_VISIBILITIES => [
|
||||
new ChangeMethodVisibility(ParentObject::class, 'toBePublicMethod', 'public'),
|
||||
new ChangeMethodVisibility(ParentObject::class, 'toBeProtectedMethod', 'protected'),
|
||||
new ChangeMethodVisibility(ParentObject::class, 'toBePrivateMethod', 'private'),
|
||||
new ChangeMethodVisibility(ParentObject::class, 'toBePublicStaticMethod', 'public'),
|
||||
new ChangeMethodVisibility(ParentObject::class, 'toBePublicMethod', Visibility::PUBLIC),
|
||||
new ChangeMethodVisibility(ParentObject::class, 'toBeProtectedMethod', Visibility::PROTECTED),
|
||||
new ChangeMethodVisibility(ParentObject::class, 'toBePrivateMethod', Visibility::PRIVATE),
|
||||
new ChangeMethodVisibility(ParentObject::class, 'toBePublicStaticMethod', Visibility::PUBLIC),
|
||||
],
|
||||
],
|
||||
];
|
||||
|
@ -5,6 +5,7 @@ declare(strict_types=1);
|
||||
namespace Rector\Generic\Tests\Rector\Property\ChangePropertyVisibilityRector;
|
||||
|
||||
use Iterator;
|
||||
use Rector\Core\ValueObject\Visibility;
|
||||
use Rector\Generic\Rector\Property\ChangePropertyVisibilityRector;
|
||||
use Rector\Generic\Tests\Rector\Property\ChangePropertyVisibilityRector\Source\ParentObject;
|
||||
use Rector\Testing\PHPUnit\AbstractRectorTestCase;
|
||||
@ -34,13 +35,13 @@ final class ChangePropertyVisibilityRectorTest extends AbstractRectorTestCase
|
||||
ChangePropertyVisibilityRector::class => [
|
||||
ChangePropertyVisibilityRector::PROPERTY_TO_VISIBILITY_BY_CLASS => [
|
||||
ParentObject::class => [
|
||||
'toBePublicProperty' => 'public',
|
||||
'toBeProtectedProperty' => 'protected',
|
||||
'toBePrivateProperty' => 'private',
|
||||
'toBePublicStaticProperty' => 'public',
|
||||
'toBePublicProperty' => Visibility::PUBLIC,
|
||||
'toBeProtectedProperty' => Visibility::PROTECTED,
|
||||
'toBePrivateProperty' => Visibility::PRIVATE,
|
||||
'toBePublicStaticProperty' => Visibility::PUBLIC,
|
||||
],
|
||||
'Rector\Generic\Tests\Rector\Property\ChangePropertyVisibilityRector\Fixture\Fixture3' => [
|
||||
'toBePublicStaticProperty' => 'public',
|
||||
'toBePublicStaticProperty' => Visibility::PUBLIC,
|
||||
],
|
||||
],
|
||||
],
|
||||
|
@ -9,6 +9,7 @@ use PhpParser\Node\Stmt\Class_;
|
||||
use PhpParser\Node\Stmt\ClassLike;
|
||||
use PhpParser\Node\Stmt\Property;
|
||||
use PhpParser\Node\Stmt\Trait_;
|
||||
use Rector\Core\ValueObject\Visibility;
|
||||
use Rector\Order\PropertyRanker;
|
||||
use Rector\Order\Rector\AbstractConstantPropertyMethodOrderRector;
|
||||
use Rector\Order\ValueObject\PropertyNameRankAndPosition;
|
||||
@ -137,7 +138,7 @@ CODE_SAMPLE
|
||||
|
||||
/**
|
||||
* @param Class_|Trait_ $classLike
|
||||
* @return array<string, Property[]>
|
||||
* @return array<int, Property[]>
|
||||
*/
|
||||
private function resolvePropertyByVisibilityByPosition(ClassLike $classLike): array
|
||||
{
|
||||
@ -147,7 +148,7 @@ CODE_SAMPLE
|
||||
continue;
|
||||
}
|
||||
|
||||
$visibility = $this->getVisibilityAsString($classStmt);
|
||||
$visibility = $this->getVisibilityAsConstant($classStmt);
|
||||
$propertyByVisibilityByPosition[$visibility][$position] = $classStmt;
|
||||
}
|
||||
|
||||
@ -177,16 +178,16 @@ CODE_SAMPLE
|
||||
return $propertyNames;
|
||||
}
|
||||
|
||||
private function getVisibilityAsString(Property $property): string
|
||||
private function getVisibilityAsConstant(Property $property): int
|
||||
{
|
||||
if ($property->isPrivate()) {
|
||||
return 'private';
|
||||
return Visibility::PRIVATE;
|
||||
}
|
||||
|
||||
if ($property->isProtected()) {
|
||||
return 'protected';
|
||||
return Visibility::PROTECTED;
|
||||
}
|
||||
|
||||
return 'public';
|
||||
return Visibility::PUBLIC;
|
||||
}
|
||||
}
|
||||
|
@ -73,7 +73,8 @@ class SomeClass
|
||||
}
|
||||
CODE_SAMPLE
|
||||
,
|
||||
$configuration),
|
||||
$configuration
|
||||
),
|
||||
]);
|
||||
}
|
||||
|
||||
|
@ -11,6 +11,7 @@ use PhpParser\Node\Stmt\ClassMethod;
|
||||
use PhpParser\Node\Stmt\Property;
|
||||
use Rector\Core\Exception\InvalidNodeTypeException;
|
||||
use Rector\Core\Exception\ShouldNotHappenException;
|
||||
use Rector\Core\ValueObject\Visibility;
|
||||
|
||||
final class VisibilityManipulator
|
||||
{
|
||||
@ -20,31 +21,21 @@ final class VisibilityManipulator
|
||||
private const ALLOWED_NODE_TYPES = [ClassMethod::class, Property::class, ClassConst::class, Class_::class];
|
||||
|
||||
/**
|
||||
* @var string
|
||||
* @var int[]
|
||||
*/
|
||||
private const STATIC = 'static';
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
private const FINAL = 'final';
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
private const ABSTRACT = 'abstract';
|
||||
|
||||
/**
|
||||
* @var string[]
|
||||
*/
|
||||
private const ALLOWED_VISIBILITIES = ['public', 'protected', 'private', 'static'];
|
||||
private const ALLOWED_VISIBILITIES = [
|
||||
Visibility::PUBLIC,
|
||||
Visibility::PROTECTED,
|
||||
Visibility::PRIVATE,
|
||||
Visibility::STATIC,
|
||||
];
|
||||
|
||||
/**
|
||||
* @param ClassMethod|Property|ClassConst $node
|
||||
*/
|
||||
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
|
||||
{
|
||||
$this->addVisibilityFlag($node, self::ABSTRACT);
|
||||
$this->addVisibilityFlag($node, Visibility::ABSTRACT);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -72,7 +63,7 @@ final class VisibilityManipulator
|
||||
*/
|
||||
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
|
||||
*/
|
||||
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);
|
||||
} elseif ($visibility === 'protected') {
|
||||
} elseif ($visibility === Visibility::PROTECTED) {
|
||||
$this->makeProtected($node);
|
||||
} elseif ($visibility === 'private') {
|
||||
} elseif ($visibility === Visibility::PRIVATE) {
|
||||
$this->makePrivate($node);
|
||||
} elseif ($visibility === 'static') {
|
||||
} elseif ($visibility === Visibility::STATIC) {
|
||||
$this->makeStatic($node);
|
||||
} else {
|
||||
throw new ShouldNotHappenException(sprintf(
|
||||
@ -140,7 +131,7 @@ final class VisibilityManipulator
|
||||
*/
|
||||
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
|
||||
{
|
||||
$this->replaceVisibilityFlag($node, 'protected');
|
||||
$this->replaceVisibilityFlag($node, Visibility::PROTECTED);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -156,39 +147,16 @@ final class VisibilityManipulator
|
||||
*/
|
||||
public function makePrivate(Node $node): void
|
||||
{
|
||||
$this->replaceVisibilityFlag($node, 'private');
|
||||
$this->replaceVisibilityFlag($node, Visibility::PRIVATE);
|
||||
}
|
||||
|
||||
/**
|
||||
* @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__);
|
||||
|
||||
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;
|
||||
}
|
||||
$node->flags |= $visibility;
|
||||
}
|
||||
|
||||
private function ensureIsClassMethodOrProperty(Node $node, string $location): void
|
||||
@ -210,16 +178,14 @@ final class VisibilityManipulator
|
||||
/**
|
||||
* @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();
|
||||
if ($isStatic) {
|
||||
$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);
|
||||
}
|
||||
|
||||
|
@ -33,7 +33,7 @@ trait VisibilityTrait
|
||||
/**
|
||||
* @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);
|
||||
}
|
||||
|
40
src/ValueObject/Visibility.php
Normal file
40
src/ValueObject/Visibility.php
Normal 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;
|
||||
}
|
@ -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
|
||||
{
|
||||
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user