DoctrineAnnotationBracesFixerTest.php 7.9 KB

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