ExplicitIndirectVariableFixerTest.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. <?php
  2. declare(strict_types=1);
  3. /*
  4. * This file is part of PHP CS Fixer.
  5. *
  6. * (c) Fabien Potencier <fabien@symfony.com>
  7. * Dariusz Rumiński <dariusz.ruminski@gmail.com>
  8. *
  9. * This source file is subject to the MIT license that is bundled
  10. * with this source code in the file LICENSE.
  11. */
  12. namespace PhpCsFixer\Tests\Fixer\LanguageConstruct;
  13. use PhpCsFixer\Tests\Test\AbstractFixerTestCase;
  14. /**
  15. * @author Filippo Tessarotto <zoeslam@gmail.com>
  16. *
  17. * @internal
  18. *
  19. * @covers \PhpCsFixer\Fixer\LanguageConstruct\ExplicitIndirectVariableFixer
  20. */
  21. final class ExplicitIndirectVariableFixerTest extends AbstractFixerTestCase
  22. {
  23. /**
  24. * @dataProvider provideFixCases
  25. */
  26. public function testFix(string $expected, ?string $input = null): void
  27. {
  28. $this->doTest($expected, $input);
  29. }
  30. /**
  31. * @return iterable<string, array{0: string, 1?: null|string}>
  32. */
  33. public static function provideFixCases(): iterable
  34. {
  35. yield 'variable variable function call' => [
  36. '<?php echo ${$foo}($bar);',
  37. '<?php echo $$foo($bar);',
  38. ];
  39. yield 'variable variable array fetch' => [
  40. '<?php echo ${$foo}[\'bar\'][\'baz\'];',
  41. '<?php echo $$foo[\'bar\'][\'baz\'];',
  42. ];
  43. yield 'dynamic property access' => [
  44. '<?php echo $foo->{$bar}[\'baz\'];',
  45. '<?php echo $foo->$bar[\'baz\'];',
  46. ];
  47. yield 'dynamic property access with method call' => [
  48. '<?php echo $foo->{$bar}[\'baz\']();',
  49. '<?php echo $foo->$bar[\'baz\']();',
  50. ];
  51. yield 'variable variable with comments between dollar signs' => [
  52. '<?php echo $
  53. /* C1 */
  54. // C2
  55. {$foo}
  56. // C3
  57. ;',
  58. '<?php echo $
  59. /* C1 */
  60. // C2
  61. $foo
  62. // C3
  63. ;',
  64. ];
  65. yield 'dynamic static property access using variable variable' => [
  66. '<?php echo Foo::${$bar};',
  67. '<?php echo Foo::$$bar;',
  68. ];
  69. yield 'dynamic static property access using variable variable with method call' => [
  70. '<?php echo Foo::${$bar}->baz();',
  71. '<?php echo Foo::$$bar->baz();',
  72. ];
  73. }
  74. /**
  75. * @dataProvider provideFix80Cases
  76. *
  77. * @requires PHP 8.0
  78. */
  79. public function testFix80(string $expected, ?string $input): void
  80. {
  81. $this->doTest($expected, $input);
  82. }
  83. /**
  84. * @return iterable<string, array{0: string, 1?: null|string}>
  85. */
  86. public static function provideFix80Cases(): iterable
  87. {
  88. yield 'dynamic property fetch with nullsafe operator' => [
  89. '<?php echo $foo?->{$bar}["baz"];',
  90. '<?php echo $foo?->$bar["baz"];',
  91. ];
  92. yield 'dynamic property fetch with nullsafe operator and method call' => [
  93. '<?php echo $foo?->{$bar}["baz"]();',
  94. '<?php echo $foo?->$bar["baz"]();',
  95. ];
  96. }
  97. /**
  98. * @dataProvider provideFix83Cases
  99. *
  100. * @requires PHP 8.3
  101. */
  102. public function testFix83(string $expected, ?string $input): void
  103. {
  104. $this->doTest($expected, $input);
  105. }
  106. /**
  107. * @return iterable<string, array{0: string, 1?: null|string}>
  108. */
  109. public static function provideFix83Cases(): iterable
  110. {
  111. yield 'dynamic class const fetch with variable variable' => [
  112. '<?php echo Foo::{${$bar}};',
  113. '<?php echo Foo::{$$bar};',
  114. ];
  115. }
  116. }