ErrorSuppressionFixerTest.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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\Fixer\LanguageConstruct\ErrorSuppressionFixer;
  14. use PhpCsFixer\Tests\Test\AbstractFixerTestCase;
  15. /**
  16. * @author Jules Pietri <jules@heahprod.com>
  17. * @author Kuba Werłos <werlos@gmail.com>
  18. *
  19. * @internal
  20. *
  21. * @covers \PhpCsFixer\Fixer\LanguageConstruct\ErrorSuppressionFixer
  22. *
  23. * @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\LanguageConstruct\ErrorSuppressionFixer>
  24. *
  25. * @phpstan-import-type _AutogeneratedInputConfiguration from \PhpCsFixer\Fixer\LanguageConstruct\ErrorSuppressionFixer
  26. */
  27. final class ErrorSuppressionFixerTest extends AbstractFixerTestCase
  28. {
  29. /**
  30. * @param _AutogeneratedInputConfiguration $config
  31. *
  32. * @dataProvider provideFixCases
  33. */
  34. public function testFix(string $expected, ?string $input = null, array $config = []): void
  35. {
  36. $this->fixer->configure($config);
  37. $this->doTest($expected, $input);
  38. }
  39. public static function provideFixCases(): iterable
  40. {
  41. yield [
  42. '<?php trigger_error("This is not a deprecation warning."); @f(); ?>',
  43. ];
  44. yield [
  45. '<?php trigger_error("This is not a deprecation warning.", E_USER_WARNING); ?>',
  46. ];
  47. yield [
  48. '<?php A\B\trigger_error("This is not a deprecation warning.", E_USER_DEPRECATED); ?>',
  49. ];
  50. yield [
  51. '<?php @trigger_error("This is a deprecation warning.", E_USER_DEPRECATED); ?>',
  52. '<?php trigger_error("This is a deprecation warning.", E_USER_DEPRECATED); ?>',
  53. ];
  54. yield [
  55. '<?php trigger_error("This is a deprecation warning.", E_USER_DEPRECATED); ?>',
  56. null,
  57. [ErrorSuppressionFixer::OPTION_MUTE_DEPRECATION_ERROR => false],
  58. ];
  59. yield [
  60. '<?php @\trigger_error("This is a deprecation warning.", E_USER_DEPRECATED); ?>',
  61. '<?php \trigger_error("This is a deprecation warning.", E_USER_DEPRECATED); ?>',
  62. ];
  63. yield [
  64. '<?php echo "test";@trigger_error("This is a deprecation warning.", E_USER_DEPRECATED); ?>',
  65. '<?php echo "test";trigger_error("This is a deprecation warning.", E_USER_DEPRECATED); ?>',
  66. ];
  67. yield [
  68. '<?php //
  69. @Trigger_Error/**/("This is a deprecation warning.", E_USER_DEPRECATED/***/); ?>',
  70. '<?php //
  71. Trigger_Error/**/("This is a deprecation warning.", E_USER_DEPRECATED/***/); ?>',
  72. ];
  73. yield [
  74. '<?php new trigger_error("This is not a deprecation warning.", E_USER_DEPRECATED); ?>',
  75. ];
  76. yield [
  77. '<?php new \trigger_error("This is not a deprecation warning.", E_USER_DEPRECATED); ?>',
  78. ];
  79. yield [
  80. '<?php $foo->trigger_error("This is not a deprecation warning.", E_USER_DEPRECATED); ?>',
  81. ];
  82. yield [
  83. '<?php Foo::trigger_error("This is not a deprecation warning.", E_USER_DEPRECATED); ?>',
  84. ];
  85. yield [
  86. '<?php @trigger_error("This is a deprecation warning.", E_USER_DEPRECATED); mkdir("dir"); ?>',
  87. '<?php trigger_error("This is a deprecation warning.", E_USER_DEPRECATED); @mkdir("dir"); ?>',
  88. [ErrorSuppressionFixer::OPTION_MUTE_DEPRECATION_ERROR => true, ErrorSuppressionFixer::OPTION_NOISE_REMAINING_USAGES => true],
  89. ];
  90. yield [
  91. '<?php $foo->isBar(); ?>',
  92. '<?php @$foo->isBar(); ?>',
  93. [ErrorSuppressionFixer::OPTION_NOISE_REMAINING_USAGES => true],
  94. ];
  95. yield [
  96. '<?php Foo::isBar(); ?>',
  97. '<?php @Foo::isBar(); ?>',
  98. [ErrorSuppressionFixer::OPTION_NOISE_REMAINING_USAGES => true],
  99. ];
  100. yield [
  101. '<?php @trigger_error("This is a deprecation warning.", E_USER_DEPRECATED); @mkdir("dir"); ?>',
  102. '<?php trigger_error("This is a deprecation warning.", E_USER_DEPRECATED); @mkdir("dir"); ?>',
  103. [ErrorSuppressionFixer::OPTION_MUTE_DEPRECATION_ERROR => true, ErrorSuppressionFixer::OPTION_NOISE_REMAINING_USAGES => true, ErrorSuppressionFixer::OPTION_NOISE_REMAINING_USAGES_EXCLUDE => ['mkdir']],
  104. ];
  105. yield [
  106. '<?php @trigger_error("This is a deprecation warning.", E_USER_DEPRECATED); @mkdir("dir"); unlink($path); ?>',
  107. '<?php trigger_error("This is a deprecation warning.", E_USER_DEPRECATED); @mkdir("dir"); @unlink($path); ?>',
  108. [ErrorSuppressionFixer::OPTION_MUTE_DEPRECATION_ERROR => true, ErrorSuppressionFixer::OPTION_NOISE_REMAINING_USAGES => true, ErrorSuppressionFixer::OPTION_NOISE_REMAINING_USAGES_EXCLUDE => ['mkdir']],
  109. ];
  110. yield [
  111. '<?php @trigger_error("This is a deprecation warning.", E_USER_DEPRECATED); @trigger_error("This is not a deprecation warning.", E_USER_WARNING); ?>',
  112. '<?php trigger_error("This is a deprecation warning.", E_USER_DEPRECATED); @trigger_error("This is not a deprecation warning.", E_USER_WARNING); ?>',
  113. [ErrorSuppressionFixer::OPTION_MUTE_DEPRECATION_ERROR => true, ErrorSuppressionFixer::OPTION_NOISE_REMAINING_USAGES => true, ErrorSuppressionFixer::OPTION_NOISE_REMAINING_USAGES_EXCLUDE => ['trigger_error']],
  114. ];
  115. yield [
  116. '<?php @trigger_error("This is a deprecation warning.", E_USER_DEPRECATED, );',
  117. '<?php trigger_error("This is a deprecation warning.", E_USER_DEPRECATED, );',
  118. ];
  119. }
  120. /**
  121. * @dataProvider provideFixPre80Cases
  122. *
  123. * @requires PHP <8.0
  124. */
  125. public function testFixPre80(string $expected, ?string $input = null): void
  126. {
  127. $this->doTest($expected, $input);
  128. }
  129. /**
  130. * @return iterable<array{string}>
  131. */
  132. public static function provideFixPre80Cases(): iterable
  133. {
  134. yield [
  135. '<?php \A\B/* */\trigger_error("This is not a deprecation warning.", E_USER_DEPRECATED); ?>',
  136. ];
  137. }
  138. /**
  139. * @dataProvider provideFix81Cases
  140. *
  141. * @requires PHP 8.1
  142. */
  143. public function testFix81(string $expected, ?string $input = null): void
  144. {
  145. $this->doTest($expected, $input);
  146. }
  147. /**
  148. * @return iterable<array{string}>
  149. */
  150. public static function provideFix81Cases(): iterable
  151. {
  152. yield [
  153. '<?php $a = trigger_error(...);',
  154. ];
  155. }
  156. }