NoUselessSprintfFixerTest.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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. final class NoUselessSprintfFixerTest extends AbstractFixerTestCase
  20. {
  21. /**
  22. * @dataProvider provideFixCases
  23. */
  24. public function testFix(string $expected, ?string $input = null): void
  25. {
  26. $this->doTest($expected, $input);
  27. }
  28. public function provideFixCases(): \Generator
  29. {
  30. yield 'simple' => [
  31. '<?php echo "bar";',
  32. '<?php echo sprintf("bar");',
  33. ];
  34. yield 'simple II' => [
  35. "<?php echo 'bar' ?>",
  36. "<?php echo sprintf('bar') ?>",
  37. ];
  38. yield 'simple III' => [
  39. '<?php echo $bar;',
  40. '<?php echo sprintf($bar);',
  41. ];
  42. yield 'simple IV' => [
  43. '<?php echo 1;',
  44. '<?php echo sprintf(1);',
  45. ];
  46. yield 'minimal' => [
  47. '<?php $foo;',
  48. '<?php sprintf($foo);',
  49. ];
  50. yield 'case insensitive' => [
  51. '<?php echo "bar";',
  52. '<?php echo SPRINTF("bar");',
  53. ];
  54. yield 'nested' => [
  55. '<?php echo /* test */"bar";',
  56. '<?php echo sprintf(sprintf(sprintf(/* test */sprintf(sprintf(sprintf("bar"))))));',
  57. ];
  58. yield [
  59. '<?php namespace Foo {
  60. function sprintf($foo) {
  61. echo $foo;
  62. }
  63. }',
  64. ];
  65. yield [
  66. '<?php namespace Foo;
  67. function sprintf($foo) {
  68. echo $foo;
  69. }
  70. ',
  71. ];
  72. yield [
  73. '<?php
  74. echo \Foo\sprintf("abc");
  75. echo $bar->sprintf($foo);
  76. echo Bar::sprintf($foo);
  77. echo sprint(...$foo);
  78. echo sprint("%d", 1);
  79. echo sprint("%d%d%d", 1, 2, 3);
  80. echo sprint();
  81. ',
  82. ];
  83. }
  84. /**
  85. * @dataProvider provideFix70Cases
  86. * @requires PHP 7.0
  87. */
  88. public function testFix70(string $expected, ?string $input = null): void
  89. {
  90. $this->doTest($expected, $input);
  91. }
  92. public function provideFix70Cases(): \Generator
  93. {
  94. yield [
  95. '<?php echo sprint[2]("foo");',
  96. ];
  97. }
  98. /**
  99. * @dataProvider provideFix73Cases
  100. * @requires PHP 7.3
  101. */
  102. public function testFix73(string $expected, ?string $input = null): void
  103. {
  104. $this->doTest($expected, $input);
  105. }
  106. public function provideFix73Cases(): \Generator
  107. {
  108. yield 'trailing comma' => [
  109. '<?php echo "bar";',
  110. '<?php echo sprintf("bar",);',
  111. ];
  112. }
  113. /**
  114. * @dataProvider provideFixPre80Cases
  115. * @requires PHP <8.0
  116. */
  117. public function testFixPre80(string $expected, string $input = null): void
  118. {
  119. $this->doTest($expected, $input);
  120. }
  121. public function provideFixPre80Cases(): \Generator
  122. {
  123. yield [
  124. '<?php echo "bar";',
  125. '<?php echo \ sprintf("bar");',
  126. ];
  127. yield [
  128. '<?php echo A /* 1 */ \ B \ sprintf("bar");',
  129. ];
  130. }
  131. }