ExplicitIndirectVariableFixerTest.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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. * @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\LanguageConstruct\ExplicitIndirectVariableFixer>
  22. */
  23. final class ExplicitIndirectVariableFixerTest extends AbstractFixerTestCase
  24. {
  25. /**
  26. * @dataProvider provideFixCases
  27. */
  28. public function testFix(string $expected, ?string $input = null): void
  29. {
  30. $this->doTest($expected, $input);
  31. }
  32. /**
  33. * @return iterable<string, array{0: string, 1?: null|string}>
  34. */
  35. public static function provideFixCases(): iterable
  36. {
  37. yield 'variable variable function call' => [
  38. '<?php echo ${$foo}($bar);',
  39. '<?php echo $$foo($bar);',
  40. ];
  41. yield 'variable variable array fetch' => [
  42. '<?php echo ${$foo}[\'bar\'][\'baz\'];',
  43. '<?php echo $$foo[\'bar\'][\'baz\'];',
  44. ];
  45. yield 'dynamic property access' => [
  46. '<?php echo $foo->{$bar}[\'baz\'];',
  47. '<?php echo $foo->$bar[\'baz\'];',
  48. ];
  49. yield 'dynamic property access with method call' => [
  50. '<?php echo $foo->{$bar}[\'baz\']();',
  51. '<?php echo $foo->$bar[\'baz\']();',
  52. ];
  53. yield 'variable variable with comments between dollar signs' => [
  54. '<?php echo $
  55. /* C1 */
  56. // C2
  57. {$foo}
  58. // C3
  59. ;',
  60. '<?php echo $
  61. /* C1 */
  62. // C2
  63. $foo
  64. // C3
  65. ;',
  66. ];
  67. yield 'dynamic static property access using variable variable' => [
  68. '<?php echo Foo::${$bar};',
  69. '<?php echo Foo::$$bar;',
  70. ];
  71. yield 'dynamic static property access using variable variable with method call' => [
  72. '<?php echo Foo::${$bar}->baz();',
  73. '<?php echo Foo::$$bar->baz();',
  74. ];
  75. }
  76. /**
  77. * @dataProvider provideFix80Cases
  78. *
  79. * @requires PHP 8.0
  80. */
  81. public function testFix80(string $expected, ?string $input): void
  82. {
  83. $this->doTest($expected, $input);
  84. }
  85. /**
  86. * @return iterable<string, array{0: string, 1?: null|string}>
  87. */
  88. public static function provideFix80Cases(): iterable
  89. {
  90. yield 'dynamic property fetch with nullsafe operator' => [
  91. '<?php echo $foo?->{$bar}["baz"];',
  92. '<?php echo $foo?->$bar["baz"];',
  93. ];
  94. yield 'dynamic property fetch with nullsafe operator and method call' => [
  95. '<?php echo $foo?->{$bar}["baz"]();',
  96. '<?php echo $foo?->$bar["baz"]();',
  97. ];
  98. }
  99. /**
  100. * @dataProvider provideFix83Cases
  101. *
  102. * @requires PHP 8.3
  103. */
  104. public function testFix83(string $expected, ?string $input): void
  105. {
  106. $this->doTest($expected, $input);
  107. }
  108. /**
  109. * @return iterable<string, array{0: string, 1?: null|string}>
  110. */
  111. public static function provideFix83Cases(): iterable
  112. {
  113. yield 'dynamic class const fetch with variable variable' => [
  114. '<?php echo Foo::{${$bar}};',
  115. '<?php echo Foo::{$$bar};',
  116. ];
  117. }
  118. }