DoctrineAnnotationBracesFixerTest.php 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598
  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\DoctrineAnnotation;
  13. use PhpCsFixer\Tests\AbstractDoctrineAnnotationFixerTestCase;
  14. /**
  15. * @internal
  16. *
  17. * @covers \PhpCsFixer\AbstractDoctrineAnnotationFixer
  18. * @covers \PhpCsFixer\Doctrine\Annotation\DocLexer
  19. * @covers \PhpCsFixer\Fixer\DoctrineAnnotation\DoctrineAnnotationBracesFixer
  20. *
  21. * @extends AbstractDoctrineAnnotationFixerTestCase<\PhpCsFixer\Fixer\DoctrineAnnotation\DoctrineAnnotationBracesFixer>
  22. *
  23. * @phpstan-import-type _AutogeneratedInputConfiguration from \PhpCsFixer\Fixer\DoctrineAnnotation\DoctrineAnnotationBracesFixer
  24. */
  25. final class DoctrineAnnotationBracesFixerTest extends AbstractDoctrineAnnotationFixerTestCase
  26. {
  27. /**
  28. * @dataProvider provideFixWithBracesCases
  29. */
  30. public function testFixWithBraces(string $expected, ?string $input = null): void
  31. {
  32. $this->fixer->configure(['syntax' => 'with_braces']);
  33. $this->doTest($expected, $input);
  34. }
  35. /**
  36. * @return iterable<array{0: string, 1?: null|string}>
  37. */
  38. public static function provideFixWithBracesCases(): iterable
  39. {
  40. yield from self::createTestCases([
  41. ['
  42. /**
  43. * @Foo()
  44. */'],
  45. ['
  46. /**
  47. * @Foo ()
  48. */'],
  49. ['
  50. /**
  51. * @Foo
  52. * (
  53. * )
  54. */'],
  55. ['
  56. /**
  57. * Foo.
  58. *
  59. * @author John Doe
  60. *
  61. * @Foo()
  62. */', '
  63. /**
  64. * Foo.
  65. *
  66. * @author John Doe
  67. *
  68. * @Foo
  69. */'],
  70. [
  71. '/** @Foo() */',
  72. '/** @Foo */',
  73. ],
  74. ['
  75. /**
  76. * @Foo(@Bar())
  77. */', '
  78. /**
  79. * @Foo(@Bar)
  80. */'],
  81. ['
  82. /**
  83. * @Foo(
  84. * @Bar()
  85. * )
  86. */', '
  87. /**
  88. * @Foo(
  89. * @Bar
  90. * )
  91. */'],
  92. ['
  93. /**
  94. * @Foo(
  95. * @Bar(),
  96. * "baz"
  97. * )
  98. */', '
  99. /**
  100. * @Foo(
  101. * @Bar,
  102. * "baz"
  103. * )
  104. */'],
  105. ['
  106. /**
  107. * @Foo(
  108. * @Bar\Baz()
  109. * )
  110. */', '
  111. /**
  112. * @Foo(
  113. * @Bar\Baz
  114. * )
  115. */'],
  116. ['
  117. /**
  118. * @Foo() @Bar\Baz()
  119. */', '
  120. /**
  121. * @Foo @Bar\Baz
  122. */'],
  123. ['
  124. /**
  125. * @Foo("@Bar")
  126. */'],
  127. ['
  128. /**
  129. * Description with a single " character.
  130. *
  131. * @Foo("string "" with inner quote")
  132. *
  133. * @param mixed description with a single " character.
  134. */'],
  135. ['
  136. /**
  137. * @Foo(@Bar
  138. */'],
  139. ['
  140. /**
  141. * @Foo())@Bar)
  142. */', '
  143. /**
  144. * @Foo)@Bar)
  145. */'],
  146. ['
  147. /**
  148. * See {@link https://help Help} or {@see BarClass} for details.
  149. */'],
  150. ['
  151. /**
  152. * @var int
  153. */'],
  154. ['
  155. /**
  156. * // PHPDocumentor 1
  157. * @abstract
  158. * @access
  159. * @code
  160. * @deprec
  161. * @encode
  162. * @exception
  163. * @final
  164. * @ingroup
  165. * @inheritdoc
  166. * @inheritDoc
  167. * @magic
  168. * @name
  169. * @toc
  170. * @tutorial
  171. * @private
  172. * @static
  173. * @staticvar
  174. * @staticVar
  175. * @throw
  176. *
  177. * // PHPDocumentor 2
  178. * @api
  179. * @author
  180. * @category
  181. * @copyright
  182. * @deprecated
  183. * @example
  184. * @filesource
  185. * @global
  186. * @ignore
  187. * @internal
  188. * @license
  189. * @link
  190. * @method
  191. * @package
  192. * @param
  193. * @property
  194. * @property-read
  195. * @property-write
  196. * @return
  197. * @see
  198. * @since
  199. * @source
  200. * @subpackage
  201. * @throws
  202. * @todo
  203. * @TODO
  204. * @usedBy
  205. * @uses
  206. * @var
  207. * @version
  208. *
  209. * // PHPUnit
  210. * @after
  211. * @afterClass
  212. * @backupGlobals
  213. * @backupStaticAttributes
  214. * @before
  215. * @beforeClass
  216. * @codeCoverageIgnore
  217. * @codeCoverageIgnoreStart
  218. * @codeCoverageIgnoreEnd
  219. * @covers
  220. * @coversDefaultClass
  221. * @coversNothing
  222. * @dataProvider
  223. * @depends
  224. * @expectedException
  225. * @expectedExceptionCode
  226. * @expectedExceptionMessage
  227. * @expectedExceptionMessageRegExp
  228. * @group
  229. * @large
  230. * @medium
  231. * @preserveGlobalState
  232. * @requires
  233. * @runTestsInSeparateProcesses
  234. * @runInSeparateProcess
  235. * @small
  236. * @test
  237. * @testdox
  238. * @ticket
  239. * @uses
  240. *
  241. * // PHPCheckStyle
  242. * @SuppressWarnings
  243. *
  244. * // PHPStorm
  245. * @noinspection
  246. *
  247. * // PEAR
  248. * @package_version
  249. *
  250. * // PlantUML
  251. * @enduml
  252. * @startuml
  253. *
  254. * // Psalm
  255. * @psalm
  256. * @psalm-param
  257. *
  258. * // PHPStan
  259. * @phpstan
  260. * @phpstan-param
  261. *
  262. * // other
  263. * @fix
  264. * @FIXME
  265. * @fixme
  266. * @fixme: foo
  267. * @override
  268. * @todo: foo
  269. */'],
  270. ]);
  271. yield [
  272. '<?php
  273. /**
  274. * @see \User getId()
  275. */
  276. ',
  277. ];
  278. }
  279. /**
  280. * @dataProvider provideFixWithoutBracesCases
  281. */
  282. public function testFixWithoutBraces(string $expected, ?string $input = null): void
  283. {
  284. $this->doTest($expected, $input);
  285. $this->fixer->configure(['syntax' => 'without_braces']);
  286. $this->doTest($expected, $input);
  287. }
  288. /**
  289. * @return iterable<array{0: string, 1?: null|string}>
  290. */
  291. public static function provideFixWithoutBracesCases(): iterable
  292. {
  293. yield from self::createTestCases([
  294. ['
  295. /**
  296. * Foo.
  297. *
  298. * @author John Doe
  299. *
  300. * @Baz\Bar
  301. */', '
  302. /**
  303. * Foo.
  304. *
  305. * @author John Doe
  306. *
  307. * @Baz\Bar ( )
  308. */'],
  309. [
  310. '/** @Foo */',
  311. '/** @Foo () */',
  312. ],
  313. ['
  314. /**
  315. * @Foo("bar")
  316. */'],
  317. ['
  318. /**
  319. * @Foo
  320. */', '
  321. /**
  322. * @Foo
  323. * (
  324. * )
  325. */'],
  326. ['
  327. /**
  328. * @Foo(@Bar)
  329. */', '
  330. /**
  331. * @Foo(@Bar())
  332. */'],
  333. ['
  334. /**
  335. * @Foo(
  336. * @Bar
  337. * )
  338. */', '
  339. /**
  340. * @Foo(
  341. * @Bar()
  342. * )
  343. */'],
  344. ['
  345. /**
  346. * @Foo(
  347. * @Bar,
  348. * "baz"
  349. * )
  350. */', '
  351. /**
  352. * @Foo(
  353. * @Bar(),
  354. * "baz"
  355. * )
  356. */'],
  357. ['
  358. /**
  359. * @Foo(
  360. * @Bar\Baz
  361. * )
  362. */', '
  363. /**
  364. * @Foo(
  365. * @Bar\Baz()
  366. * )
  367. */'],
  368. ['
  369. /**
  370. * @Foo @Bar\Baz
  371. */', '
  372. /**
  373. * @Foo() @Bar\Baz()
  374. */'],
  375. ['
  376. /**
  377. * @\Foo @\Bar\Baz
  378. */', '
  379. /**
  380. * @\Foo() @\Bar\Baz()
  381. */'],
  382. ['
  383. /**
  384. * @Foo("@Bar()")
  385. */'],
  386. ['
  387. /**
  388. * Description with a single " character.
  389. *
  390. * @Foo("string "" with inner quote")
  391. *
  392. * @param mixed description with a single " character.
  393. */'],
  394. ['
  395. /**
  396. * @Foo(
  397. */'],
  398. ['
  399. /**
  400. * @Foo)
  401. */'],
  402. ['
  403. /**
  404. * @Foo(@Bar()
  405. */'],
  406. ['
  407. /**
  408. * @Foo
  409. * @Bar
  410. * @Baz
  411. */', '
  412. /**
  413. * @Foo()
  414. * @Bar()
  415. * @Baz()
  416. */'],
  417. ['
  418. /**
  419. * @FIXME ()
  420. * @fixme ()
  421. * @TODO ()
  422. * @todo ()
  423. */'],
  424. ['
  425. /**
  426. * // PHPDocumentor 1
  427. * @abstract()
  428. * @access()
  429. * @code()
  430. * @deprec()
  431. * @encode()
  432. * @exception()
  433. * @final()
  434. * @ingroup()
  435. * @inheritdoc()
  436. * @inheritDoc()
  437. * @magic()
  438. * @name()
  439. * @toc()
  440. * @tutorial()
  441. * @private()
  442. * @static()
  443. * @staticvar()
  444. * @staticVar()
  445. * @throw()
  446. *
  447. * // PHPDocumentor 2
  448. * @api()
  449. * @author()
  450. * @category()
  451. * @copyright()
  452. * @deprecated()
  453. * @example()
  454. * @filesource()
  455. * @global()
  456. * @ignore()
  457. * @internal()
  458. * @license()
  459. * @link()
  460. * @method()
  461. * @package()
  462. * @param()
  463. * @property()
  464. * @property-read()
  465. * @property-write()
  466. * @return()
  467. * @see()
  468. * @since()
  469. * @source()
  470. * @subpackage()
  471. * @throws()
  472. * @todo()
  473. * @TODO()
  474. * @usedBy()
  475. * @uses()
  476. * @var()
  477. * @version()
  478. *
  479. * // PHPUnit
  480. * @after()
  481. * @afterClass()
  482. * @backupGlobals()
  483. * @backupStaticAttributes()
  484. * @before()
  485. * @beforeClass()
  486. * @codeCoverageIgnore()
  487. * @codeCoverageIgnoreStart()
  488. * @codeCoverageIgnoreEnd()
  489. * @covers()
  490. * @coversDefaultClass()
  491. * @coversNothing()
  492. * @dataProvider()
  493. * @depends()
  494. * @expectedException()
  495. * @expectedExceptionCode()
  496. * @expectedExceptionMessage()
  497. * @expectedExceptionMessageRegExp()
  498. * @group()
  499. * @large()
  500. * @medium()
  501. * @preserveGlobalState()
  502. * @requires()
  503. * @runTestsInSeparateProcesses()
  504. * @runInSeparateProcess()
  505. * @small()
  506. * @test()
  507. * @testdox()
  508. * @ticket()
  509. * @uses()
  510. *
  511. * // PHPCheckStyle
  512. * @SuppressWarnings()
  513. *
  514. * // PHPStorm
  515. * @noinspection()
  516. *
  517. * // PEAR
  518. * @package_version()
  519. *
  520. * // PlantUML
  521. * @enduml()
  522. * @startuml()
  523. *
  524. * // Psalm
  525. * @psalm()
  526. * @psalm-param()
  527. *
  528. * // PHPStan
  529. * @phpstan()
  530. * @psalm-param()
  531. *
  532. *
  533. * // other
  534. * @fix()
  535. * @FIXME()
  536. * @fixme()
  537. * @fixme: foo()
  538. * @override()
  539. * @todo: foo()
  540. */'],
  541. ]);
  542. yield [
  543. '<?php
  544. /**
  545. * @see \User getId()
  546. */
  547. ',
  548. ];
  549. }
  550. /**
  551. * @dataProvider provideFix82Cases
  552. *
  553. * @requires PHP 8.2
  554. */
  555. public function testFix82(string $expected, string $input): void
  556. {
  557. $this->doTest($expected, $input);
  558. }
  559. /**
  560. * @return iterable<array{string, string}>
  561. */
  562. public static function provideFix82Cases(): iterable
  563. {
  564. yield [
  565. '<?php
  566. /**
  567. * @author John Doe
  568. *
  569. * @Baz\Bar
  570. */
  571. readonly class FooClass{}',
  572. '<?php
  573. /**
  574. * @author John Doe
  575. *
  576. * @Baz\Bar ( )
  577. */
  578. readonly class FooClass{}',
  579. ];
  580. }
  581. }