SelfAccessorFixerTest.php 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  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 Gregor Harlan <gharlan@web.de>
  16. *
  17. * @internal
  18. *
  19. * @covers \PhpCsFixer\Fixer\ClassNotation\SelfAccessorFixer
  20. *
  21. * @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\ClassNotation\SelfAccessorFixer>
  22. */
  23. final class SelfAccessorFixerTest extends AbstractFixerTestCase
  24. {
  25. /**
  26. * @dataProvider provideFixCases
  27. */
  28. public function testFix(string $expected, ?string $input = null): void
  29. {
  30. $this->doTest($expected, $input);
  31. }
  32. /**
  33. * @return iterable<int|string, array{0: string, 1?: string}>
  34. */
  35. public static function provideFixCases(): iterable
  36. {
  37. yield [
  38. '<?php class Foo { const BAR = self::BAZ; }',
  39. '<?php class Foo { const BAR = Foo::BAZ; }',
  40. ];
  41. yield [
  42. '<?php class Foo { private $bar = self::BAZ; }',
  43. '<?php class Foo { private $bar = fOO::BAZ; }', // case insensitive
  44. ];
  45. yield [
  46. '<?php class Foo { function bar($a = self::BAR) {} }',
  47. '<?php class Foo { function bar($a = Foo::BAR) {} }',
  48. ];
  49. yield [
  50. '<?php class Foo { function bar() { self::baz(); } }',
  51. '<?php class Foo { function bar() { Foo::baz(); } }',
  52. ];
  53. yield [
  54. '<?php class Foo { function bar() { self::class; } }',
  55. '<?php class Foo { function bar() { Foo::class; } }',
  56. ];
  57. yield [
  58. '<?php class Foo { function bar() { $x instanceof self; } }',
  59. '<?php class Foo { function bar() { $x instanceof Foo; } }',
  60. ];
  61. yield [
  62. '<?php class Foo { function bar() { new self(); } }',
  63. '<?php class Foo { function bar() { new Foo(); } }',
  64. ];
  65. yield [
  66. '<?php interface Foo { const BAR = self::BAZ; function bar($a = self::BAR); }',
  67. '<?php interface Foo { const BAR = Foo::BAZ; function bar($a = Foo::BAR); }',
  68. ];
  69. yield [
  70. '<?php class Foo { const Foo = 1; }',
  71. ];
  72. yield [
  73. '<?php class Foo { function foo() { } }',
  74. ];
  75. yield [
  76. '<?php class Foo { function bar() { new \Baz\Foo(); } }',
  77. ];
  78. yield [
  79. '<?php class Foo { function bar() { new Foo\Baz(); } }',
  80. ];
  81. yield [
  82. '<?php class Foo { function bar() { Baz\Foo::class; } }',
  83. ];
  84. yield [
  85. // In trait "self" will reference the class it's used in, not the actual trait, so we can't replace "Foo" with "self" here
  86. '<?php trait Foo { function bar() { Foo::bar(); } } class Bar { use Foo; }',
  87. ];
  88. yield [
  89. '<?php class Foo { public function bar(self $foo, self $bar) { return new self(); } }',
  90. '<?php class Foo { public function bar(Foo $foo, Foo $bar) { return new Foo(); } }',
  91. ];
  92. yield [
  93. '<?php interface Foo { public function bar(self $foo, self $bar); }',
  94. '<?php interface Foo { public function bar(Foo $foo, Foo $bar); }',
  95. ];
  96. yield [
  97. '<?php interface Foo { public function bar(self $foo); }',
  98. '<?php interface Foo { public function bar(\Foo $foo); }',
  99. ];
  100. yield [
  101. '<?php namespace Foo; interface Bar { public function baz(\Bar $bar); }',
  102. ];
  103. yield [
  104. '<?php namespace Foo; interface Bar { public function baz(self $bar); }',
  105. '<?php namespace Foo; interface Bar { public function baz(Bar $bar); }',
  106. ];
  107. yield [
  108. '<?php namespace Foo; interface Bar { public function baz(self $bar); }',
  109. '<?php namespace Foo; interface Bar { public function baz(\Foo\Bar $bar); }',
  110. ];
  111. yield [
  112. '<?php namespace Foo; interface Bar { public function baz(Foo\Bar $bar); }',
  113. ];
  114. yield [
  115. '<?php namespace Foo; interface Bar { public function baz(Bar\Foo $bar); }',
  116. ];
  117. yield [
  118. '<?php
  119. namespace Foo\Foo2;
  120. interface Bar {
  121. public function baz00(Foo2\Bar $bar);
  122. public function baz10(\Foo2\Bar $bar);
  123. public function baz20(Foo\Foo2\Bar $bar);
  124. public function baz21(self $bar);
  125. public function baz30(Test\Foo\Foo2\Bar $bar);
  126. public function baz31(\Test\Foo\Foo2\Bar $bar);
  127. }',
  128. '<?php
  129. namespace Foo\Foo2;
  130. interface Bar {
  131. public function baz00(Foo2\Bar $bar);
  132. public function baz10(\Foo2\Bar $bar);
  133. public function baz20(Foo\Foo2\Bar $bar);
  134. public function baz21(\Foo\Foo2\Bar $bar);
  135. public function baz30(Test\Foo\Foo2\Bar $bar);
  136. public function baz31(\Test\Foo\Foo2\Bar $bar);
  137. }',
  138. ];
  139. yield [
  140. '<?php class Foo { function bar() {
  141. new class() { function baz() { new Foo(); } };
  142. } }',
  143. ];
  144. yield [
  145. '<?php class Foo { protected $foo; function bar() { return $this->foo::find(2); } }',
  146. ];
  147. yield [
  148. '<?php class Foo { public function bar(self $foo, self $bar): self { return new self(); } }',
  149. '<?php class Foo { public function bar(Foo $foo, Foo $bar): Foo { return new Foo(); } }',
  150. ];
  151. yield [
  152. '<?php interface Foo { public function bar(self $foo, self $bar): self; }',
  153. '<?php interface Foo { public function bar(Foo $foo, Foo $bar): Foo; }',
  154. ];
  155. yield [
  156. '<?php class Foo { public function bar(?self $foo, ?self $bar): ?self { return new self(); } }',
  157. '<?php class Foo { public function bar(?Foo $foo, ?Foo $bar): ?Foo { return new Foo(); } }',
  158. ];
  159. yield [
  160. "<?php interface Foo{ public function bar()\t/**/:?/**/self; }",
  161. "<?php interface Foo{ public function bar()\t/**/:?/**/Foo; }",
  162. ];
  163. yield 'do not replace in lambda' => [
  164. '<?php
  165. final class A
  166. {
  167. public static function Z(): void
  168. {
  169. (function () {
  170. var_dump(self::class, A::class);
  171. })->bindTo(null, B::class)();
  172. }
  173. }',
  174. ];
  175. yield 'do not replace in arrow function' => [
  176. '<?php
  177. final class A
  178. {
  179. public function Z($b): void
  180. {
  181. $a = fn($b) => self::class. A::class . $b;
  182. echo $a;
  183. }
  184. }',
  185. ];
  186. }
  187. /**
  188. * @dataProvider provideFix80Cases
  189. *
  190. * @requires PHP 8.0
  191. */
  192. public function testFix80(string $expected, ?string $input = null): void
  193. {
  194. $this->doTest($expected, $input);
  195. }
  196. /**
  197. * @return iterable<array{0: string, 1?: string}>
  198. */
  199. public static function provideFix80Cases(): iterable
  200. {
  201. yield [
  202. '<?php interface Foo { public function bar(self $foo, self $bar,): self; }',
  203. '<?php interface Foo { public function bar(Foo $foo, Foo $bar,): Foo; }',
  204. ];
  205. yield [
  206. '<?php class Foo { function bar() { $x instanceof (Foo()); } }',
  207. ];
  208. yield [
  209. '<?php class Foo { protected $foo; function bar() { return $this?->foo::find(2); } }',
  210. ];
  211. yield [
  212. '<?php class Foo { public function f(self|Bar|Baz $b) {} }',
  213. '<?php class Foo { public function f(Foo|Bar|Baz $b) {} }',
  214. ];
  215. yield [
  216. '<?php class Foo { public function f(Bar|self|Baz $b) {} }',
  217. '<?php class Foo { public function f(Bar|Foo|Baz $b) {} }',
  218. ];
  219. yield [
  220. '<?php class Foo { public function f(Bar|Baz|self $b) {} }',
  221. '<?php class Foo { public function f(Bar|Baz|Foo $b) {} }',
  222. ];
  223. yield [
  224. '<?php class Foo { public function f(Bar|Foo\C|Baz $b) {} }',
  225. ];
  226. yield [
  227. '<?php class Foo { public function f(Bar|C\Foo|Baz $b) {} }',
  228. ];
  229. }
  230. /**
  231. * @dataProvider provideFix82Cases
  232. *
  233. * @requires PHP 8.2
  234. */
  235. public function testFix82(string $expected, ?string $input = null): void
  236. {
  237. $this->doTest($expected, $input);
  238. }
  239. /**
  240. * @return iterable<array{string, string}>
  241. */
  242. public static function provideFix82Cases(): iterable
  243. {
  244. yield [
  245. '<?php class Foo { public function f(self|(Bar&Baz)|Qux $b) {} }',
  246. '<?php class Foo { public function f(Foo|(Bar&Baz)|Qux $b) {} }',
  247. ];
  248. yield [
  249. '<?php class Foo { public function f(Bar|(Baz&Qux)|self $b) {} }',
  250. '<?php class Foo { public function f(Bar|(Baz&Qux)|Foo $b) {} }',
  251. ];
  252. }
  253. }