ClassKeywordRemoveFixerTest.php 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  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\LanguageConstruct;
  13. use PhpCsFixer\Tests\Test\AbstractFixerTestCase;
  14. /**
  15. * @author Sullivan Senechal <soullivaneuh@gmail.com>
  16. *
  17. * @internal
  18. *
  19. * @covers \PhpCsFixer\Fixer\LanguageConstruct\ClassKeywordRemoveFixer
  20. *
  21. * @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\LanguageConstruct\ClassKeywordRemoveFixer>
  22. */
  23. final class ClassKeywordRemoveFixerTest 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, 2?: string, 3?: string}>
  34. */
  35. public static function provideFixCases(): iterable
  36. {
  37. yield [
  38. "<?php
  39. use Foo\\Bar\\Thing;
  40. echo 'Foo\\Bar\\Thing';
  41. ",
  42. '<?php
  43. use Foo\Bar\Thing;
  44. echo Thing::class;
  45. ',
  46. ];
  47. yield [
  48. '<?php
  49. use Foo\Bar;
  50. '."
  51. echo 'Foo\\Bar\\Thing';
  52. ",
  53. '<?php
  54. use Foo\Bar;
  55. '.'
  56. echo Bar\Thing::class;
  57. ',
  58. ];
  59. yield [
  60. "<?php
  61. namespace Foo;
  62. use Foo\\Bar;
  63. echo 'Foo\\Bar\\Baz';
  64. ",
  65. '<?php
  66. namespace Foo;
  67. use Foo\Bar;
  68. echo \Foo\Bar\Baz::class;
  69. ',
  70. ];
  71. yield [
  72. "<?php
  73. use Foo\\Bar\\Thing as Alias;
  74. echo 'Foo\\Bar\\Thing';
  75. ",
  76. '<?php
  77. use Foo\Bar\Thing as Alias;
  78. echo Alias::class;
  79. ',
  80. ];
  81. yield [
  82. "<?php
  83. use Foo\\Bar\\Dummy;
  84. use Foo\\Bar\\Thing as Alias;
  85. echo 'Foo\\Bar\\Dummy';
  86. echo 'Foo\\Bar\\Thing';
  87. ",
  88. '<?php
  89. use Foo\Bar\Dummy;
  90. use Foo\Bar\Thing as Alias;
  91. echo Dummy::class;
  92. echo Alias::class;
  93. ',
  94. ];
  95. yield [
  96. "<?php
  97. echo 'DateTime';
  98. ",
  99. '<?php
  100. echo \DateTime::class;
  101. ',
  102. ];
  103. yield [
  104. "<?php
  105. echo 'Thing';
  106. ",
  107. '<?php
  108. echo Thing::class;
  109. ',
  110. ];
  111. yield [
  112. "<?php
  113. class Foo {
  114. public function amazingFunction() {
  115. echo 'Thing';
  116. }
  117. }
  118. ",
  119. '<?php
  120. class Foo {
  121. public function amazingFunction() {
  122. echo Thing::class;
  123. }
  124. }
  125. ',
  126. ];
  127. yield [
  128. "<?php
  129. namespace A\\B;
  130. use Foo\\Bar;
  131. echo 'Foo\\Bar';
  132. ",
  133. '<?php
  134. namespace A\B;
  135. use Foo\Bar;
  136. echo Bar::class;
  137. ',
  138. ];
  139. yield [
  140. "<?php
  141. namespace A\\B {
  142. class D {
  143. }
  144. }
  145. namespace B\\B {
  146. class D {
  147. }
  148. }
  149. namespace C {
  150. use A\\B\\D;
  151. var_dump('A\\B\\D');
  152. }
  153. namespace C1 {
  154. use B\\B\\D;
  155. var_dump('B\\B\\D');
  156. }
  157. ",
  158. '<?php
  159. namespace A\B {
  160. class D {
  161. }
  162. }
  163. namespace B\B {
  164. class D {
  165. }
  166. }
  167. namespace C {
  168. use A\B\D;
  169. var_dump(D::class);
  170. }
  171. namespace C1 {
  172. use B\B\D;
  173. var_dump(D::class);
  174. }
  175. ',
  176. ];
  177. yield [
  178. '<?php
  179. namespace Foo;
  180. class Bar extends Baz {
  181. public function a() {
  182. return self::class;
  183. }
  184. public function b() {
  185. return static::class;
  186. }
  187. public function c() {
  188. return parent::class;
  189. }
  190. }
  191. ',
  192. ];
  193. yield [
  194. "<?php
  195. namespace Foo;
  196. var_dump('Foo\\Bar\\Baz');
  197. ",
  198. '<?php
  199. namespace Foo;
  200. var_dump(Bar\Baz::class);
  201. ',
  202. ];
  203. yield [
  204. "<?php
  205. namespace Foo\\Bar;
  206. var_dump('Foo\\Bar\\Baz');
  207. ",
  208. '<?php
  209. namespace Foo\Bar;
  210. var_dump(Baz::class);
  211. ',
  212. ];
  213. yield [
  214. "<?php
  215. use Foo\\Bar\\{ClassA, ClassB, ClassC as C};
  216. use function Foo\\Bar\\{fn_a, fn_b, fn_c};
  217. use const Foo\\Bar\\{ConstA, ConstB, ConstC};
  218. echo 'Foo\\Bar\\ClassB';
  219. echo 'Foo\\Bar\\ClassC';
  220. ",
  221. '<?php
  222. use Foo\Bar\{ClassA, ClassB, ClassC as C};
  223. use function Foo\Bar\{fn_a, fn_b, fn_c};
  224. use const Foo\Bar\{ConstA, ConstB, ConstC};
  225. echo ClassB::class;
  226. echo C::class;
  227. ',
  228. "<?php
  229. namespace {
  230. var_dump('Foo');
  231. }
  232. namespace A {
  233. use B\\C;
  234. var_dump('B\\C');
  235. }
  236. namespace {
  237. var_dump('Bar\\Baz');
  238. }
  239. namespace B {
  240. use A\\C\\D;
  241. var_dump('A\\C\\D');
  242. }
  243. namespace {
  244. var_dump('Qux\\Quux');
  245. }
  246. ",
  247. '<?php
  248. namespace {
  249. var_dump(Foo::class);
  250. }
  251. namespace A {
  252. use B\C;
  253. var_dump(C::class);
  254. }
  255. namespace {
  256. var_dump(Bar\Baz::class);
  257. }
  258. namespace B {
  259. use A\C\D;
  260. var_dump(D::class);
  261. }
  262. namespace {
  263. var_dump(Qux\Quux::class);
  264. }
  265. ',
  266. ];
  267. }
  268. /**
  269. * @requires PHP <8.0
  270. */
  271. public function testFixPrePHP80(): void
  272. {
  273. $this->doTest(
  274. "<?php echo 'DateTime'
  275. # a
  276. /* b */?>
  277. ",
  278. '<?php echo \
  279. DateTime:: # a
  280. /* b */ class?>
  281. '
  282. );
  283. }
  284. /**
  285. * @requires PHP 8.0
  286. */
  287. public function testNotFixPHP8(): void
  288. {
  289. $this->doTest(
  290. "<?php
  291. echo 'Thing';
  292. echo \$thing::class;
  293. ",
  294. '<?php
  295. echo Thing::class;
  296. echo $thing::class;
  297. '
  298. );
  299. }
  300. }