NoUselessSprintfFixerTest.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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\FunctionNotation;
  13. use PhpCsFixer\Tests\Test\AbstractFixerTestCase;
  14. /**
  15. * @internal
  16. *
  17. * @covers \PhpCsFixer\Fixer\FunctionNotation\NoUselessSprintfFixer
  18. *
  19. * @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\FunctionNotation\NoUselessSprintfFixer>
  20. */
  21. final class NoUselessSprintfFixerTest 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<int|string, array{0: string, 1?: string}>
  32. */
  33. public static function provideFixCases(): iterable
  34. {
  35. yield 'simple' => [
  36. '<?php echo "bar";',
  37. '<?php echo sprintf("bar");',
  38. ];
  39. yield 'simple II' => [
  40. "<?php echo 'bar' ?>",
  41. "<?php echo sprintf('bar') ?>",
  42. ];
  43. yield 'simple III' => [
  44. '<?php echo $bar;',
  45. '<?php echo sprintf($bar);',
  46. ];
  47. yield 'simple IV' => [
  48. '<?php echo 1;',
  49. '<?php echo sprintf(1);',
  50. ];
  51. yield 'minimal' => [
  52. '<?php $foo;',
  53. '<?php sprintf($foo);',
  54. ];
  55. yield 'case insensitive' => [
  56. '<?php echo "bar";',
  57. '<?php echo SPRINTF("bar");',
  58. ];
  59. yield 'nested' => [
  60. '<?php echo /* test */"bar";',
  61. '<?php echo sprintf(sprintf(sprintf(/* test */sprintf(sprintf(sprintf("bar"))))));',
  62. ];
  63. yield [
  64. '<?php namespace Foo {
  65. function sprintf($foo) {
  66. echo $foo;
  67. }
  68. }',
  69. ];
  70. yield [
  71. '<?php namespace Foo;
  72. function sprintf($foo) {
  73. echo $foo;
  74. }
  75. ',
  76. ];
  77. yield [
  78. '<?php
  79. echo \Foo\sprintf("abc");
  80. echo $bar->sprintf($foo);
  81. echo Bar::sprintf($foo);
  82. echo sprint(...$foo);
  83. echo sprint("%d", 1);
  84. echo sprint("%d%d%d", 1, 2, 3);
  85. echo sprint();
  86. ',
  87. ];
  88. yield [
  89. '<?php echo sprint[2]("foo");',
  90. ];
  91. yield 'trailing comma' => [
  92. '<?php echo "bar";',
  93. '<?php echo sprintf("bar",);',
  94. ];
  95. }
  96. /**
  97. * @dataProvider provideFixPre80Cases
  98. *
  99. * @requires PHP <8.0
  100. */
  101. public function testFixPre80(string $expected, ?string $input = null): void
  102. {
  103. $this->doTest($expected, $input);
  104. }
  105. /**
  106. * @return iterable<array{0: string, 1?: string}>
  107. */
  108. public static function provideFixPre80Cases(): iterable
  109. {
  110. yield [
  111. '<?php echo "bar";',
  112. '<?php echo \ sprintf("bar");',
  113. ];
  114. yield [
  115. '<?php echo A /* 1 */ \ B \ sprintf("bar");',
  116. ];
  117. }
  118. }