55namespace Rector \Php55 \Rector \String_ ;
66
77use PhpParser \Node ;
8- use PhpParser \Node \Expr \BinaryOp \Concat ;
98use PhpParser \Node \Expr \ClassConstFetch ;
10- use PhpParser \Node \Expr \FuncCall ;
119use PhpParser \Node \Name \FullyQualified ;
1210use PhpParser \Node \Scalar \String_ ;
13- use PhpParser \NodeVisitor ;
1411use PHPStan \Reflection \ReflectionProvider ;
1512use Rector \Contract \Rector \ConfigurableRectorInterface ;
13+ use Rector \NodeTypeResolver \Node \AttributeKey ;
1614use Rector \Rector \AbstractRector ;
1715use Rector \ValueObject \PhpVersionFeature ;
1816use Rector \VersionBonding \Contract \MinPhpVersionInterface ;
2624final class StringClassNameToClassConstantRector extends AbstractRector implements MinPhpVersionInterface, ConfigurableRectorInterface
2725{
2826 /**
27+ * @deprecated since 2.2.12. Default behavior now.
2928 * @var string
3029 */
3130 public const SHOULD_KEEP_PRE_SLASH = 'should_keep_pre_slash ' ;
@@ -35,8 +34,6 @@ final class StringClassNameToClassConstantRector extends AbstractRector implemen
3534 */
3635 private array $ classesToSkip = [];
3736
38- private bool $ shouldKeepPreslash = false ;
39-
4037 public function __construct (
4138 private readonly ReflectionProvider $ reflectionProvider ,
4239 ) {
@@ -74,11 +71,7 @@ public function run()
7471}
7572CODE_SAMPLE
7673 ,
77- [
78- 'ClassName ' ,
79- 'AnotherClassName ' ,
80- self ::SHOULD_KEEP_PRE_SLASH => false ,
81- ],
74+ ['ClassName ' , 'AnotherClassName ' ],
8275 ),
8376 ]);
8477 }
@@ -88,21 +81,15 @@ public function run()
8881 */
8982 public function getNodeTypes (): array
9083 {
91- return [String_::class, FuncCall::class ];
84+ return [String_::class];
9285 }
9386
9487 /**
95- * @param String_|FuncCall $node
96- * @return Concat|ClassConstFetch|null|NodeVisitor::DONT_TRAVERSE_CHILDREN
88+ * @param String_ $node
9789 */
98- public function refactor (Node $ node ): Concat | ClassConstFetch |null | int
90+ public function refactor (Node $ node ): ClassConstFetch |null
9991 {
100- // keep allowed string as condition
101- if ($ node instanceof FuncCall) {
102- if ($ this ->isName ($ node , 'is_a ' )) {
103- return NodeVisitor::DONT_TRAVERSE_CHILDREN ;
104- }
105-
92+ if ($ this ->shouldSkipIsA ($ node )) {
10693 return null ;
10794 }
10895
@@ -119,14 +106,6 @@ public function refactor(Node $node): Concat|ClassConstFetch|null|int
119106 }
120107
121108 $ fullyQualified = new FullyQualified ($ classLikeName );
122- if ($ this ->shouldKeepPreslash && $ classLikeName !== $ node ->value ) {
123- $ preSlashCount = strlen ($ node ->value ) - strlen ($ classLikeName );
124- $ preSlash = str_repeat ('\\' , $ preSlashCount );
125- $ string = new String_ ($ preSlash );
126-
127- return new Concat ($ string , new ClassConstFetch ($ fullyQualified , 'class ' ));
128- }
129-
130109 return new ClassConstFetch ($ fullyQualified , 'class ' );
131110 }
132111
@@ -135,13 +114,6 @@ public function refactor(Node $node): Concat|ClassConstFetch|null|int
135114 */
136115 public function configure (array $ configuration ): void
137116 {
138- if (isset ($ configuration [self ::SHOULD_KEEP_PRE_SLASH ]) && is_bool (
139- $ configuration [self ::SHOULD_KEEP_PRE_SLASH ]
140- )) {
141- $ this ->shouldKeepPreslash = $ configuration [self ::SHOULD_KEEP_PRE_SLASH ];
142- unset($ configuration [self ::SHOULD_KEEP_PRE_SLASH ]);
143- }
144-
145117 Assert::allString ($ configuration );
146118
147119 $ this ->classesToSkip = $ configuration ;
@@ -184,4 +156,15 @@ private function shouldSkip(string $classLikeName): bool
184156
185157 return false ;
186158 }
159+
160+ private function shouldSkipIsA (String_ $ string ): bool
161+ {
162+ if (! $ string ->getAttribute (AttributeKey::IS_ARG_VALUE , false )) {
163+ return false ;
164+ }
165+
166+ $ funcCallName = $ string ->getAttribute (AttributeKey::FROM_FUNC_CALL_NAME );
167+
168+ return $ funcCallName === 'is_a ' ;
169+ }
187170}
0 commit comments