LowercaseStaticReferenceFixerTest.php 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  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. final class LowercaseStaticReferenceFixerTest 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 static function provideFixCases(): iterable
  31. {
  32. yield [
  33. '<?php class Foo extends Bar { public function baz() { self::qux(); } }',
  34. '<?php class Foo extends Bar { public function baz() { SELF::qux(); } }',
  35. ];
  36. yield [
  37. '<?php class Foo extends Bar { public function baz() { static::qux(); } }',
  38. '<?php class Foo extends Bar { public function baz() { STATIC::qux(); } }',
  39. ];
  40. yield [
  41. '<?php class Foo extends Bar { public function baz() { parent::baz(); } }',
  42. '<?php class Foo extends Bar { public function baz() { PARENT::baz(); } }',
  43. ];
  44. yield [
  45. '<?php class Foo extends Bar { public function baz() { parent::baz(); } }',
  46. '<?php class Foo extends Bar { public function baz() { Parent::baz(); } }',
  47. ];
  48. yield [
  49. '<?php class Foo extends Bar { public function baz() { return new self(); } }',
  50. '<?php class Foo extends Bar { public function baz() { return new Self(); } }',
  51. ];
  52. yield [
  53. '<?php class Foo extends Bar { public function baz() { return SelfFoo::FOO; } }',
  54. ];
  55. yield [
  56. '<?php class Foo extends Bar { public function baz() { return FooSelf::FOO; } }',
  57. ];
  58. yield [
  59. '<?php class Foo extends Bar { private STATIC $baz; }',
  60. ];
  61. yield [
  62. '<?php class Foo extends Bar { STATIC private $baz; }',
  63. ];
  64. yield [
  65. '<?php class Foo extends Bar { public function paRent() {} }',
  66. ];
  67. yield [
  68. '<?php $foo->Self();',
  69. ];
  70. yield [
  71. '<?php Foo::Self();',
  72. ];
  73. yield [
  74. '<?php if ($foo instanceof self) { return true; }',
  75. '<?php if ($foo instanceof Self) { return true; }',
  76. ];
  77. yield [
  78. '<?php if ($foo instanceof static) { return true; }',
  79. '<?php if ($foo instanceof Static) { return true; }',
  80. ];
  81. yield [
  82. '<?php if ($foo instanceof parent) { return true; }',
  83. '<?php if ($foo instanceof Parent) { return true; }',
  84. ];
  85. yield [
  86. '<?php if ($foo instanceof Self\Bar) { return true; }',
  87. ];
  88. yield [
  89. '<?php if ($foo instanceof MySelf) { return true; }',
  90. ];
  91. yield [
  92. '<?php class Foo extends Bar { public function baz(self $x) {} }',
  93. '<?php class Foo extends Bar { public function baz(Self $x) {} }',
  94. ];
  95. yield [
  96. '<?php class Foo extends Bar { public function baz(parent $x) {} }',
  97. '<?php class Foo extends Bar { public function baz(Parent $x) {} }',
  98. ];
  99. yield [
  100. '<?php class Foo extends Bar { public function baz(MySelf $x) {} }',
  101. ];
  102. yield [
  103. '<?php class Foo extends Bar { public function baz(Self\Qux $x) {} }',
  104. ];
  105. yield [
  106. '<?php $a = STATIC function() {};',
  107. ];
  108. yield [
  109. '<?php class A { public function B() { STATIC $a; echo $a; }}',
  110. ];
  111. yield [
  112. '<?php class A { public function B() { $collection = $static ? new static($b) : new self(); } }',
  113. '<?php class A { public function B() { $collection = $static ? new STATIC($b) : new self(); } }',
  114. ];
  115. yield [
  116. '<?php class A { STATIC public function B() {} }',
  117. ];
  118. yield [
  119. '<?php
  120. $a = function () {
  121. STATIC $B = false;
  122. if ($B) {
  123. echo 1;
  124. }
  125. $B = true;
  126. };
  127. ',
  128. ];
  129. yield [
  130. '<?php class A { const PARENT = 42; }',
  131. ];
  132. yield [
  133. '<?php namespace Foo\Parent;',
  134. ];
  135. yield [
  136. '<?php namespace Parent\Foo;',
  137. ];
  138. yield [
  139. '<?php class Foo extends Bar { public function baz() : self {} }',
  140. '<?php class Foo extends Bar { public function baz() : Self {} }',
  141. ];
  142. yield [
  143. '<?php class Foo extends Bar { public function baz() : parent {} }',
  144. '<?php class Foo extends Bar { public function baz() : Parent {} }',
  145. ];
  146. yield [
  147. '<?php class Foo extends Bar { public function baz() : MySelf {} }',
  148. ];
  149. yield [
  150. '<?php class Foo extends Bar { public function baz() : Self\Qux {} }',
  151. ];
  152. yield [
  153. '<?php class Foo extends Bar { public function baz(?self $x) {} }',
  154. '<?php class Foo extends Bar { public function baz(?Self $x) {} }',
  155. ];
  156. yield [
  157. '<?php class Foo extends Bar { public function baz(?parent $x) {} }',
  158. '<?php class Foo extends Bar { public function baz(?Parent $x) {} }',
  159. ];
  160. yield [
  161. '<?php class Foo extends Bar { public function baz() : ?self {} }',
  162. '<?php class Foo extends Bar { public function baz() : ?Self {} }',
  163. ];
  164. yield [
  165. '<?php class Foo extends Bar { public function baz() : ?parent {} }',
  166. '<?php class Foo extends Bar { public function baz() : ?Parent {} }',
  167. ];
  168. yield [
  169. '<?php class Foo extends Bar { public function baz() : ?MySelf {} }',
  170. ];
  171. yield [
  172. '<?php class Foo extends Bar { public function baz() : ?Self\Qux {} }',
  173. ];
  174. yield [
  175. '<?php class Foo {
  176. private STATIC int $baz1;
  177. private STATIC ?int $baz2;
  178. }',
  179. ];
  180. yield [
  181. '<?php
  182. class Foo { public function bar() {} }
  183. class FooChild extends Foo
  184. {
  185. public function bar()
  186. {
  187. switch (true) {
  188. case parent::bar():
  189. }
  190. }
  191. }',
  192. '<?php
  193. class Foo { public function bar() {} }
  194. class FooChild extends Foo
  195. {
  196. public function bar()
  197. {
  198. switch (true) {
  199. case PARENT::bar():
  200. }
  201. }
  202. }',
  203. ];
  204. }
  205. /**
  206. * @dataProvider provideFix80Cases
  207. *
  208. * @requires PHP 8.0
  209. */
  210. public function testFix80(string $expected, ?string $input = null): void
  211. {
  212. $this->doTest($expected, $input);
  213. }
  214. public static function provideFix80Cases(): iterable
  215. {
  216. yield ['<?php $foo?->Self();'];
  217. yield [
  218. '<?php class Foo extends A {
  219. public function baz1() : int|parent {}
  220. public function baz2() : parent|int {}
  221. public function baz3() : ?parent {}
  222. }',
  223. '<?php class Foo extends A {
  224. public function baz1() : int|Parent {}
  225. public function baz2() : Parent|int {}
  226. public function baz3() : ?Parent {}
  227. }',
  228. ];
  229. yield [
  230. '<?php class Foo extends A {
  231. public function baz1() : int|static {}
  232. public function baz2() : static|int {}
  233. public function baz3() : ?static {}
  234. }',
  235. '<?php class Foo extends A {
  236. public function baz1() : int|STATIC {}
  237. public function baz2() : STATIC|int {}
  238. public function baz3() : ?STATIC {}
  239. }',
  240. ];
  241. yield [
  242. '<?php
  243. class Foo
  244. {
  245. private int|self $prop1, $prop2;
  246. private self|int $prop3, $prop4;
  247. }
  248. ',
  249. '<?php
  250. class Foo
  251. {
  252. private int|SELF $prop1, $prop2;
  253. private SELF|int $prop3, $prop4;
  254. }
  255. ',
  256. ];
  257. }
  258. /**
  259. * @dataProvider provideFix81Cases
  260. *
  261. * @requires PHP 8.1
  262. */
  263. public function testFix81(string $expected, ?string $input = null): void
  264. {
  265. $this->doTest($expected, $input);
  266. }
  267. public static function provideFix81Cases(): iterable
  268. {
  269. yield [
  270. '<?php class A { final const PARENT = 42; }',
  271. ];
  272. yield [
  273. '<?php enum Foo: string { case PARENT = \'parent\'; }',
  274. ];
  275. }
  276. }