Skip to content

Commit

Permalink
Fix TernaryToNullCoalescingFixer when dealing with object properties
Browse files Browse the repository at this point in the history
  • Loading branch information
HypeMC committed Oct 1, 2020
1 parent 70fe3d7 commit 26b8ff6
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 1 deletion.
1 change: 0 additions & 1 deletion src/Fixer/Operator/TernaryToNullCoalescingFixer.php
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,6 @@ private function hasChangingContent(Tokens $tokens)
static $operatorsPerId = [
T_DEC,
T_INC,
T_STRING,
T_YIELD,
T_YIELD_FROM,
];
Expand Down
48 changes: 48 additions & 0 deletions tests/Fixer/Operator/TernaryToNullCoalescingFixerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,54 @@ public function provideFixCases()
'<?php $x = $a ?? isset($b) ? $b : isset($c) ? $c : "";',
'<?php $x = isset($a) ? $a : isset($b) ? $b : isset($c) ? $c : "";',
],
[
'<?php $x = $obj->a ?? null;',
'<?php $x = isset($obj->a) ? $obj->a : null;',
],
[
'<?php $x = $obj->a->b ?? null;',
'<?php $x = isset($obj->a->b) ? $obj->a->b : null;',
],
[
'<?php $x = $obj->$a ?? null;',
'<?php $x = isset($obj->$a) ? $obj->$a : null;',
],
[
'<?php $x = $obj->a->$b ?? null;',
'<?php $x = isset($obj->a->$b) ? $obj->a->$b : null;',
],
[
'<?php $x = $obj->a[3] ?? null;',
'<?php $x = isset($obj->a[3]) ? $obj->a[3] : null;',
],
[
'<?php $x = $obj->a[\'foo\'] ?? null;',
'<?php $x = isset($obj->a[\'foo\']) ? $obj->a[\'foo\'] : null;',
],
[
'<?php $x = $obj->a[$b] ?? null;',
'<?php $x = isset($obj->a[$b]) ? $obj->a[$b] : null;',
],
[
'<?php $x = $obj->a[$b][\'foo\'] ?? null;',
'<?php $x = isset($obj->a[$b][\'foo\']) ? $obj->a[$b][\'foo\'] : null;',
],
[
'<?php $x = $obj->a[$b[\'foo\']] ?? null;',
'<?php $x = isset($obj->a[$b[\'foo\']]) ? $obj->a[$b[\'foo\']] : null;',
],
[
'<?php $x = $a[$obj->b] ?? null;',
'<?php $x = isset($a[$obj->b]) ? $a[$obj->b] : null;',
],
[
'<?php $x = Foo::A[$b] ?? null;',
'<?php $x = isset(Foo::A[$b]) ? Foo::A[$b] : null;',
],
[
'<?php $x = $a[Foo::B] ?? null;',
'<?php $x = isset($a[Foo::B]) ? $a[Foo::B] : null;',
],
[
'<?php $x = /*a1*//*a2*/ /*b*/ $a /*c*/ ?? /*d*/ isset($b) /*e*/ ? /*f*/ $b /*g*/ : /*h*/ isset($c) /*i*/ ? /*j*/ $c /*k*/ : /*l*/ "";',
'<?php $x = isset($a) /*a1*//*a2*/ ? /*b*/ $a /*c*/ : /*d*/ isset($b) /*e*/ ? /*f*/ $b /*g*/ : /*h*/ isset($c) /*i*/ ? /*j*/ $c /*k*/ : /*l*/ "";',
Expand Down

0 comments on commit 26b8ff6

Please sign in to comment.