mirror of
https://github.com/rectorphp/rector.git
synced 2024-06-26 04:42:36 +00:00
[Downgrade PHP 7.1] Class constant visibility (#4633)
This commit is contained in:
parent
eeec7d8cb9
commit
05688bd36b
|
@ -2,6 +2,7 @@
|
|||
|
||||
declare(strict_types=1);
|
||||
|
||||
use Rector\DowngradePhp71\Rector\ClassConst\DowngradeClassConstantVisibilityRector;
|
||||
use Rector\DowngradePhp71\Rector\FunctionLike\DowngradeNullableTypeParamDeclarationRector;
|
||||
use Rector\DowngradePhp71\Rector\FunctionLike\DowngradeNullableTypeReturnDeclarationRector;
|
||||
use Rector\DowngradePhp71\Rector\FunctionLike\DowngradeVoidTypeReturnDeclarationRector;
|
||||
|
@ -12,4 +13,5 @@ return static function (ContainerConfigurator $containerConfigurator): void {
|
|||
$services->set(DowngradeNullableTypeParamDeclarationRector::class);
|
||||
$services->set(DowngradeNullableTypeReturnDeclarationRector::class);
|
||||
$services->set(DowngradeVoidTypeReturnDeclarationRector::class);
|
||||
$services->set(DowngradeClassConstantVisibilityRector::class);
|
||||
};
|
||||
|
|
|
@ -4401,6 +4401,29 @@ Replace array spread with `array_merge` function
|
|||
|
||||
<br>
|
||||
|
||||
## DowngradeClassConstantVisibilityRector
|
||||
|
||||
Downgrade class constant visibility
|
||||
|
||||
- class: `Rector\DowngradePhp71\Rector\ClassConst\DowngradeClassConstantVisibilityRector`
|
||||
|
||||
```diff
|
||||
<?php
|
||||
|
||||
class SomeClass
|
||||
{
|
||||
const PUBLIC_CONST_A = 1;
|
||||
- public const PUBLIC_CONST_B = 2;
|
||||
+ const PUBLIC_CONST_B = 2;
|
||||
- protected const PROTECTED_CONST = 3;
|
||||
+ const PROTECTED_CONST = 3;
|
||||
- private const PRIVATE_CONST = 4;
|
||||
+ const PRIVATE_CONST = 4;
|
||||
}
|
||||
```
|
||||
|
||||
<br>
|
||||
|
||||
## DowngradeFlexibleHeredocSyntaxRector
|
||||
|
||||
Changes heredoc/nowdoc that contains closing word to safe wrapper name
|
||||
|
|
|
@ -0,0 +1,69 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Rector\DowngradePhp71\Rector\ClassConst;
|
||||
|
||||
use PhpParser\Node;
|
||||
use PhpParser\Node\Stmt\ClassConst;
|
||||
use Rector\DowngradePhp71\Rector\FunctionLike\AbstractDowngradeRector;
|
||||
use Symplify\RuleDocGenerator\ValueObject\CodeSample\ConfiguredCodeSample;
|
||||
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
|
||||
|
||||
/**
|
||||
* @see \Rector\DowngradePhp71\Tests\Rector\ClassConst\DowngradeClassConstantVisibilityRectorTest
|
||||
*/
|
||||
final class DowngradeClassConstantVisibilityRector extends AbstractDowngradeRector
|
||||
{
|
||||
public function getRuleDefinition(): RuleDefinition
|
||||
{
|
||||
return new RuleDefinition(
|
||||
'Downgrade class constant visibility',
|
||||
[
|
||||
new ConfiguredCodeSample(
|
||||
<<<'CODE_SAMPLE'
|
||||
<?php
|
||||
|
||||
class SomeClass
|
||||
{
|
||||
public const PUBLIC_CONST_B = 2;
|
||||
protected const PROTECTED_CONST = 3;
|
||||
private const PRIVATE_CONST = 4;
|
||||
}
|
||||
CODE_SAMPLE
|
||||
,
|
||||
<<<'CODE_SAMPLE'
|
||||
<?php
|
||||
|
||||
class SomeClass
|
||||
{
|
||||
const PUBLIC_CONST_B = 2;
|
||||
const PROTECTED_CONST = 3;
|
||||
const PRIVATE_CONST = 4;
|
||||
}
|
||||
CODE_SAMPLE
|
||||
,
|
||||
[]
|
||||
),
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string[]
|
||||
*/
|
||||
public function getNodeTypes(): array
|
||||
{
|
||||
return [ClassConst::class];
|
||||
}
|
||||
|
||||
/**
|
||||
* @param ClassConst $node
|
||||
*/
|
||||
public function refactor(Node $node): ?Node
|
||||
{
|
||||
$this->removeVisibility($node);
|
||||
|
||||
return $node;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,37 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Rector\DowngradePhp71\Tests\Rector\ClassConst;
|
||||
|
||||
use Iterator;
|
||||
use Rector\Core\ValueObject\PhpVersionFeature;
|
||||
use Rector\DowngradePhp71\Rector\ClassConst\DowngradeClassConstantVisibilityRector;
|
||||
use Rector\Testing\PHPUnit\AbstractRectorTestCase;
|
||||
use Symplify\SmartFileSystem\SmartFileInfo;
|
||||
|
||||
final class DowngradeClassConstantVisibilityRectorTest extends AbstractRectorTestCase
|
||||
{
|
||||
/**
|
||||
* @dataProvider provideData()
|
||||
*/
|
||||
public function test(SmartFileInfo $fileInfo): void
|
||||
{
|
||||
$this->doTestFileInfo($fileInfo);
|
||||
}
|
||||
|
||||
public function provideData(): Iterator
|
||||
{
|
||||
return $this->yieldFilesFromDirectory(__DIR__ . '/Fixture');
|
||||
}
|
||||
|
||||
protected function getRectorClass(): string
|
||||
{
|
||||
return DowngradeClassConstantVisibilityRector::class;
|
||||
}
|
||||
|
||||
protected function getPhpVersion(): string
|
||||
{
|
||||
return PhpVersionFeature::BEFORE_CONSTANT_VISIBILITY;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,27 @@
|
|||
<?php
|
||||
|
||||
namespace Rector\DowngradePhp71\Tests\Rector\ClassConst\Fixture;
|
||||
|
||||
class SomeClass
|
||||
{
|
||||
const PUBLIC_CONST_A = 1;
|
||||
public const PUBLIC_CONST_B = 2;
|
||||
protected const PROTECTED_CONST = 3;
|
||||
private const PRIVATE_CONST = 4;
|
||||
}
|
||||
|
||||
?>
|
||||
-----
|
||||
<?php
|
||||
|
||||
namespace Rector\DowngradePhp71\Tests\Rector\ClassConst\Fixture;
|
||||
|
||||
class SomeClass
|
||||
{
|
||||
const PUBLIC_CONST_A = 1;
|
||||
const PUBLIC_CONST_B = 2;
|
||||
const PROTECTED_CONST = 3;
|
||||
const PRIVATE_CONST = 4;
|
||||
}
|
||||
|
||||
?>
|
|
@ -83,6 +83,33 @@ final class VisibilityManipulator
|
|||
$this->addVisibilityFlag($node, $visibility);
|
||||
}
|
||||
|
||||
/**
|
||||
* This way "abstract", "static", "final" are kept
|
||||
*
|
||||
* @param ClassMethod|Property|ClassConst $node
|
||||
*/
|
||||
public function removeOriginalVisibilityFromFlags(Node $node): void
|
||||
{
|
||||
$this->ensureIsClassMethodOrProperty($node, __METHOD__);
|
||||
|
||||
// no modifier
|
||||
if ($node->flags === 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
if ($node->isPublic()) {
|
||||
$node->flags -= Class_::MODIFIER_PUBLIC;
|
||||
}
|
||||
|
||||
if ($node->isProtected()) {
|
||||
$node->flags -= Class_::MODIFIER_PROTECTED;
|
||||
}
|
||||
|
||||
if ($node->isPrivate()) {
|
||||
$node->flags -= Class_::MODIFIER_PRIVATE;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Class_|ClassMethod|Property|ClassConst $node
|
||||
*/
|
||||
|
@ -115,33 +142,6 @@ final class VisibilityManipulator
|
|||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* This way "abstract", "static", "final" are kept
|
||||
*
|
||||
* @param ClassMethod|Property|ClassConst $node
|
||||
*/
|
||||
private function removeOriginalVisibilityFromFlags(Node $node): void
|
||||
{
|
||||
$this->ensureIsClassMethodOrProperty($node, __METHOD__);
|
||||
|
||||
// no modifier
|
||||
if ($node->flags === 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
if ($node->isPublic()) {
|
||||
$node->flags -= Class_::MODIFIER_PUBLIC;
|
||||
}
|
||||
|
||||
if ($node->isProtected()) {
|
||||
$node->flags -= Class_::MODIFIER_PROTECTED;
|
||||
}
|
||||
|
||||
if ($node->isPrivate()) {
|
||||
$node->flags -= Class_::MODIFIER_PRIVATE;
|
||||
}
|
||||
}
|
||||
|
||||
private function ensureIsClassMethodOrProperty(Node $node, string $location): void
|
||||
{
|
||||
foreach (self::ALLOWED_NODE_TYPES as $allowedNodeType) {
|
||||
|
|
|
@ -54,6 +54,14 @@ trait VisibilityTrait
|
|||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param ClassMethod|Property|ClassConst $node
|
||||
*/
|
||||
public function removeVisibility(Node $node): void
|
||||
{
|
||||
$this->visibilityManipulator->removeOriginalVisibilityFromFlags($node);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param ClassMethod|Class_ $node
|
||||
*/
|
||||
|
|
|
@ -71,6 +71,11 @@ final class PhpVersionFeature
|
|||
*/
|
||||
public const VOID_TYPE = '7.1';
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
public const BEFORE_CONSTANT_VISIBILITY = '7.0';
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
|
|
Loading…
Reference in New Issue
Block a user