Skip to content

Commit b703cc4

Browse files
committed
NewWithBracesFixer - Fix object operator and curly brace open cases
1 parent 74666dc commit b703cc4

File tree

2 files changed

+25
-3
lines changed

2 files changed

+25
-3
lines changed

src/Fixer/Operator/NewWithBracesFixer.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -120,8 +120,8 @@ protected function applyFix(\SplFileInfo $file, Tokens $tokens)
120120
}
121121

122122
// entrance into array index syntax - need to look for exit
123-
while ($nextToken->equals('[')) {
124-
$nextIndex = $tokens->findBlockEnd(Tokens::BLOCK_TYPE_INDEX_SQUARE_BRACE, $nextIndex) + 1;
123+
while ($nextToken->equals('[') || $nextToken->isGivenKind(CT::T_ARRAY_INDEX_CURLY_BRACE_OPEN)) {
124+
$nextIndex = $tokens->findBlockEnd($tokens->detectBlockType($nextToken)['type'], $nextIndex) + 1;
125125
$nextToken = $tokens[$nextIndex];
126126
}
127127

@@ -132,7 +132,7 @@ protected function applyFix(\SplFileInfo $file, Tokens $tokens)
132132
}
133133

134134
// new statement with () - nothing to do
135-
if ($nextToken->equals('(')) {
135+
if ($nextToken->equals('(') || $nextToken->isGivenKind(T_OBJECT_OPERATOR)) {
136136
continue;
137137
}
138138

tests/Fixer/Operator/NewWithBracesFixerTest.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,12 @@ public function provideFixCases()
7474
'<?php $foo = new $foo();',
7575
'<?php $foo = new $foo;',
7676
],
77+
[
78+
'<?php
79+
$bar1 = new $foo[0]->bar();
80+
$bar2 = new $foo[0][1]->bar();
81+
',
82+
],
7783
[
7884
'<?php $xyz = new X(new Y(new Z()));',
7985
'<?php $xyz = new X(new Y(new Z));',
@@ -106,6 +112,22 @@ public function provideFixCases()
106112
'<?php $a = new $b[$c]();',
107113
'<?php $a = new $b[$c];',
108114
],
115+
[
116+
'<?php $a = new $b[$c][0]();',
117+
'<?php $a = new $b[$c][0];',
118+
],
119+
[
120+
'<?php $a = new $b{$c}();',
121+
'<?php $a = new $b{$c};',
122+
],
123+
[
124+
'<?php $a = new $b{$c}{0}{1}() ?>',
125+
'<?php $a = new $b{$c}{0}{1} ?>',
126+
],
127+
[
128+
'<?php $a = new $b{$c}[1]{0}[2]();',
129+
'<?php $a = new $b{$c}[1]{0}[2];',
130+
],
109131
[
110132
'<?php $a = new $b[$c[$d ? foo() : bar("bar[...]") - 1]]();',
111133
'<?php $a = new $b[$c[$d ? foo() : bar("bar[...]") - 1]];',

0 commit comments

Comments
 (0)