LowercaseStaticReferenceFixerTest.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  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\Casing;
  13. use PhpCsFixer\Tests\Test\AbstractFixerTestCase;
  14. /**
  15. * @author Kuba Werłos <werlos@gmail.com>
  16. *
  17. * @covers \PhpCsFixer\Fixer\Casing\LowercaseStaticReferenceFixer
  18. *
  19. * @internal
  20. *
  21. * @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\Casing\LowercaseStaticReferenceFixer>
  22. */
  23. final class LowercaseStaticReferenceFixerTest 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<array{0: string, 1?: string}>
  34. */
  35. public static function provideFixCases(): iterable
  36. {
  37. yield [
  38. '<?php class Foo extends Bar { public function baz() { self::qux(); } }',
  39. '<?php class Foo extends Bar { public function baz() { SELF::qux(); } }',
  40. ];
  41. yield [
  42. '<?php class Foo extends Bar { public function baz() { static::qux(); } }',
  43. '<?php class Foo extends Bar { public function baz() { STATIC::qux(); } }',
  44. ];
  45. yield [
  46. '<?php class Foo extends Bar { public function baz() { parent::baz(); } }',
  47. '<?php class Foo extends Bar { public function baz() { PARENT::baz(); } }',
  48. ];
  49. yield [
  50. '<?php class Foo extends Bar { public function baz() { parent::baz(); } }',
  51. '<?php class Foo extends Bar { public function baz() { Parent::baz(); } }',
  52. ];
  53. yield [
  54. '<?php class Foo extends Bar { public function baz() { return new self(); } }',
  55. '<?php class Foo extends Bar { public function baz() { return new Self(); } }',
  56. ];
  57. yield [
  58. '<?php class Foo extends Bar { public function baz() { return SelfFoo::FOO; } }',
  59. ];
  60. yield [
  61. '<?php class Foo extends Bar { public function baz() { return FooSelf::FOO; } }',
  62. ];
  63. yield [
  64. '<?php class Foo extends Bar { private STATIC $baz; }',
  65. ];
  66. yield [
  67. '<?php class Foo extends Bar { STATIC private $baz; }',
  68. ];
  69. yield [
  70. '<?php class Foo extends Bar { public function paRent() {} }',
  71. ];
  72. yield [
  73. '<?php $foo->Self();',
  74. ];
  75. yield [
  76. '<?php Foo::Self();',
  77. ];
  78. yield [
  79. '<?php if ($foo instanceof self) { return true; }',
  80. '<?php if ($foo instanceof Self) { return true; }',
  81. ];
  82. yield [
  83. '<?php if ($foo instanceof static) { return true; }',
  84. '<?php if ($foo instanceof Static) { return true; }',
  85. ];
  86. yield [
  87. '<?php if ($foo instanceof parent) { return true; }',
  88. '<?php if ($foo instanceof Parent) { return true; }',
  89. ];
  90. yield [
  91. '<?php if ($foo instanceof Self\Bar) { return true; }',
  92. ];
  93. yield [
  94. '<?php if ($foo instanceof MySelf) { return true; }',
  95. ];
  96. yield [
  97. '<?php class Foo extends Bar { public function baz(self $x) {} }',
  98. '<?php class Foo extends Bar { public function baz(Self $x) {} }',
  99. ];
  100. yield [
  101. '<?php class Foo extends Bar { public function baz(parent $x) {} }',
  102. '<?php class Foo extends Bar { public function baz(Parent $x) {} }',
  103. ];
  104. yield [
  105. '<?php class Foo extends Bar { public function baz(MySelf $x) {} }',
  106. ];
  107. yield [
  108. '<?php class Foo extends Bar { public function baz(Self\Qux $x) {} }',
  109. ];
  110. yield [
  111. '<?php $a = STATIC function() {};',
  112. ];
  113. yield [
  114. '<?php class A { public function B() { STATIC $a; echo $a; }}',
  115. ];
  116. yield [
  117. '<?php class A { public function B() { $collection = $static ? new static($b) : new self(); } }',
  118. '<?php class A { public function B() { $collection = $static ? new STATIC($b) : new self(); } }',
  119. ];
  120. yield [
  121. '<?php class A { STATIC public function B() {} }',
  122. ];
  123. yield [
  124. '<?php
  125. $a = function () {
  126. STATIC $B = false;
  127. if ($B) {
  128. echo 1;
  129. }
  130. $B = true;
  131. };
  132. ',
  133. ];
  134. yield [
  135. '<?php class A { const PARENT = 42; }',
  136. ];
  137. yield [
  138. '<?php namespace Foo\Parent;',
  139. ];
  140. yield [
  141. '<?php namespace Parent\Foo;',
  142. ];
  143. yield [
  144. '<?php class Foo extends Bar { public function baz() : self {} }',
  145. '<?php class Foo extends Bar { public function baz() : Self {} }',
  146. ];
  147. yield [
  148. '<?php class Foo extends Bar { public function baz() : parent {} }',
  149. '<?php class Foo extends Bar { public function baz() : Parent {} }',
  150. ];
  151. yield [
  152. '<?php class Foo extends Bar { public function baz() : MySelf {} }',
  153. ];
  154. yield [
  155. '<?php class Foo extends Bar { public function baz() : Self\Qux {} }',
  156. ];
  157. yield [
  158. '<?php class Foo extends Bar { public function baz(?self $x) {} }',
  159. '<?php class Foo extends Bar { public function baz(?Self $x) {} }',
  160. ];
  161. yield [
  162. '<?php class Foo extends Bar { public function baz(?parent $x) {} }',
  163. '<?php class Foo extends Bar { public function baz(?Parent $x) {} }',
  164. ];
  165. yield [
  166. '<?php class Foo extends Bar { public function baz() : ?self {} }',
  167. '<?php class Foo extends Bar { public function baz() : ?Self {} }',
  168. ];
  169. yield [
  170. '<?php class Foo extends Bar { public function baz() : ?parent {} }',
  171. '<?php class Foo extends Bar { public function baz() : ?Parent {} }',
  172. ];
  173. yield [
  174. '<?php class Foo extends Bar { public function baz() : ?MySelf {} }',
  175. ];
  176. yield [
  177. '<?php class Foo extends Bar { public function baz() : ?Self\Qux {} }',
  178. ];
  179. yield [
  180. '<?php class Foo {
  181. private STATIC int $baz1;
  182. private STATIC ?int $baz2;
  183. }',
  184. ];
  185. yield [
  186. '<?php
  187. class Foo { public function bar() {} }
  188. class FooChild extends Foo
  189. {
  190. public function bar()
  191. {
  192. switch (true) {
  193. case parent::bar():
  194. }
  195. }
  196. }',
  197. '<?php
  198. class Foo { public function bar() {} }
  199. class FooChild extends Foo
  200. {
  201. public function bar()
  202. {
  203. switch (true) {
  204. case PARENT::bar():
  205. }
  206. }
  207. }',
  208. ];
  209. }
  210. /**
  211. * @dataProvider provideFix80Cases
  212. *
  213. * @requires PHP 8.0
  214. */
  215. public function testFix80(string $expected, ?string $input = null): void
  216. {
  217. $this->doTest($expected, $input);
  218. }
  219. /**
  220. * @return iterable<array{0: string, 1?: string}>
  221. */
  222. public static function provideFix80Cases(): iterable
  223. {
  224. yield ['<?php $foo?->Self();'];
  225. yield [
  226. '<?php class Foo extends A {
  227. public function baz1() : int|parent {}
  228. public function baz2() : parent|int {}
  229. public function baz3() : ?parent {}
  230. }',
  231. '<?php class Foo extends A {
  232. public function baz1() : int|Parent {}
  233. public function baz2() : Parent|int {}
  234. public function baz3() : ?Parent {}
  235. }',
  236. ];
  237. yield [
  238. '<?php class Foo extends A {
  239. public function baz1() : int|static {}
  240. public function baz2() : static|int {}
  241. public function baz3() : ?static {}
  242. }',
  243. '<?php class Foo extends A {
  244. public function baz1() : int|STATIC {}
  245. public function baz2() : STATIC|int {}
  246. public function baz3() : ?STATIC {}
  247. }',
  248. ];
  249. yield [
  250. '<?php
  251. class Foo
  252. {
  253. private int|self $prop1, $prop2;
  254. private self|int $prop3, $prop4;
  255. }
  256. ',
  257. '<?php
  258. class Foo
  259. {
  260. private int|SELF $prop1, $prop2;
  261. private SELF|int $prop3, $prop4;
  262. }
  263. ',
  264. ];
  265. }
  266. /**
  267. * @dataProvider provideFix81Cases
  268. *
  269. * @requires PHP 8.1
  270. */
  271. public function testFix81(string $expected, ?string $input = null): void
  272. {
  273. $this->doTest($expected, $input);
  274. }
  275. /**
  276. * @return iterable<array{string}>
  277. */
  278. public static function provideFix81Cases(): iterable
  279. {
  280. yield [
  281. '<?php class A { final const PARENT = 42; }',
  282. ];
  283. yield [
  284. '<?php enum Foo: string { case PARENT = \'parent\'; }',
  285. ];
  286. }
  287. /**
  288. * @dataProvider provideFix83Cases
  289. *
  290. * @requires PHP 8.3
  291. */
  292. public function testFix83(string $expected, ?string $input = null): void
  293. {
  294. $this->doTest($expected, $input);
  295. }
  296. /**
  297. * @return iterable<array{0: string, 1?: null|string}>
  298. */
  299. public static function provideFix83Cases(): iterable
  300. {
  301. yield [<<<'PHP'
  302. <?php
  303. class Foo {
  304. private const array PARENT = ['parent'];
  305. private const array SELF = ['self'];
  306. private const array STATIC = ['static'];
  307. }
  308. PHP];
  309. yield [<<<'PHP'
  310. <?php
  311. class Foo {
  312. private const int PARENT = 1;
  313. private const int SELF = 2;
  314. private const int STATIC = 3;
  315. }
  316. PHP];
  317. yield [<<<'PHP'
  318. <?php
  319. class Foo {
  320. private const int|static PARENT = 1;
  321. private const int|static SELF = 2;
  322. private const int|static STATIC = 3;
  323. }
  324. PHP];
  325. yield [<<<'PHP'
  326. <?php
  327. class Foo {
  328. private const string|(Bar&Baz) PARENT = 'parent';
  329. private const string|(Bar&Baz) SELF = 'self';
  330. private const string|(Bar&Baz) STATIC = 'static';
  331. }
  332. PHP];
  333. }
  334. }