ExplicitIndirectVariableFixerTest.php 2.2 KB

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