77use PhpParser \Node ;
88use PhpParser \Node \Stmt \Class_ ;
99use 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 ;
1212use Rector \Rector \AbstractRector ;
13- use Rector \RuleDoc \CodeSample \ConfiguredCodeSample ;
13+ use Rector \RuleDoc \CodeSample \CodeSample ;
1414use 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'
4126class FrameworkClass
4227{
4328 protected const SOME_CONSTANT = 1;
@@ -48,8 +33,8 @@ class MyClass extends FrameworkClass
4833 public const SOME_CONSTANT = 1;
4934}
5035CODE_SAMPLE
51- ,
52- <<<'CODE_SAMPLE'
36+ ,
37+ <<<'CODE_SAMPLE'
5338class FrameworkClass
5439{
5540 protected const SOME_CONSTANT = 1;
@@ -60,11 +45,8 @@ class MyClass extends FrameworkClass
6045 protected const SOME_CONSTANT = 1;
6146}
6247CODE_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}
0 commit comments