ProtectedToPrivateFixerTest.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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\ProtectedToPrivateFixer
  20. */
  21. final class ProtectedToPrivateFixerTest 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. public function provideFixCases(): array
  31. {
  32. $attributesAndMethodsOriginal = $this->getAttributesAndMethods(true);
  33. $attributesAndMethodsFixed = $this->getAttributesAndMethods(false);
  34. return [
  35. 'final-extends' => [
  36. "<?php final class MyClass extends MyAbstractClass { {$attributesAndMethodsOriginal} }",
  37. ],
  38. 'normal-extends' => [
  39. "<?php class MyClass extends MyAbstractClass { {$attributesAndMethodsOriginal} }",
  40. ],
  41. 'abstract' => [
  42. "<?php abstract class MyAbstractClass { {$attributesAndMethodsOriginal} }",
  43. ],
  44. 'normal' => [
  45. "<?php class MyClass { {$attributesAndMethodsOriginal} }",
  46. ],
  47. 'trait' => [
  48. "<?php trait MyTrait { {$attributesAndMethodsOriginal} }",
  49. ],
  50. 'final-with-trait' => [
  51. "<?php final class MyClass { use MyTrait; {$attributesAndMethodsOriginal} }",
  52. ],
  53. 'multiline-comment' => [
  54. '<?php final class MyClass { /* public protected private */ }',
  55. ],
  56. 'inline-comment' => [
  57. "<?php final class MyClass { \n // public protected private \n }",
  58. ],
  59. 'final' => [
  60. "<?php final class MyClass { {$attributesAndMethodsFixed} } class B {use C;}",
  61. "<?php final class MyClass { {$attributesAndMethodsOriginal} } class B {use C;}",
  62. ],
  63. 'final-implements' => [
  64. "<?php final class MyClass implements MyInterface { {$attributesAndMethodsFixed} }",
  65. "<?php final class MyClass implements MyInterface { {$attributesAndMethodsOriginal} }",
  66. ],
  67. 'final-with-use-before' => [
  68. "<?php use stdClass; final class MyClass { {$attributesAndMethodsFixed} }",
  69. "<?php use stdClass; final class MyClass { {$attributesAndMethodsOriginal} }",
  70. ],
  71. 'final-with-use-after' => [
  72. "<?php final class MyClass { {$attributesAndMethodsFixed} } use stdClass;",
  73. "<?php final class MyClass { {$attributesAndMethodsOriginal} } use stdClass;",
  74. ],
  75. 'multiple-classes' => [
  76. "<?php final class MyFirstClass { {$attributesAndMethodsFixed} } class MySecondClass { {$attributesAndMethodsOriginal} } final class MyThirdClass { {$attributesAndMethodsFixed} } ",
  77. "<?php final class MyFirstClass { {$attributesAndMethodsOriginal} } class MySecondClass { {$attributesAndMethodsOriginal} } final class MyThirdClass { {$attributesAndMethodsOriginal} } ",
  78. ],
  79. 'minimal-set' => [
  80. '<?php final class MyClass { private $v1; }',
  81. '<?php final class MyClass { protected $v1; }',
  82. ],
  83. 'anonymous-class-inside' => [
  84. "<?php
  85. final class Foo
  86. {
  87. {$attributesAndMethodsFixed}
  88. private function bar()
  89. {
  90. new class {
  91. {$attributesAndMethodsOriginal}
  92. };
  93. }
  94. }
  95. ",
  96. "<?php
  97. final class Foo
  98. {
  99. {$attributesAndMethodsOriginal}
  100. protected function bar()
  101. {
  102. new class {
  103. {$attributesAndMethodsOriginal}
  104. };
  105. }
  106. }
  107. ",
  108. ],
  109. [
  110. '<?php $a = new class{protected function A(){ echo 123; }};',
  111. ],
  112. ];
  113. }
  114. /**
  115. * @dataProvider provideFix74Cases
  116. * @requires PHP 7.4
  117. */
  118. public function test74Fix(string $expected, ?string $input = null): void
  119. {
  120. $this->doTest($expected, $input);
  121. }
  122. public function provideFix74Cases(): \Generator
  123. {
  124. yield [
  125. '<?php final class Foo { private int $foo; }',
  126. '<?php final class Foo { protected int $foo; }',
  127. ];
  128. yield [
  129. '<?php final class Foo { private ?string $foo; }',
  130. '<?php final class Foo { protected ?string $foo; }',
  131. ];
  132. yield [
  133. '<?php final class Foo { private array $foo; }',
  134. '<?php final class Foo { protected array $foo; }',
  135. ];
  136. }
  137. /**
  138. * @dataProvider provideFix80Cases
  139. * @requires PHP 8.0
  140. */
  141. public function testFix80(string $expected, ?string $input = null): void
  142. {
  143. $this->doTest($expected, $input);
  144. }
  145. public static function provideFix80Cases(): \Generator
  146. {
  147. yield [
  148. '<?php
  149. final class Foo2 {
  150. private int|float $a;
  151. }
  152. ',
  153. '<?php
  154. final class Foo2 {
  155. protected int|float $a;
  156. }
  157. ',
  158. ];
  159. }
  160. /**
  161. * @dataProvider provideFix81Cases
  162. * @requires PHP 8.1
  163. */
  164. public function testFix81(string $expected, ?string $input = null): void
  165. {
  166. $this->doTest($expected, $input);
  167. }
  168. public static function provideFix81Cases(): \Generator
  169. {
  170. yield [
  171. '<?php
  172. final class Foo { private readonly int $d; }
  173. ',
  174. '<?php
  175. final class Foo { protected readonly int $d; }
  176. ',
  177. ];
  178. yield [
  179. // '<?php final class Foo { final private const Y = "i"; }', 'Fatal error: Private constant Foo::Y cannot be final as it is not visible to other classes on line 1.
  180. '<?php
  181. final class Foo1 { final protected const Y = "abc"; }
  182. final class Foo2 { protected final const Y = "def"; }
  183. ',
  184. ];
  185. yield [
  186. '<?php final class Foo { private Foo1&Bar $foo; }',
  187. '<?php final class Foo { protected Foo1&Bar $foo; }',
  188. ];
  189. }
  190. private function getAttributesAndMethods(bool $original): string
  191. {
  192. $attributesAndMethodsOriginal = '
  193. public $v1;
  194. protected $v2;
  195. private $v3;
  196. public static $v4;
  197. protected static $v5;
  198. private static $v6;
  199. public function f1(){}
  200. protected function f2(){}
  201. private function f3(){}
  202. public static function f4(){}
  203. protected static function f5(){}
  204. private static function f6(){}
  205. ';
  206. if ($original) {
  207. return $attributesAndMethodsOriginal;
  208. }
  209. return str_replace('protected', 'private', $attributesAndMethodsOriginal);
  210. }
  211. }