FinalInternalClassFixerTest.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656
  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\ConfigurationException\InvalidFixerConfigurationException;
  14. use PhpCsFixer\Tests\Test\AbstractFixerTestCase;
  15. /**
  16. * @internal
  17. *
  18. * @covers \PhpCsFixer\Fixer\ClassNotation\FinalInternalClassFixer
  19. */
  20. final class FinalInternalClassFixerTest extends AbstractFixerTestCase
  21. {
  22. /**
  23. * @param string $expected PHP source code
  24. * @param null|string $input PHP source code
  25. *
  26. * @dataProvider provideFixCases
  27. */
  28. public function testFix(string $expected, ?string $input = null): void
  29. {
  30. $this->doTest($expected, $input);
  31. }
  32. public static function provideFixCases(): iterable
  33. {
  34. $input = $expected = '<?php ';
  35. for ($i = 1; $i < 10; ++$i) {
  36. $input .= sprintf("/** @internal */\nclass class%d\n{\n}\n", $i);
  37. $expected .= sprintf("/** @internal */\nfinal class class%d\n{\n}\n", $i);
  38. }
  39. yield 'fix multiple classes' => [
  40. $expected,
  41. $input,
  42. ];
  43. yield [
  44. '<?php
  45. /** @internal */
  46. final class class1
  47. {
  48. }
  49. interface A {}
  50. trait B{}
  51. /** @internal */
  52. final class class2
  53. {
  54. }
  55. ',
  56. '<?php
  57. /** @internal */
  58. class class1
  59. {
  60. }
  61. interface A {}
  62. trait B{}
  63. /** @internal */
  64. class class2
  65. {
  66. }
  67. ',
  68. ];
  69. yield [
  70. '<?php
  71. /** @internal */
  72. final class class1
  73. {
  74. }
  75. /** @internal */
  76. final class class2
  77. {
  78. }
  79. /**
  80. * @internal
  81. * @final
  82. */
  83. class class3
  84. {
  85. }
  86. /**
  87. * @internal
  88. */
  89. abstract class class4 {}
  90. ',
  91. '<?php
  92. /** @internal */
  93. final class class1
  94. {
  95. }
  96. /** @internal */
  97. class class2
  98. {
  99. }
  100. /**
  101. * @internal
  102. * @final
  103. */
  104. class class3
  105. {
  106. }
  107. /**
  108. * @internal
  109. */
  110. abstract class class4 {}
  111. ',
  112. ];
  113. yield [
  114. '<?php
  115. /**
  116. * @ annotation_with_space_after_at_sign
  117. */
  118. class A {}
  119. ',
  120. ];
  121. yield 'indent before `class`' => [
  122. '<?php /** @internal */
  123. final class class1
  124. {
  125. }',
  126. '<?php /** @internal */
  127. class class1
  128. {
  129. }',
  130. ];
  131. yield 'multiple classes, first with internal annotation and second without internal annotation' => [
  132. '<?php
  133. /** @internal */
  134. final class Foo {}
  135. class Bar {}
  136. ',
  137. '<?php
  138. /** @internal */
  139. class Foo {}
  140. class Bar {}
  141. ',
  142. ];
  143. yield 'multiple classes, first without internal annotation and second with internal annotation' => [
  144. '<?php
  145. class Foo {}
  146. /** @internal */
  147. final class Bar {}
  148. ',
  149. '<?php
  150. class Foo {}
  151. /** @internal */
  152. class Bar {}
  153. ',
  154. ];
  155. }
  156. /**
  157. * @param array<string, mixed> $config
  158. *
  159. * @dataProvider provideFixWithConfigCases
  160. */
  161. public function testFixWithConfig(string $expected, string $input, array $config): void
  162. {
  163. $this->fixer->configure($config);
  164. $this->doTest($expected, $input);
  165. }
  166. public static function provideFixWithConfigCases(): iterable
  167. {
  168. yield [
  169. "<?php\n/** @CUSTOM */final class A{}",
  170. "<?php\n/** @CUSTOM */class A{}",
  171. [
  172. 'include' => ['@Custom'],
  173. ],
  174. ];
  175. yield [
  176. '<?php
  177. /**
  178. * @CUSTOM
  179. * @abc
  180. */
  181. final class A{}
  182. /**
  183. * @CUSTOM
  184. */
  185. final class B{}
  186. ',
  187. '<?php
  188. /**
  189. * @CUSTOM
  190. * @abc
  191. */
  192. class A{}
  193. /**
  194. * @CUSTOM
  195. */
  196. class B{}
  197. ',
  198. [
  199. 'include' => ['@Custom', '@abc'],
  200. ],
  201. ];
  202. yield [
  203. '<?php
  204. /**
  205. * @CUSTOM
  206. * @internal
  207. */
  208. final class A{}
  209. /**
  210. * @CUSTOM
  211. * @internal
  212. * @other
  213. */
  214. final class B{}
  215. /**
  216. * @CUSTOM
  217. * @internal
  218. * @not-fix
  219. */
  220. class C{}
  221. ',
  222. '<?php
  223. /**
  224. * @CUSTOM
  225. * @internal
  226. */
  227. class A{}
  228. /**
  229. * @CUSTOM
  230. * @internal
  231. * @other
  232. */
  233. class B{}
  234. /**
  235. * @CUSTOM
  236. * @internal
  237. * @not-fix
  238. */
  239. class C{}
  240. ',
  241. [
  242. 'include' => ['@Custom', '@internal'],
  243. 'exclude' => ['@not-fix'],
  244. ],
  245. ];
  246. yield [
  247. '<?php
  248. /**
  249. * @internal
  250. */
  251. final class A{}
  252. /**
  253. * @abc
  254. */
  255. class B{}
  256. ',
  257. '<?php
  258. /**
  259. * @internal
  260. */
  261. class A{}
  262. /**
  263. * @abc
  264. */
  265. class B{}
  266. ',
  267. [
  268. 'exclude' => ['abc'],
  269. ],
  270. ];
  271. yield [
  272. '<?php final class A{}',
  273. '<?php class A{}',
  274. ['consider_absent_docblock_as_internal_class' => true],
  275. ];
  276. yield 'class with annotation with matching include and partial matching exclude' => [
  277. '<?php
  278. /** @HelloWorld */
  279. final class Foo {}
  280. ',
  281. '<?php
  282. /** @HelloWorld */
  283. class Foo {}
  284. ',
  285. [
  286. 'include' => ['HelloWorld'],
  287. 'exclude' => ['Hello'],
  288. ],
  289. ];
  290. }
  291. /**
  292. * @param string $expected PHP source code
  293. * @param null|string $input PHP source code
  294. *
  295. * @dataProvider provideAnonymousClassesCases
  296. */
  297. public function testAnonymousClasses(string $expected, ?string $input = null): void
  298. {
  299. $this->doTest($expected, $input);
  300. }
  301. /**
  302. * @return iterable<int|string, array{0: string, 1?: string}>
  303. */
  304. public static function provideAnonymousClassesCases(): iterable
  305. {
  306. yield [
  307. '<?php
  308. /** @internal */
  309. $a = new class (){};',
  310. ];
  311. yield [
  312. '<?php
  313. /** @internal */
  314. $a = new class{};',
  315. ];
  316. yield [
  317. '<?php $object = new /**/ class(){};',
  318. ];
  319. }
  320. public function testConfigureSameAnnotationInBothLists(): void
  321. {
  322. $this->expectException(InvalidFixerConfigurationException::class);
  323. $this->expectExceptionMessageMatches(
  324. sprintf('#^%s$#', preg_quote('[final_internal_class] Annotation cannot be used in both "include" and "exclude" list, got duplicates: "internal123".', '#'))
  325. );
  326. $this->fixer->configure([
  327. 'include' => ['@internal123', 'a'],
  328. 'exclude' => ['@internal123', 'b'],
  329. ]);
  330. }
  331. /**
  332. * @group legacy
  333. */
  334. public function testConfigureBothNewAndOldIncludeSet(): void
  335. {
  336. $this->expectException(InvalidFixerConfigurationException::class);
  337. $this->expectExceptionMessageMatches(sprintf('#^%s$#', preg_quote('[final_internal_class] Configuration cannot contain deprecated option "annotation_include" and new option "include".', '#')));
  338. $this->expectDeprecation('Option "annotation_include" for rule "final_internal_class" is deprecated and will be removed in version 4.0. Use "include" to configure PHPDoc annotations tags and attributes.');
  339. $this->fixer->configure([
  340. 'annotation_include' => ['@internal', 'a'],
  341. 'include' => ['@internal', 'b'],
  342. ]);
  343. }
  344. /**
  345. * @group legacy
  346. */
  347. public function testConfigureBothNewAndOldExcludeSet(): void
  348. {
  349. $this->expectException(InvalidFixerConfigurationException::class);
  350. $this->expectExceptionMessageMatches(sprintf('#^%s$#', preg_quote('[final_internal_class] Configuration cannot contain deprecated option "annotation_exclude" and new option "exclude".', '#')));
  351. $this->expectDeprecation('Option "annotation_exclude" for rule "final_internal_class" is deprecated and will be removed in version 4.0. Use "exclude" to configure PHPDoc annotations tags and attributes.');
  352. $this->fixer->configure([
  353. 'annotation_exclude' => ['@internal', 'a'],
  354. 'exclude' => ['@internal', 'b'],
  355. ]);
  356. }
  357. /**
  358. * @param array<string, list<string>> $config
  359. *
  360. * @dataProvider provideFix80Cases
  361. *
  362. * @requires PHP 8.0
  363. */
  364. public function testFix80(string $expected, ?string $input, array $config): void
  365. {
  366. $this->fixer->configure($config);
  367. $this->doTest($expected, $input);
  368. }
  369. /**
  370. * @return iterable<int|string, array{0: string, 1: null|string, 2: array{consider_absent_docblock_as_internal_class? : bool, exclude?: list<string>, include?: list<string>}}>
  371. */
  372. public static function provideFix80Cases(): iterable
  373. {
  374. yield 'multiple attributes, all configured as not to fix' => [
  375. '<?php
  376. #[X]
  377. #[A]
  378. class Foo {}',
  379. null,
  380. ['exclude' => ['a', 'X']],
  381. ];
  382. yield 'multiple attributes, one configured as to fix, one as not to fix' => [
  383. '<?php
  384. #[Internal]
  385. #[A]
  386. class Foo {}',
  387. null,
  388. [
  389. 'include' => ['internal'],
  390. 'exclude' => ['A'],
  391. ],
  392. ];
  393. yield 'multiple attributes, one configured as to fix' => [
  394. '<?php
  395. #[Internal]
  396. #[A]
  397. final class Foo {}',
  398. '<?php
  399. #[Internal]
  400. #[A]
  401. class Foo {}',
  402. ['include' => ['internal']],
  403. ];
  404. yield 'single attribute configured as to fix' => [
  405. '<?php
  406. #[Internal]
  407. final class Foo {}',
  408. '<?php
  409. #[Internal]
  410. class Foo {}',
  411. ['include' => ['internal']],
  412. ];
  413. yield 'class that should be ignored as it has an attribute not included with absent docblock as true' => [
  414. '<?php
  415. #[StandWithUkraine]
  416. class Foo {}',
  417. null,
  418. ['consider_absent_docblock_as_internal_class' => true],
  419. ];
  420. yield 'mixed bag of cases' => [
  421. '<?php
  422. #[Entity(repositoryClass: PostRepository::class)]
  423. class User
  424. {}
  425. #[ORM\Entity]
  426. #[Index(name: "category_idx", columns: ["category"])]
  427. final class Article
  428. {}
  429. #[A]
  430. class ArticleB
  431. {}
  432. #[B]
  433. final class Foo {}
  434. #[C]
  435. class FooX {}
  436. $object1 = new #[ExampleAttribute] class(){};
  437. $object2 = new /* */ class(){};
  438. $object3 = new #[B] #[ExampleAttribute] class(){};
  439. /**
  440. * @B
  441. */
  442. final class PhpDocClass{}
  443. ',
  444. '<?php
  445. #[Entity(repositoryClass: PostRepository::class)]
  446. class User
  447. {}
  448. #[ORM\Entity]
  449. #[Index(name: "category_idx", columns: ["category"])]
  450. class Article
  451. {}
  452. #[A]
  453. class ArticleB
  454. {}
  455. #[B]
  456. class Foo {}
  457. #[C]
  458. class FooX {}
  459. $object1 = new #[ExampleAttribute] class(){};
  460. $object2 = new /* */ class(){};
  461. $object3 = new #[B] #[ExampleAttribute] class(){};
  462. /**
  463. * @B
  464. */
  465. class PhpDocClass{}
  466. ',
  467. [
  468. 'exclude' => ['Entity', 'A'],
  469. 'include' => ['orm\entity', 'B'],
  470. ],
  471. ];
  472. yield 'multiple classes, first configured with attribute, second without attribute' => [
  473. '<?php
  474. #[Internal]
  475. final class Foo {}
  476. class Bar {}',
  477. '<?php
  478. #[Internal]
  479. class Foo {}
  480. class Bar {}',
  481. ['include' => ['internal']],
  482. ];
  483. yield 'multiple classes, first configured without attribute, second with attribute' => [
  484. '<?php
  485. class Foo {}
  486. #[Internal]
  487. final class Bar {}',
  488. '<?php
  489. class Foo {}
  490. #[Internal]
  491. class Bar {}',
  492. ['include' => ['internal']],
  493. ];
  494. yield 'include by attribute, but exclude by doc' => [
  495. '<?php
  496. /** @final */
  497. #[A]
  498. class Foo {}',
  499. null,
  500. [
  501. 'exclude' => ['final'],
  502. 'include' => ['A'],
  503. ],
  504. ];
  505. yield 'include by phpDoc, but exclude by attribute' => [
  506. '<?php
  507. /** @a */
  508. #[Internal]
  509. class Foo {}',
  510. null,
  511. [
  512. 'exclude' => ['Internal'],
  513. 'include' => ['A'],
  514. ],
  515. ];
  516. yield 'comment between attributes' => [
  517. '<?php
  518. #[A]
  519. /**
  520. * @B
  521. */
  522. #[C]
  523. final class Foo {}',
  524. '<?php
  525. #[A]
  526. /**
  527. * @B
  528. */
  529. #[C]
  530. class Foo {}',
  531. [
  532. 'include' => ['A', 'C'],
  533. ],
  534. ];
  535. }
  536. /**
  537. * @param array<string, mixed> $config
  538. *
  539. * @dataProvider provideFix82Cases
  540. *
  541. * @requires PHP 8.2
  542. */
  543. public function testFix82(string $expected, ?string $input, array $config): void
  544. {
  545. $this->fixer->configure($config);
  546. $this->doTest($expected, $input);
  547. }
  548. /**
  549. * @return iterable<int|string, array{0: string, 1: null|string, 2: array{consider_absent_docblock_as_internal_class? : bool, exclude?: list<string>, include?: list<string>}}>
  550. */
  551. public static function provideFix82Cases(): iterable
  552. {
  553. yield 'readonly with enabled `consider_absent_docblock_as_internal_class`' => [
  554. '<?php readonly final class A{}',
  555. '<?php readonly class A{}',
  556. ['consider_absent_docblock_as_internal_class' => true],
  557. ];
  558. yield 'readonly with `internal` attribute and comment in-between' => [
  559. '<?php #[Internal] readonly /* comment */ final class A{}',
  560. '<?php #[Internal] readonly /* comment */ class A{}',
  561. ['consider_absent_docblock_as_internal_class' => true],
  562. ];
  563. }
  564. }