File tree Expand file tree Collapse file tree 3 files changed +110
-1
lines changed
rules-tests/Php54/Rector/Array_/LongArrayToShortArrayRector/Fixture
rules/Php54/Rector/Array_ Expand file tree Collapse file tree 3 files changed +110
-1
lines changed Original file line number Diff line number Diff line change 1+ <?php
2+
3+ namespace Rector \Tests \Php54 \Rector \Array_ \LongArrayToShortArrayTest \Fixture ;
4+
5+ final class MultiNested
6+ {
7+ public function run ()
8+ {
9+ return array (
10+ array (
11+ array (
12+ 'abc ' => true ,
13+ ),
14+ ),
15+ );
16+ }
17+ }
18+
19+ ?>
20+ -----
21+ <?php
22+
23+ namespace Rector \Tests \Php54 \Rector \Array_ \LongArrayToShortArrayTest \Fixture ;
24+
25+ final class MultiNested
26+ {
27+ public function run ()
28+ {
29+ return [
30+ [
31+ [
32+ 'abc ' => true ,
33+ ],
34+ ],
35+ ];
36+ }
37+ }
38+
39+ ?>
Original file line number Diff line number Diff line change 1+ <?php
2+
3+ namespace Rector \Tests \Php54 \Rector \Array_ \LongArrayToShortArrayTest \Fixture ;
4+
5+ final class SpacedArray
6+ {
7+ public function run ()
8+ {
9+ return array (
10+ array (
11+ array (
12+ 'abc ' => true ,
13+ ),
14+ ),
15+ );
16+ }
17+ }
18+
19+ ?>
20+ -----
21+ <?php
22+
23+ namespace Rector \Tests \Php54 \Rector \Array_ \LongArrayToShortArrayTest \Fixture ;
24+
25+ final class SpacedArray
26+ {
27+ public function run ()
28+ {
29+ return [
30+ [
31+ [
32+ 'abc ' => true ,
33+ ],
34+ ],
35+ ];
36+ }
37+ }
38+
39+ ?>
Original file line number Diff line number Diff line change @@ -76,8 +76,39 @@ public function refactor(Node $node): ?Node
7676 return null ;
7777 }
7878
79- $ node ->setAttribute (AttributeKey::ORIGINAL_NODE , null );
8079 $ node ->setAttribute (AttributeKey::KIND , Array_::KIND_SHORT );
80+
81+ $ tokens = $ this ->file ->getOldTokens ();
82+
83+ $ startTokenPos = $ node ->getStartTokenPos ();
84+ $ endTokenPos = $ node ->getEndTokenPos ();
85+
86+ if (! isset ($ tokens [$ startTokenPos ], $ tokens [$ endTokenPos ])) {
87+ return null ;
88+ }
89+
90+ // replace array opening
91+ $ tokens [$ startTokenPos ]->text = '' ;
92+
93+ $ iteration = 1 ;
94+ while (isset ($ tokens [$ startTokenPos + $ iteration ])) {
95+ if (trim ($ tokens [$ startTokenPos + $ iteration ]->text ) === '' ) {
96+ ++$ iteration ;
97+ continue ;
98+ }
99+
100+ if (trim ($ tokens [$ startTokenPos + $ iteration ]->text ) !== '( ' ) {
101+ break ;
102+ }
103+
104+ // replace ( parentheses opening
105+ $ tokens [$ startTokenPos + $ iteration ]->text = '[ ' ;
106+ // replace ) parentheses closing
107+ $ tokens [$ endTokenPos ]->text = '] ' ;
108+
109+ break ;
110+ }
111+
81112 return $ node ;
82113 }
83114}
You can’t perform that action at this time.
0 commit comments