FinalInternalClassFixerTest.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632
  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 array<string, string> $configuration
  24. *
  25. * @dataProvider provideFixCases
  26. */
  27. public function testFix(string $expected, ?string $input = null, array $configuration = []): void
  28. {
  29. $this->fixer->configure($configuration);
  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. yield [
  156. "<?php\n/** @CUSTOM */final class A{}",
  157. "<?php\n/** @CUSTOM */class A{}",
  158. [
  159. 'include' => ['@Custom'],
  160. ],
  161. ];
  162. yield [
  163. '<?php
  164. /**
  165. * @CUSTOM
  166. * @abc
  167. */
  168. final class A{}
  169. /**
  170. * @CUSTOM
  171. */
  172. final class B{}
  173. ',
  174. '<?php
  175. /**
  176. * @CUSTOM
  177. * @abc
  178. */
  179. class A{}
  180. /**
  181. * @CUSTOM
  182. */
  183. class B{}
  184. ',
  185. [
  186. 'include' => ['@Custom', '@abc'],
  187. ],
  188. ];
  189. yield [
  190. '<?php
  191. /**
  192. * @CUSTOM
  193. * @internal
  194. */
  195. final class A{}
  196. /**
  197. * @CUSTOM
  198. * @internal
  199. * @other
  200. */
  201. final class B{}
  202. /**
  203. * @CUSTOM
  204. * @internal
  205. * @not-fix
  206. */
  207. class C{}
  208. ',
  209. '<?php
  210. /**
  211. * @CUSTOM
  212. * @internal
  213. */
  214. class A{}
  215. /**
  216. * @CUSTOM
  217. * @internal
  218. * @other
  219. */
  220. class B{}
  221. /**
  222. * @CUSTOM
  223. * @internal
  224. * @not-fix
  225. */
  226. class C{}
  227. ',
  228. [
  229. 'include' => ['@Custom', '@internal'],
  230. 'exclude' => ['@not-fix'],
  231. ],
  232. ];
  233. yield [
  234. '<?php
  235. /**
  236. * @internal
  237. */
  238. final class A{}
  239. /**
  240. * @abc
  241. */
  242. class B{}
  243. ',
  244. '<?php
  245. /**
  246. * @internal
  247. */
  248. class A{}
  249. /**
  250. * @abc
  251. */
  252. class B{}
  253. ',
  254. [
  255. 'exclude' => ['abc'],
  256. ],
  257. ];
  258. yield [
  259. '<?php final class A{}',
  260. '<?php class A{}',
  261. ['consider_absent_docblock_as_internal_class' => true],
  262. ];
  263. yield 'class with annotation with matching include and partial matching exclude' => [
  264. '<?php
  265. /** @HelloWorld */
  266. final class Foo {}
  267. ',
  268. '<?php
  269. /** @HelloWorld */
  270. class Foo {}
  271. ',
  272. [
  273. 'include' => ['HelloWorld'],
  274. 'exclude' => ['Hello'],
  275. ],
  276. ];
  277. yield [
  278. '<?php
  279. /** @internal */
  280. $a = new class (){};',
  281. ];
  282. yield [
  283. '<?php
  284. /** @internal */
  285. $a = new class{};',
  286. ];
  287. yield [
  288. '<?php $object = new /**/ class(){};',
  289. ];
  290. }
  291. /**
  292. * @group legacy
  293. *
  294. * @param array<mixed> $config
  295. *
  296. * @dataProvider provideInvalidConfigurationCases
  297. */
  298. public function testInvalidConfiguration(array $config, string $exceptionExpression, ?string $deprecationMessage = null): void
  299. {
  300. $this->expectException(InvalidFixerConfigurationException::class);
  301. $this->expectExceptionMessageMatches($exceptionExpression);
  302. if (null !== $deprecationMessage) {
  303. $this->expectDeprecation($deprecationMessage);
  304. }
  305. $this->fixer->configure($config);
  306. }
  307. /**
  308. * @return iterable<array{array<mixed>, string, 2?: string}>
  309. */
  310. public static function provideInvalidConfigurationCases(): iterable
  311. {
  312. yield 'same annotation in both lists' => [
  313. [
  314. 'include' => ['@internal123', 'a'],
  315. 'exclude' => ['@internal123', 'b'],
  316. ],
  317. sprintf('#^%s$#', preg_quote('[final_internal_class] Annotation cannot be used in both "include" and "exclude" list, got duplicates: "internal123".', '#')),
  318. ];
  319. yield 'both new and old include set' => [
  320. [
  321. 'annotation_include' => ['@internal', 'a'],
  322. 'include' => ['@internal', 'b'],
  323. ],
  324. sprintf('#^%s$#', preg_quote('[final_internal_class] Configuration cannot contain deprecated option "annotation_include" and new option "include".', '#')),
  325. '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.',
  326. ];
  327. yield 'both new and old exclude set' => [
  328. [
  329. 'annotation_exclude' => ['@internal', 'a'],
  330. 'exclude' => ['@internal', 'b'],
  331. ],
  332. sprintf('#^%s$#', preg_quote('[final_internal_class] Configuration cannot contain deprecated option "annotation_exclude" and new option "exclude".', '#')),
  333. '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.',
  334. ];
  335. }
  336. /**
  337. * @param array<string, list<string>> $config
  338. *
  339. * @dataProvider provideFix80Cases
  340. *
  341. * @requires PHP 8.0
  342. */
  343. public function testFix80(string $expected, ?string $input, array $config): void
  344. {
  345. $this->fixer->configure($config);
  346. $this->doTest($expected, $input);
  347. }
  348. /**
  349. * @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>}}>
  350. */
  351. public static function provideFix80Cases(): iterable
  352. {
  353. yield 'multiple attributes, all configured as not to fix' => [
  354. '<?php
  355. #[X]
  356. #[A]
  357. class Foo {}',
  358. null,
  359. ['exclude' => ['a', 'X']],
  360. ];
  361. yield 'multiple attributes, one configured as to fix, one as not to fix' => [
  362. '<?php
  363. #[Internal]
  364. #[A]
  365. class Foo {}',
  366. null,
  367. [
  368. 'include' => ['internal'],
  369. 'exclude' => ['A'],
  370. ],
  371. ];
  372. yield 'multiple attributes, one configured as to fix' => [
  373. '<?php
  374. #[Internal]
  375. #[A]
  376. final class Foo {}',
  377. '<?php
  378. #[Internal]
  379. #[A]
  380. class Foo {}',
  381. ['include' => ['internal']],
  382. ];
  383. yield 'single attribute configured as to fix' => [
  384. '<?php
  385. #[Internal]
  386. final class Foo {}',
  387. '<?php
  388. #[Internal]
  389. class Foo {}',
  390. ['include' => ['internal']],
  391. ];
  392. yield 'class that should be ignored as it has an attribute not included with absent docblock as true' => [
  393. '<?php
  394. #[StandWithUkraine]
  395. class Foo {}',
  396. null,
  397. ['consider_absent_docblock_as_internal_class' => true],
  398. ];
  399. yield 'mixed bag of cases' => [
  400. '<?php
  401. #[Entity(repositoryClass: PostRepository::class)]
  402. class User
  403. {}
  404. #[ORM\Entity]
  405. #[Index(name: "category_idx", columns: ["category"])]
  406. final class Article
  407. {}
  408. #[A]
  409. class ArticleB
  410. {}
  411. #[B]
  412. final class Foo {}
  413. #[C]
  414. class FooX {}
  415. $object1 = new #[ExampleAttribute] class(){};
  416. $object2 = new /* */ class(){};
  417. $object3 = new #[B] #[ExampleAttribute] class(){};
  418. /**
  419. * @B
  420. */
  421. final class PhpDocClass{}
  422. ',
  423. '<?php
  424. #[Entity(repositoryClass: PostRepository::class)]
  425. class User
  426. {}
  427. #[ORM\Entity]
  428. #[Index(name: "category_idx", columns: ["category"])]
  429. class Article
  430. {}
  431. #[A]
  432. class ArticleB
  433. {}
  434. #[B]
  435. class Foo {}
  436. #[C]
  437. class FooX {}
  438. $object1 = new #[ExampleAttribute] class(){};
  439. $object2 = new /* */ class(){};
  440. $object3 = new #[B] #[ExampleAttribute] class(){};
  441. /**
  442. * @B
  443. */
  444. class PhpDocClass{}
  445. ',
  446. [
  447. 'exclude' => ['Entity', 'A'],
  448. 'include' => ['orm\entity', 'B'],
  449. ],
  450. ];
  451. yield 'multiple classes, first configured with attribute, second without attribute' => [
  452. '<?php
  453. #[Internal]
  454. final class Foo {}
  455. class Bar {}',
  456. '<?php
  457. #[Internal]
  458. class Foo {}
  459. class Bar {}',
  460. ['include' => ['internal']],
  461. ];
  462. yield 'multiple classes, first configured without attribute, second with attribute' => [
  463. '<?php
  464. class Foo {}
  465. #[Internal]
  466. final class Bar {}',
  467. '<?php
  468. class Foo {}
  469. #[Internal]
  470. class Bar {}',
  471. ['include' => ['internal']],
  472. ];
  473. yield 'include by attribute, but exclude by doc' => [
  474. '<?php
  475. /** @final */
  476. #[A]
  477. class Foo {}',
  478. null,
  479. [
  480. 'exclude' => ['final'],
  481. 'include' => ['A'],
  482. ],
  483. ];
  484. yield 'include by phpDoc, but exclude by attribute' => [
  485. '<?php
  486. /** @a */
  487. #[Internal]
  488. class Foo {}',
  489. null,
  490. [
  491. 'exclude' => ['Internal'],
  492. 'include' => ['A'],
  493. ],
  494. ];
  495. yield 'comment between attributes' => [
  496. '<?php
  497. #[A]
  498. /**
  499. * @B
  500. */
  501. #[C]
  502. final class Foo {}',
  503. '<?php
  504. #[A]
  505. /**
  506. * @B
  507. */
  508. #[C]
  509. class Foo {}',
  510. [
  511. 'include' => ['A', 'C'],
  512. ],
  513. ];
  514. }
  515. /**
  516. * @param array<string, mixed> $config
  517. *
  518. * @dataProvider provideFix82Cases
  519. *
  520. * @requires PHP 8.2
  521. */
  522. public function testFix82(string $expected, ?string $input, array $config): void
  523. {
  524. $this->fixer->configure($config);
  525. $this->doTest($expected, $input);
  526. }
  527. /**
  528. * @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>}}>
  529. */
  530. public static function provideFix82Cases(): iterable
  531. {
  532. yield 'readonly with enabled `consider_absent_docblock_as_internal_class`' => [
  533. '<?php readonly final class A{}',
  534. '<?php readonly class A{}',
  535. ['consider_absent_docblock_as_internal_class' => true],
  536. ];
  537. yield 'readonly with `internal` attribute and comment in-between' => [
  538. '<?php #[Internal] readonly /* comment */ final class A{}',
  539. '<?php #[Internal] readonly /* comment */ class A{}',
  540. ['consider_absent_docblock_as_internal_class' => true],
  541. ];
  542. }
  543. }