44
55namespace Rector \CodingStyle \Rector \FuncCall ;
66
7+ use PhpParser \Node \Expr \StaticCall ;
8+ use PhpParser \Node \Arg ;
9+ use PhpParser \Node \Scalar \String_ ;
10+ use PhpParser \Node \Expr \FuncCall ;
11+ use PhpParser \Node \VariadicPlaceholder ;
12+ use PhpParser \Node \Expr \Array_ ;
13+ use PhpParser \Node \Expr \Variable ;
14+ use PhpParser \Node \Expr \MethodCall ;
15+ use PhpParser \Node \Name \FullyQualified ;
16+ use PhpParser \Node \Expr \ClassConstFetch ;
17+ use PhpParser \Node \Expr ;
18+ use PhpParser \Node \Name ;
19+ use PhpParser \Node \Identifier ;
720use PhpParser \Node ;
821use Rector \Rector \AbstractRector ;
922use Rector \ValueObject \PhpVersionFeature ;
@@ -26,9 +39,9 @@ public function getRuleDefinition(): RuleDefinition
2639 'Change `Closure::fromCallable()` to first class callable syntax ' ,
2740 [
2841 new CodeSample ('Closure::fromCallable([$obj, \'method \']); ' , '$obj->method(...); ' ),
29- new CodeSample (' Closure::fromCallable( \ 'trim \ '); ' , 'trim(...); ' ),
42+ new CodeSample (" Closure::fromCallable('trim'); " , 'trim(...); ' ),
3043 new CodeSample (
31- ' Closure::fromCallable([ \ 'SomeClass \ ', \ 'staticMethod \ ']); ' ,
44+ " Closure::fromCallable(['SomeClass', 'staticMethod']); " ,
3245 'SomeClass::staticMethod(...); '
3346 ),
3447 ]
@@ -41,11 +54,11 @@ public function getRuleDefinition(): RuleDefinition
4154 */
4255 public function getNodeTypes (): array
4356 {
44- return [Node \ Expr \ StaticCall::class];
57+ return [StaticCall::class];
4558 }
4659
4760 /**
48- * @param Node\Expr\ StaticCall $node
61+ * @param StaticCall $node
4962 */
5063 public function refactor (Node $ node ): ?Node
5164 {
@@ -54,52 +67,52 @@ public function refactor(Node $node): ?Node
5467 }
5568
5669 $ arg = $ node ->args [0 ];
57- if (! $ arg instanceof Node \ Arg) {
70+ if (! $ arg instanceof Arg) {
5871 return null ;
5972 }
6073
61- if ($ arg ->value instanceof Node \ Scalar \ String_) {
62- return new Node \ Expr \ FuncCall (
74+ if ($ arg ->value instanceof String_) {
75+ return new FuncCall (
6376 $ this ->toFullyQualified ($ arg ->value ->value ),
64- [new Node \ VariadicPlaceholder ()],
77+ [new VariadicPlaceholder ()],
6578 );
6679 }
6780
68- if ($ arg ->value instanceof Node \ Expr \ Array_) {
81+ if ($ arg ->value instanceof Array_) {
6982 if (
7083 ! array_key_exists (0 , $ arg ->value ->items )
7184 || ! array_key_exists (1 , $ arg ->value ->items )
72- || ! $ arg ->value ->items [1 ]->value instanceof Node \ Scalar \ String_
85+ || ! $ arg ->value ->items [1 ]->value instanceof String_
7386 ) {
7487 return null ;
7588 }
7689
77- if ($ arg ->value ->items [0 ]->value instanceof Node \ Expr \ Variable) {
78- return new Node \ Expr \ MethodCall (
90+ if ($ arg ->value ->items [0 ]->value instanceof Variable) {
91+ return new MethodCall (
7992 $ arg ->value ->items [0 ]->value ,
8093 $ arg ->value ->items [1 ]->value ->value ,
81- [new Node \ VariadicPlaceholder ()],
94+ [new VariadicPlaceholder ()],
8295 );
8396 }
8497
85- if ($ arg ->value ->items [0 ]->value instanceof Node \ Scalar \ String_) {
86- $ classNode = new Node \ Name \ FullyQualified ($ arg ->value ->items [0 ]->value ->value );
87- } elseif ($ arg ->value ->items [0 ]->value instanceof Node \ Expr \ ClassConstFetch) {
88- if ($ arg ->value ->items [0 ]->value ->class instanceof Node \ Expr) {
98+ if ($ arg ->value ->items [0 ]->value instanceof String_) {
99+ $ classNode = new FullyQualified ($ arg ->value ->items [0 ]->value ->value );
100+ } elseif ($ arg ->value ->items [0 ]->value instanceof ClassConstFetch) {
101+ if ($ arg ->value ->items [0 ]->value ->class instanceof Expr) {
89102 return null ;
90103 }
91104
92- $ classNode = new Node \ Name \ FullyQualified ($ arg ->value ->items [0 ]->value ->class ->name );
93- } elseif ($ arg ->value ->items [0 ]->value instanceof Node \ Name \ FullyQualified) {
94- $ classNode = new Node \ Name \ FullyQualified ($ arg ->value ->items [0 ]->value ->name );
105+ $ classNode = new FullyQualified ($ arg ->value ->items [0 ]->value ->class ->name );
106+ } elseif ($ arg ->value ->items [0 ]->value instanceof FullyQualified) {
107+ $ classNode = new FullyQualified ($ arg ->value ->items [0 ]->value ->name );
95108 } else {
96109 return null ;
97110 }
98111
99- return new Node \ Expr \ StaticCall (
112+ return new StaticCall (
100113 $ classNode ,
101114 $ arg ->value ->items [1 ]->value ->value ,
102- [new Node \ VariadicPlaceholder ()],
115+ [new VariadicPlaceholder ()],
103116 );
104117 }
105118
@@ -111,37 +124,33 @@ public function provideMinPhpVersion(): int
111124 return PhpVersionFeature::FIRST_CLASS_CALLABLE_SYNTAX ;
112125 }
113126
114- public function shouldSkip (Node \ Expr \ StaticCall $ node ): bool
127+ public function shouldSkip (StaticCall $ staticCall ): bool
115128 {
116- if (! $ node ->class instanceof Node \ Name) {
129+ if (! $ staticCall ->class instanceof Name) {
117130 return true ;
118131 }
119132
120- if (! $ this ->isName ($ node ->class , 'Closure ' )) {
133+ if (! $ this ->isName ($ staticCall ->class , 'Closure ' )) {
121134 return true ;
122135 }
123136
124- if (! $ node ->name instanceof Node \ Identifier || $ node ->name ->name !== 'fromCallable ' ) {
137+ if (! $ staticCall ->name instanceof Identifier || $ staticCall ->name ->name !== 'fromCallable ' ) {
125138 return true ;
126139 }
127140
128- if ($ node ->isFirstClassCallable ()) {
141+ if ($ staticCall ->isFirstClassCallable ()) {
129142 return true ;
130143 }
131144
132- $ args = $ node ->getArgs ();
133- if (count ($ args ) !== 1 ) {
134- return true ;
135- }
136-
137- return false ;
145+ $ args = $ staticCall ->getArgs ();
146+ return count ($ args ) !== 1 ;
138147 }
139148
140- public function toFullyQualified (string $ functionName ): Node \ Name \ FullyQualified
149+ public function toFullyQualified (string $ functionName ): FullyQualified
141150 {
142151 // in case there's already a \ prefix, remove it
143152 $ functionName = ltrim ($ functionName , '\\' );
144153
145- return new Node \ Name \ FullyQualified ($ functionName );
154+ return new FullyQualified ($ functionName );
146155 }
147156}
0 commit comments