@@ -94,24 +94,57 @@ public function refactor(Node $node): ?Node
9494 return null ;
9595 }
9696
97- // start node
98- if (! isset ($ oldTokens [$ node -> getStartTokenPos ()])) {
97+ // "new" must end with arguments, e.g. skip "new Foo" or anonymous class
98+ if (! isset ($ oldTokens [$ new -> getEndTokenPos ()]) || ( string ) $ oldTokens [ $ new -> getEndTokenPos ()] !== ' ) ' ) {
9999 return null ;
100100 }
101101
102- // end of "new" node
103- if (! isset ($ oldTokens [$ new ->getEndTokenPos ()])) {
102+ // all wrapping "(" between the node start and "new"
103+ $ openingPositions = [];
104+ for ($ position = $ node ->getStartTokenPos (); $ position < $ new ->getStartTokenPos (); ++$ position ) {
105+ if (! isset ($ oldTokens [$ position ])) {
106+ return null ;
107+ }
108+
109+ if ($ oldTokens [$ position ]->isIgnorable ()) {
110+ continue ;
111+ }
112+
113+ if ((string ) $ oldTokens [$ position ] !== '( ' ) {
114+ return null ;
115+ }
116+
117+ $ openingPositions [] = $ position ;
118+ }
119+
120+ if ($ openingPositions === []) {
104121 return null ;
105122 }
106123
107- if ((string ) $ oldTokens [$ node ->getStartTokenPos ()] === '( ' && (string ) $ oldTokens [$ new ->getEndTokenPos ()] === ') ' ) {
108- $ oldTokens [$ node ->getStartTokenPos ()]->text = '' ;
109- $ oldTokens [$ new ->getEndTokenPos ()]->text = '' ;
124+ // the matching ")" after "new", not the arguments ")"
125+ $ closingPositions = [];
126+ $ position = $ new ->getEndTokenPos () + 1 ;
127+ while (count ($ closingPositions ) < count ($ openingPositions )) {
128+ if (! isset ($ oldTokens [$ position ])) {
129+ return null ;
130+ }
131+
132+ if (! $ oldTokens [$ position ]->isIgnorable ()) {
133+ if ((string ) $ oldTokens [$ position ] !== ') ' ) {
134+ return null ;
135+ }
136+
137+ $ closingPositions [] = $ position ;
138+ }
139+
140+ ++$ position ;
141+ }
110142
111- return $ node ;
143+ foreach ([...$ openingPositions , ...$ closingPositions ] as $ parenthesisPosition ) {
144+ $ oldTokens [$ parenthesisPosition ]->text = '' ;
112145 }
113146
114- return null ;
147+ return $ node ;
115148 }
116149
117150 public function provideMinPhpVersion (): int
0 commit comments