FinalPublicMethodForAbstractClassFixerTest.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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\ClassNotation;
  13. use PhpCsFixer\Tests\Test\AbstractFixerTestCase;
  14. /**
  15. * @author Filippo Tessarotto <zoeslam@gmail.com>
  16. *
  17. * @internal
  18. *
  19. * @covers \PhpCsFixer\Fixer\ClassNotation\FinalPublicMethodForAbstractClassFixer
  20. *
  21. * @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\ClassNotation\FinalPublicMethodForAbstractClassFixer>
  22. */
  23. final class FinalPublicMethodForAbstractClassFixerTest extends AbstractFixerTestCase
  24. {
  25. /**
  26. * @param string $expected PHP source code
  27. * @param null|string $input PHP source code
  28. *
  29. * @dataProvider provideFixCases
  30. */
  31. public function testFix(string $expected, ?string $input = null): void
  32. {
  33. $this->doTest($expected, $input);
  34. }
  35. /**
  36. * @return iterable<array{0: string, 1?: string}>
  37. */
  38. public static function provideFixCases(): iterable
  39. {
  40. $original = $fixed = self::getClassElementStubs();
  41. $fixed = str_replace('public function f1', 'final public function f1', $fixed);
  42. $fixed = str_replace('public static function f4', 'final public static function f4', $fixed);
  43. $fixed = str_replace('static public function f7', 'final static public function f7', $fixed);
  44. yield 'regular-class' => ["<?php class MyClass { {$original} }"];
  45. yield 'final-class' => ["<?php final class MyClass { {$original} }"];
  46. yield 'trait' => ["<?php trait MyClass { {$original} }"];
  47. yield 'interface' => [
  48. '<?php interface MyClass {
  49. public function f1();
  50. public static function f4();
  51. static public function f7();
  52. }',
  53. ];
  54. yield 'magic-methods' => [
  55. '<?php abstract class MyClass {
  56. public function __construct() {}
  57. public function __destruct() {}
  58. public function __call($a, $b) {}
  59. public static function __callStatic($a, $b) {}
  60. public function __get($a) {}
  61. public function __set($a, $b) {}
  62. public function __isset($a) {}
  63. public function __unset($a) {}
  64. public function __sleep() {}
  65. public function __wakeup() {}
  66. public function __toString() {}
  67. public function __invoke() {}
  68. public function __clone() {}
  69. public function __debugInfo() {}
  70. }',
  71. ];
  72. yield 'magic-methods-casing' => [
  73. '<?php abstract class MyClass {
  74. public function __Construct() {}
  75. public function __SET($a, $b) {}
  76. public function __ToString() {}
  77. public function __DeBuGiNfO() {}
  78. }',
  79. ];
  80. yield 'non magic-methods' => [
  81. '<?php abstract class MyClass {
  82. final public function __foo() {}
  83. final public static function __bar($a, $b) {}
  84. }',
  85. '<?php abstract class MyClass {
  86. public function __foo() {}
  87. public static function __bar($a, $b) {}
  88. }',
  89. ];
  90. yield 'abstract-class' => [
  91. "<?php abstract class MyClass { {$fixed} }",
  92. "<?php abstract class MyClass { {$original} }",
  93. ];
  94. yield 'abstract-class-with-abstract-public-methods' => [
  95. '<?php abstract class MyClass {
  96. abstract public function foo();
  97. abstract public static function bar();
  98. }',
  99. ];
  100. yield 'anonymous-class' => [
  101. \sprintf(
  102. '<?php abstract class MyClass { private function test() { $a = new class { %s }; } }',
  103. self::getClassElementStubs()
  104. ),
  105. ];
  106. yield 'constant visibility' => [
  107. '<?php abstract class MyClass {
  108. public const A = 1;
  109. protected const B = 2;
  110. private const C = 3;
  111. }',
  112. ];
  113. }
  114. /**
  115. * @dataProvider provideFix82Cases
  116. *
  117. * @requires PHP 8.2
  118. */
  119. public function testFix82(string $expected, ?string $input = null): void
  120. {
  121. $this->doTest($expected, $input);
  122. }
  123. /**
  124. * @return iterable<array{0: string, 1?: string}>
  125. */
  126. public static function provideFix82Cases(): iterable
  127. {
  128. yield 'abstract keyword after readonly/public keywords' => [
  129. '<?php readonly abstract class Foo {
  130. public abstract function bar();
  131. }',
  132. ];
  133. yield 'abstract keyword before readonly/public keywords' => [
  134. '<?php abstract readonly class Foo {
  135. abstract public function bar();
  136. }',
  137. ];
  138. yield 'abstract readonly class' => [
  139. '<?php abstract readonly class Foo {
  140. final public function bar() {}
  141. }',
  142. '<?php abstract readonly class Foo {
  143. public function bar() {}
  144. }',
  145. ];
  146. yield 'readonly abstract class' => [
  147. '<?php readonly abstract class Foo {
  148. final public function bar() {}
  149. }',
  150. '<?php readonly abstract class Foo {
  151. public function bar() {}
  152. }',
  153. ];
  154. }
  155. private static function getClassElementStubs(): string
  156. {
  157. return '
  158. public $a1;
  159. protected $a2;
  160. private $a3;
  161. public static $a4;
  162. protected static $a5;
  163. private static $a6;
  164. public function f1(){}
  165. protected function f2(){}
  166. private function f3(){}
  167. public static function f4(){}
  168. protected static function f5(){}
  169. private static function f6(){}
  170. static public function f7(){}
  171. static protected function f8(){}
  172. static private function f9(){}
  173. ';
  174. }
  175. }