ErrorSuppressionFixerTest.php 6.4 KB

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