Skip to content

Commit 635647f

Browse files
committed
[Visibility] Deprecate ChangeConstantVisibilityRector as too niche and risky to visibility across parent
1 parent 56d303c commit 635647f

7 files changed

Lines changed: 16 additions & 230 deletions

File tree

‎rules-tests/Visibility/Rector/ClassConst/ChangeConstantVisibilityRector/ChangeConstantVisibilityRectorTest.php‎

Lines changed: 0 additions & 28 deletions
This file was deleted.

‎rules-tests/Visibility/Rector/ClassConst/ChangeConstantVisibilityRector/Fixture/fixture.php.inc‎

Lines changed: 0 additions & 33 deletions
This file was deleted.

‎rules-tests/Visibility/Rector/ClassConst/ChangeConstantVisibilityRector/Fixture/fixture2.php.inc‎

Lines changed: 0 additions & 21 deletions
This file was deleted.

‎rules-tests/Visibility/Rector/ClassConst/ChangeConstantVisibilityRector/Source/ParentObject.php‎

Lines changed: 0 additions & 12 deletions
This file was deleted.

‎rules-tests/Visibility/Rector/ClassConst/ChangeConstantVisibilityRector/config/configured_rule.php‎

Lines changed: 0 additions & 23 deletions
This file was deleted.

‎rules/Visibility/Rector/ClassConst/ChangeConstantVisibilityRector.php‎

Lines changed: 16 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -7,37 +7,22 @@
77
use PhpParser\Node;
88
use PhpParser\Node\Stmt\Class_;
99
use PhpParser\Node\Stmt\Interface_;
10-
use Rector\Contract\Rector\ConfigurableRectorInterface;
11-
use Rector\Privatization\NodeManipulator\VisibilityManipulator;
10+
use Rector\Configuration\Deprecation\Contract\DeprecatedInterface;
11+
use Rector\Exception\ShouldNotHappenException;
1212
use Rector\Rector\AbstractRector;
13-
use Rector\RuleDoc\CodeSample\ConfiguredCodeSample;
13+
use Rector\RuleDoc\CodeSample\CodeSample;
1414
use Rector\RuleDoc\RuleDefinition;
15-
use Rector\ValueObject\Visibility;
16-
use Rector\Visibility\ValueObject\ChangeConstantVisibility;
17-
use Webmozart\Assert\Assert;
1815

1916
/**
20-
* @see \Rector\Tests\Visibility\Rector\ClassConst\ChangeConstantVisibilityRector\ChangeConstantVisibilityRectorTest
17+
* @deprecated This rule is deprecated, as too niche and never used. Changing a constant visibility against its parent needs extra care to not break the code. Use a custom rule to handle your case instead.
2118
*/
22-
final class ChangeConstantVisibilityRector extends AbstractRector implements ConfigurableRectorInterface
19+
final class ChangeConstantVisibilityRector extends AbstractRector implements DeprecatedInterface
2320
{
24-
/**
25-
* @var ChangeConstantVisibility[]
26-
*/
27-
private array $classConstantVisibilityChanges = [];
28-
29-
public function __construct(
30-
private readonly VisibilityManipulator $visibilityManipulator,
31-
) {
32-
}
33-
3421
public function getRuleDefinition(): RuleDefinition
3522
{
36-
return new RuleDefinition(
37-
'Change visibility of constant from parent class',
38-
[
39-
new ConfiguredCodeSample(
40-
<<<'CODE_SAMPLE'
23+
return new RuleDefinition('Change visibility of constant from parent class', [
24+
new CodeSample(
25+
<<<'CODE_SAMPLE'
4126
class FrameworkClass
4227
{
4328
protected const SOME_CONSTANT = 1;
@@ -48,8 +33,8 @@ class MyClass extends FrameworkClass
4833
public const SOME_CONSTANT = 1;
4934
}
5035
CODE_SAMPLE
51-
,
52-
<<<'CODE_SAMPLE'
36+
,
37+
<<<'CODE_SAMPLE'
5338
class FrameworkClass
5439
{
5540
protected const SOME_CONSTANT = 1;
@@ -60,11 +45,8 @@ class MyClass extends FrameworkClass
6045
protected const SOME_CONSTANT = 1;
6146
}
6247
CODE_SAMPLE
63-
,
64-
[new ChangeConstantVisibility('ParentObject', 'SOME_CONSTANT', Visibility::PROTECTED)]
65-
),
66-
]
67-
);
48+
),
49+
]);
6850
}
6951

7052
/**
@@ -80,54 +62,9 @@ public function getNodeTypes(): array
8062
*/
8163
public function refactor(Node $node): ?Node
8264
{
83-
$hasChanged = false;
84-
85-
$constantNames = [];
86-
foreach ($node->getConstants() as $classConst) {
87-
if ($classConst->consts === []) {
88-
continue;
89-
}
90-
91-
$constantNames[strtolower($classConst->consts[0]->name->toString())] = true;
92-
}
93-
94-
foreach ($this->classConstantVisibilityChanges as $classConstantVisibilityChange) {
95-
// cheap name check before the expensive type resolution below
96-
if (! isset($constantNames[strtolower($classConstantVisibilityChange->getConstant())])) {
97-
continue;
98-
}
99-
100-
if (! $this->isObjectType($node, $classConstantVisibilityChange->getObjectType())) {
101-
continue;
102-
}
103-
104-
foreach ($node->getConstants() as $classConst) {
105-
if (! $this->isName($classConst, $classConstantVisibilityChange->getConstant())) {
106-
continue;
107-
}
108-
109-
$this->visibilityManipulator->changeNodeVisibility(
110-
$classConst,
111-
$classConstantVisibilityChange->getVisibility()
112-
);
113-
$hasChanged = true;
114-
}
115-
}
116-
117-
if ($hasChanged) {
118-
return $node;
119-
}
120-
121-
return null;
122-
}
123-
124-
/**
125-
* @param mixed[] $configuration
126-
*/
127-
public function configure(array $configuration): void
128-
{
129-
Assert::allIsAOf($configuration, ChangeConstantVisibility::class);
130-
131-
$this->classConstantVisibilityChanges = $configuration;
65+
throw new ShouldNotHappenException(sprintf(
66+
'"%s" rule is deprecated, as too niche and risky. Use a custom rule to handle your case instead',
67+
self::class
68+
));
13269
}
13370
}

‎rules/Visibility/ValueObject/ChangeConstantVisibility.php‎

Lines changed: 0 additions & 34 deletions
This file was deleted.

0 commit comments

Comments
 (0)