ExplicitIndirectVariableFixerTest.php 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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. public static function provideFixCases(): iterable
  31. {
  32. yield [
  33. '<?php echo ${$foo}($bar);',
  34. '<?php echo $$foo($bar);',
  35. ];
  36. yield [
  37. '<?php echo ${$foo}[\'bar\'][\'baz\'];',
  38. '<?php echo $$foo[\'bar\'][\'baz\'];',
  39. ];
  40. yield [
  41. '<?php echo $foo->{$bar}[\'baz\'];',
  42. '<?php echo $foo->$bar[\'baz\'];',
  43. ];
  44. yield [
  45. '<?php echo $foo->{$bar}[\'baz\']();',
  46. '<?php echo $foo->$bar[\'baz\']();',
  47. ];
  48. yield [
  49. '<?php echo $
  50. /* C1 */
  51. // C2
  52. {$foo}
  53. // C3
  54. ;',
  55. '<?php echo $
  56. /* C1 */
  57. // C2
  58. $foo
  59. // C3
  60. ;',
  61. ];
  62. }
  63. /**
  64. * @param mixed $expected
  65. * @param mixed $input
  66. *
  67. * @dataProvider provideFix80Cases
  68. *
  69. * @requires PHP 8.0
  70. */
  71. public function testFix80($expected, $input): void
  72. {
  73. $this->doTest($expected, $input);
  74. }
  75. public static function provideFix80Cases(): iterable
  76. {
  77. yield [
  78. '<?php echo $foo?->{$bar}["baz"];',
  79. '<?php echo $foo?->$bar["baz"];',
  80. ];
  81. yield [
  82. '<?php echo $foo?->{$bar}["baz"]();',
  83. '<?php echo $foo?->$bar["baz"]();',
  84. ];
  85. }
  86. }