PhpdocAlignFixerTest.php 28 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294
  1. <?php
  2. /*
  3. * This file is part of PHP CS Fixer.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. * Dariusz Rumiński <dariusz.ruminski@gmail.com>
  7. *
  8. * This source file is subject to the MIT license that is bundled
  9. * with this source code in the file LICENSE.
  10. */
  11. namespace PhpCsFixer\Tests\Fixer\Phpdoc;
  12. use PhpCsFixer\Tests\Test\AbstractFixerTestCase;
  13. use PhpCsFixer\WhitespacesFixerConfig;
  14. /**
  15. * @internal
  16. *
  17. * @covers \PhpCsFixer\Fixer\Phpdoc\PhpdocAlignFixer
  18. */
  19. final class PhpdocAlignFixerTest extends AbstractFixerTestCase
  20. {
  21. public function testFix()
  22. {
  23. $this->fixer->configure(['tags' => ['param']]);
  24. $expected = <<<'EOF'
  25. <?php
  26. /**
  27. * @param EngineInterface $templating
  28. * @param string $format
  29. * @param int $code An HTTP response status code
  30. * @param bool $debug
  31. * @param mixed &$reference A parameter passed by reference
  32. */
  33. EOF;
  34. $input = <<<'EOF'
  35. <?php
  36. /**
  37. * @param EngineInterface $templating
  38. * @param string $format
  39. * @param int $code An HTTP response status code
  40. * @param bool $debug
  41. * @param mixed &$reference A parameter passed by reference
  42. */
  43. EOF;
  44. $this->doTest($expected, $input);
  45. }
  46. public function testFixLeftAlign()
  47. {
  48. $this->fixer->configure(['tags' => ['param'], 'align' => 'left']);
  49. $expected = <<<'EOF'
  50. <?php
  51. /**
  52. * @param EngineInterface $templating
  53. * @param string $format
  54. * @param int $code An HTTP response status code
  55. * @param bool $debug
  56. * @param mixed &$reference A parameter passed by reference
  57. */
  58. EOF;
  59. $input = <<<'EOF'
  60. <?php
  61. /**
  62. * @param EngineInterface $templating
  63. * @param string $format
  64. * @param int $code An HTTP response status code
  65. * @param bool $debug
  66. * @param mixed &$reference A parameter passed by reference
  67. */
  68. EOF;
  69. $this->doTest($expected, $input);
  70. }
  71. public function testFixPartiallyUntyped()
  72. {
  73. $this->fixer->configure(['tags' => ['param']]);
  74. $expected = <<<'EOF'
  75. <?php
  76. /**
  77. * @param $id
  78. * @param $parentId
  79. * @param int $websiteId
  80. * @param $position
  81. * @param int[][] $siblings
  82. */
  83. EOF;
  84. $input = <<<'EOF'
  85. <?php
  86. /**
  87. * @param $id
  88. * @param $parentId
  89. * @param int $websiteId
  90. * @param $position
  91. * @param int[][] $siblings
  92. */
  93. EOF;
  94. $this->doTest($expected, $input);
  95. }
  96. public function testFixPartiallyUntypedLeftAlign()
  97. {
  98. $this->fixer->configure(['tags' => ['param'], 'align' => 'left']);
  99. $expected = <<<'EOF'
  100. <?php
  101. /**
  102. * @param $id
  103. * @param $parentId
  104. * @param int $websiteId
  105. * @param $position
  106. * @param int[][] $siblings
  107. */
  108. EOF;
  109. $input = <<<'EOF'
  110. <?php
  111. /**
  112. * @param $id
  113. * @param $parentId
  114. * @param int $websiteId
  115. * @param $position
  116. * @param int[][] $siblings
  117. */
  118. EOF;
  119. $this->doTest($expected, $input);
  120. }
  121. public function testFixMultiLineDesc()
  122. {
  123. $this->fixer->configure(['tags' => ['param', 'property', 'method']]);
  124. $expected = <<<'EOF'
  125. <?php
  126. /**
  127. * @param EngineInterface $templating
  128. * @param string $format
  129. * @param int $code An HTTP response status code
  130. * See constants
  131. * @param bool $debug
  132. * @param bool $debug See constants
  133. * See constants
  134. * @param mixed &$reference A parameter passed by reference
  135. * @property mixed $foo A foo
  136. * See constants
  137. * @method static baz($bop) A method that does a thing
  138. * It does it well
  139. */
  140. EOF;
  141. $input = <<<'EOF'
  142. <?php
  143. /**
  144. * @param EngineInterface $templating
  145. * @param string $format
  146. * @param int $code An HTTP response status code
  147. * See constants
  148. * @param bool $debug
  149. * @param bool $debug See constants
  150. * See constants
  151. * @param mixed &$reference A parameter passed by reference
  152. * @property mixed $foo A foo
  153. * See constants
  154. * @method static baz($bop) A method that does a thing
  155. * It does it well
  156. */
  157. EOF;
  158. $this->doTest($expected, $input);
  159. }
  160. public function testFixMultiLineDescLeftAlign()
  161. {
  162. $this->fixer->configure(['tags' => ['param', 'property', 'method'], 'align' => 'left']);
  163. $expected = <<<'EOF'
  164. <?php
  165. /**
  166. * @param EngineInterface $templating
  167. * @param string $format
  168. * @param int $code An HTTP response status code
  169. * See constants
  170. * @param bool $debug
  171. * @param bool $debug See constants
  172. * See constants
  173. * @param mixed &$reference A parameter passed by reference
  174. * @property mixed $foo A foo
  175. * See constants
  176. * @method static baz($bop) A method that does a thing
  177. * It does it well
  178. */
  179. EOF;
  180. $input = <<<'EOF'
  181. <?php
  182. /**
  183. * @param EngineInterface $templating
  184. * @param string $format
  185. * @param int $code An HTTP response status code
  186. * See constants
  187. * @param bool $debug
  188. * @param bool $debug See constants
  189. * See constants
  190. * @param mixed &$reference A parameter passed by reference
  191. * @property mixed $foo A foo
  192. * See constants
  193. * @method static baz($bop) A method that does a thing
  194. * It does it well
  195. */
  196. EOF;
  197. $this->doTest($expected, $input);
  198. }
  199. public function testFixMultiLineDescWithThrows()
  200. {
  201. $this->fixer->configure(['tags' => ['param', 'return', 'throws']]);
  202. $expected = <<<'EOF'
  203. <?php
  204. /**
  205. * @param EngineInterface $templating
  206. * @param string $format
  207. * @param int $code An HTTP response status code
  208. * See constants
  209. * @param bool $debug
  210. * @param bool $debug See constants
  211. * See constants
  212. * @param mixed &$reference A parameter passed by reference
  213. *
  214. * @return Foo description foo
  215. *
  216. * @throws Foo description foo
  217. * description foo
  218. */
  219. EOF;
  220. $input = <<<'EOF'
  221. <?php
  222. /**
  223. * @param EngineInterface $templating
  224. * @param string $format
  225. * @param int $code An HTTP response status code
  226. * See constants
  227. * @param bool $debug
  228. * @param bool $debug See constants
  229. * See constants
  230. * @param mixed &$reference A parameter passed by reference
  231. *
  232. * @return Foo description foo
  233. *
  234. * @throws Foo description foo
  235. * description foo
  236. */
  237. EOF;
  238. $this->doTest($expected, $input);
  239. }
  240. public function testFixMultiLineDescWithThrowsLeftAlign()
  241. {
  242. $this->fixer->configure(['tags' => ['param', 'return', 'throws'], 'align' => 'left']);
  243. $expected = <<<'EOF'
  244. <?php
  245. /**
  246. * @param EngineInterface $templating
  247. * @param string $format
  248. * @param int $code An HTTP response status code
  249. * See constants
  250. * @param bool $debug
  251. * @param bool $debug See constants
  252. * See constants
  253. * @param mixed &$reference A parameter passed by reference
  254. *
  255. * @return Foo description foo
  256. *
  257. * @throws Foo description foo
  258. * description foo
  259. */
  260. EOF;
  261. $input = <<<'EOF'
  262. <?php
  263. /**
  264. * @param EngineInterface $templating
  265. * @param string $format
  266. * @param int $code An HTTP response status code
  267. * See constants
  268. * @param bool $debug
  269. * @param bool $debug See constants
  270. * See constants
  271. * @param mixed &$reference A parameter passed by reference
  272. *
  273. * @return Foo description foo
  274. *
  275. * @throws Foo description foo
  276. * description foo
  277. */
  278. EOF;
  279. $this->doTest($expected, $input);
  280. }
  281. public function testFixWithReturnAndThrows()
  282. {
  283. $this->fixer->configure(['tags' => ['param', 'throws', 'return']]);
  284. $expected = <<<'EOF'
  285. <?php
  286. /**
  287. * @param EngineInterface $templating
  288. * @param mixed &$reference A parameter passed by reference
  289. * @throws Bar description bar
  290. * @return Foo description foo
  291. */
  292. EOF;
  293. $input = <<<'EOF'
  294. <?php
  295. /**
  296. * @param EngineInterface $templating
  297. * @param mixed &$reference A parameter passed by reference
  298. * @throws Bar description bar
  299. * @return Foo description foo
  300. */
  301. EOF;
  302. $this->doTest($expected, $input);
  303. }
  304. /**
  305. * References the issue #55 on github issue
  306. * https://github.com/FriendsOfPhp/PHP-CS-Fixer/issues/55.
  307. */
  308. public function testFixThreeParamsWithReturn()
  309. {
  310. $this->fixer->configure(['tags' => ['param', 'return']]);
  311. $expected = <<<'EOF'
  312. <?php
  313. /**
  314. * @param string $param1
  315. * @param bool $param2 lorem ipsum
  316. * @param string $param3 lorem ipsum
  317. * @return int lorem ipsum
  318. */
  319. EOF;
  320. $input = <<<'EOF'
  321. <?php
  322. /**
  323. * @param string $param1
  324. * @param bool $param2 lorem ipsum
  325. * @param string $param3 lorem ipsum
  326. * @return int lorem ipsum
  327. */
  328. EOF;
  329. $this->doTest($expected, $input);
  330. }
  331. public function testFixOnlyReturn()
  332. {
  333. $this->fixer->configure(['tags' => ['return']]);
  334. $expected = <<<'EOF'
  335. <?php
  336. /**
  337. * @return Foo description foo
  338. */
  339. EOF;
  340. $input = <<<'EOF'
  341. <?php
  342. /**
  343. * @return Foo description foo
  344. */
  345. EOF;
  346. $this->doTest($expected, $input);
  347. }
  348. public function testReturnWithDollarThis()
  349. {
  350. $this->fixer->configure(['tags' => ['param', 'return']]);
  351. $expected = <<<'EOF'
  352. <?php
  353. /**
  354. * @param Foo $foo
  355. * @return $this
  356. */
  357. EOF;
  358. $input = <<<'EOF'
  359. <?php
  360. /**
  361. * @param Foo $foo
  362. * @return $this
  363. */
  364. EOF;
  365. $this->doTest($expected, $input);
  366. }
  367. public function testCustomAnnotationsStayUntouched()
  368. {
  369. $this->fixer->configure(['tags' => ['return']]);
  370. $expected = <<<'EOF'
  371. <?php
  372. /**
  373. * @return string
  374. * @SuppressWarnings(PHPMD.UnusedLocalVariable)
  375. */
  376. EOF;
  377. $input = <<<'EOF'
  378. <?php
  379. /**
  380. * @return string
  381. * @SuppressWarnings(PHPMD.UnusedLocalVariable)
  382. */
  383. EOF;
  384. $this->doTest($expected, $input);
  385. }
  386. public function testCustomAnnotationsStayUntouched2()
  387. {
  388. $this->fixer->configure(['tags' => ['var']]);
  389. $expected = <<<'EOF'
  390. <?php
  391. class X
  392. {
  393. /**
  394. * @var Collection<Value>|Value[]
  395. * @ORM\ManyToMany(
  396. * targetEntity="\Dl\Component\DomainModel\Product\Value\AbstractValue",
  397. * inversedBy="externalAliases"
  398. * )
  399. */
  400. private $values;
  401. }
  402. EOF;
  403. $this->doTest($expected);
  404. }
  405. public function testFixTestLeftAlign()
  406. {
  407. $this->fixer->configure(['tags' => ['param'], 'align' => 'left']);
  408. $expected = <<<'EOF'
  409. <?php
  410. /**
  411. * @param int $a
  412. * @param string $b
  413. *
  414. * @dataProvider dataJobCreation
  415. */
  416. EOF;
  417. $input = <<<'EOF'
  418. <?php
  419. /**
  420. * @param int $a
  421. * @param string $b
  422. *
  423. * @dataProvider dataJobCreation
  424. */
  425. EOF;
  426. $this->doTest($expected, $input);
  427. }
  428. public function testFixTest()
  429. {
  430. $this->fixer->configure(['tags' => ['param']]);
  431. $expected = <<<'EOF'
  432. <?php
  433. /**
  434. * @param int $a
  435. * @param string|null $b
  436. *
  437. * @dataProvider dataJobCreation
  438. */
  439. EOF;
  440. $input = <<<'EOF'
  441. <?php
  442. /**
  443. * @param int $a
  444. * @param string|null $b
  445. *
  446. * @dataProvider dataJobCreation
  447. */
  448. EOF;
  449. $this->doTest($expected, $input);
  450. }
  451. public function testFixWithVar()
  452. {
  453. $this->fixer->configure(['tags' => ['var']]);
  454. $expected = <<<'EOF'
  455. <?php
  456. /**
  457. * @var Type
  458. */
  459. EOF;
  460. $input = <<<'EOF'
  461. <?php
  462. /**
  463. * @var Type
  464. */
  465. EOF;
  466. $this->doTest($expected, $input);
  467. }
  468. public function testFixWithType()
  469. {
  470. $this->fixer->configure(['tags' => ['type']]);
  471. $expected = <<<'EOF'
  472. <?php
  473. /**
  474. * @type Type
  475. */
  476. EOF;
  477. $input = <<<'EOF'
  478. <?php
  479. /**
  480. * @type Type
  481. */
  482. EOF;
  483. $this->doTest($expected, $input);
  484. }
  485. public function testFixWithVarAndDescription()
  486. {
  487. $this->fixer->configure(['tags' => ['var']]);
  488. $expected = <<<'EOF'
  489. <?php
  490. /**
  491. * This is a variable.
  492. *
  493. * @var Type
  494. */
  495. EOF;
  496. $input = <<<'EOF'
  497. <?php
  498. /**
  499. * This is a variable.
  500. *
  501. * @var Type
  502. */
  503. EOF;
  504. $this->doTest($expected, $input);
  505. }
  506. public function testFixWithVarAndInlineDescription()
  507. {
  508. $this->fixer->configure(['tags' => ['var']]);
  509. $expected = <<<'EOF'
  510. <?php
  511. /**
  512. * @var Type This is a variable.
  513. */
  514. EOF;
  515. $input = <<<'EOF'
  516. <?php
  517. /**
  518. * @var Type This is a variable.
  519. */
  520. EOF;
  521. $this->doTest($expected, $input);
  522. }
  523. public function testFixWithTypeAndInlineDescription()
  524. {
  525. $this->fixer->configure(['tags' => ['type']]);
  526. $expected = <<<'EOF'
  527. <?php
  528. /**
  529. * @type Type This is a variable.
  530. */
  531. EOF;
  532. $input = <<<'EOF'
  533. <?php
  534. /**
  535. * @type Type This is a variable.
  536. */
  537. EOF;
  538. $this->doTest($expected, $input);
  539. }
  540. public function testRetainsNewLineCharacters()
  541. {
  542. $this->fixer->configure(['tags' => ['param']]);
  543. // when we're not modifying a docblock, then line endings shouldn't change
  544. $this->doTest("<?php\r /**\r * @param Example Hello there!\r */\r");
  545. }
  546. public function testMalformedDocBlock()
  547. {
  548. $this->fixer->configure(['tags' => ['return']]);
  549. $input = <<<'EOF'
  550. <?php
  551. /**
  552. * @return string
  553. * */
  554. EOF;
  555. $this->doTest($input);
  556. }
  557. public function testDifferentIndentation()
  558. {
  559. $this->fixer->configure(['tags' => ['param', 'return']]);
  560. $expected = <<<'EOF'
  561. <?php
  562. /**
  563. * @param int $limit
  564. * @param string $more
  565. *
  566. * @return array
  567. */
  568. /**
  569. * @param int $limit
  570. * @param string $more
  571. *
  572. * @return array
  573. */
  574. EOF;
  575. $input = <<<'EOF'
  576. <?php
  577. /**
  578. * @param int $limit
  579. * @param string $more
  580. *
  581. * @return array
  582. */
  583. /**
  584. * @param int $limit
  585. * @param string $more
  586. *
  587. * @return array
  588. */
  589. EOF;
  590. $this->doTest($expected, $input);
  591. }
  592. public function testDifferentIndentationLeftAlign()
  593. {
  594. $this->fixer->configure(['tags' => ['param', 'return'], 'align' => 'left']);
  595. $expected = <<<'EOF'
  596. <?php
  597. /**
  598. * @param int $limit
  599. * @param string $more
  600. *
  601. * @return array
  602. */
  603. /**
  604. * @param int $limit
  605. * @param string $more
  606. *
  607. * @return array
  608. */
  609. EOF;
  610. $input = <<<'EOF'
  611. <?php
  612. /**
  613. * @param int $limit
  614. * @param string $more
  615. *
  616. * @return array
  617. */
  618. /**
  619. * @param int $limit
  620. * @param string $more
  621. *
  622. * @return array
  623. */
  624. EOF;
  625. $this->doTest($expected, $input);
  626. }
  627. /**
  628. * @param string $expected
  629. * @param string $input
  630. *
  631. * @dataProvider provideMessyWhitespacesCases
  632. */
  633. public function testMessyWhitespaces(array $config, $expected, $input, WhitespacesFixerConfig $whitespacesFixerConfig)
  634. {
  635. $this->fixer->configure($config);
  636. $this->fixer->setWhitespacesConfig($whitespacesFixerConfig);
  637. $this->doTest($expected, $input);
  638. }
  639. public function provideMessyWhitespacesCases()
  640. {
  641. return [
  642. [
  643. ['tags' => ['type']],
  644. "<?php\r\n\t/**\r\n\t * @type Type This is a variable.\r\n\t */",
  645. "<?php\r\n\t/**\r\n\t * @type Type This is a variable.\r\n\t */",
  646. new WhitespacesFixerConfig("\t", "\r\n"),
  647. ],
  648. [
  649. ['tags' => ['param', 'return']],
  650. "<?php\r\n/**\r\n * @param int \$limit\r\n * @param string \$more\r\n *\r\n * @return array\r\n */",
  651. "<?php\r\n/**\r\n * @param int \$limit\r\n * @param string \$more\r\n *\r\n * @return array\r\n */",
  652. new WhitespacesFixerConfig("\t", "\r\n"),
  653. ],
  654. [
  655. [],
  656. "<?php\r\n/**\r\n * @param int \$limit\r\n * @param string \$more\r\n *\r\n * @return array\r\n */",
  657. "<?php\r\n/**\r\n * @param int \$limit\r\n * @param string \$more\r\n *\r\n * @return array\r\n */",
  658. new WhitespacesFixerConfig("\t", "\r\n"),
  659. ],
  660. [
  661. [],
  662. "<?php\n/**\n * @param int \$a\n * @param int \$b\n * ABC\n */",
  663. "<?php\n/**\n * @param int \$a\n * @param int \$b\n * ABC\n */",
  664. new WhitespacesFixerConfig(' ', "\n"),
  665. ],
  666. [
  667. [],
  668. "<?php\r\n/**\r\n * @param int \$z\r\n * @param int \$b\r\n * XYZ\r\n */",
  669. "<?php\r\n/**\r\n * @param int \$z\r\n * @param int \$b\r\n * XYZ\r\n */",
  670. new WhitespacesFixerConfig(' ', "\r\n"),
  671. ],
  672. ];
  673. }
  674. public function testCanFixBadFormatted()
  675. {
  676. $this->fixer->configure(['tags' => ['var']]);
  677. $expected = "<?php\n /**\n * @var Foo */\n";
  678. $this->doTest($expected);
  679. }
  680. public function testFixUnicode()
  681. {
  682. $this->fixer->configure(['tags' => ['param', 'return']]);
  683. $expected = <<<'EOF'
  684. <?php
  685. /**
  686. * Method test.
  687. *
  688. * @param int $foobar Description
  689. * @param string $foo Description
  690. * @param mixed $bar Description word_with_ą
  691. * @param int|null $test Description
  692. */
  693. $a = 1;
  694. /**
  695. * @return string
  696. * @SuppressWarnings(PHPMD.UnusedLocalVariable) word_with_ą
  697. */
  698. $b = 1;
  699. EOF;
  700. $input = <<<'EOF'
  701. <?php
  702. /**
  703. * Method test.
  704. *
  705. * @param int $foobar Description
  706. * @param string $foo Description
  707. * @param mixed $bar Description word_with_ą
  708. * @param int|null $test Description
  709. */
  710. $a = 1;
  711. /**
  712. * @return string
  713. * @SuppressWarnings(PHPMD.UnusedLocalVariable) word_with_ą
  714. */
  715. $b = 1;
  716. EOF;
  717. $this->doTest($expected, $input);
  718. }
  719. public function testDoesNotAlignPropertyByDefault()
  720. {
  721. $expected = <<<'EOF'
  722. <?php
  723. /**
  724. * @param int $foobar Description
  725. * @return int
  726. * @throws Exception
  727. * @var FooBar
  728. * @type BarFoo
  729. * @property string $foo Hello World
  730. */
  731. EOF;
  732. $input = <<<'EOF'
  733. <?php
  734. /**
  735. * @param int $foobar Description
  736. * @return int
  737. * @throws Exception
  738. * @var FooBar
  739. * @type BarFoo
  740. * @property string $foo Hello World
  741. */
  742. EOF;
  743. $this->doTest($expected, $input);
  744. }
  745. public function testAlignsProperty()
  746. {
  747. $this->fixer->configure(['tags' => ['param', 'property', 'return', 'throws', 'type', 'var']]);
  748. $expected = <<<'EOF'
  749. <?php
  750. /**
  751. * @param int $foobar Description
  752. * @return int
  753. * @throws Exception
  754. * @var FooBar
  755. * @type BarFoo
  756. * @property string $foo Hello World
  757. */
  758. EOF;
  759. $input = <<<'EOF'
  760. <?php
  761. /**
  762. * @param int $foobar Description
  763. * @return int
  764. * @throws Exception
  765. * @var FooBar
  766. * @type BarFoo
  767. * @property string $foo Hello World
  768. */
  769. EOF;
  770. $this->doTest($expected, $input);
  771. }
  772. public function testDoesNotAlignMethodByDefault()
  773. {
  774. $expected = <<<'EOF'
  775. <?php
  776. /**
  777. * @param int $foobar Description
  778. * @return int
  779. * @throws Exception
  780. * @var FooBar
  781. * @type BarFoo
  782. * @method string foo(string $bar) Hello World
  783. */
  784. EOF;
  785. $input = <<<'EOF'
  786. <?php
  787. /**
  788. * @param int $foobar Description
  789. * @return int
  790. * @throws Exception
  791. * @var FooBar
  792. * @type BarFoo
  793. * @method string foo(string $bar) Hello World
  794. */
  795. EOF;
  796. $this->doTest($expected, $input);
  797. }
  798. public function testAlignsMethod()
  799. {
  800. $this->fixer->configure(['tags' => ['param', 'method', 'return', 'throws', 'type', 'var']]);
  801. $expected = <<<'EOF'
  802. <?php
  803. /**
  804. * @param int $foobar Description
  805. * @return int
  806. * @throws Exception
  807. * @var FooBar
  808. * @type BarFoo
  809. * @method int foo(string $bar, string ...$things, int &$baz) Description
  810. */
  811. EOF;
  812. $input = <<<'EOF'
  813. <?php
  814. /**
  815. * @param int $foobar Description
  816. * @return int
  817. * @throws Exception
  818. * @var FooBar
  819. * @type BarFoo
  820. * @method int foo(string $bar, string ...$things, int &$baz) Description
  821. */
  822. EOF;
  823. $this->doTest($expected, $input);
  824. }
  825. public function testAlignsMethodWithoutParameters()
  826. {
  827. $this->fixer->configure(['tags' => ['method', 'property']]);
  828. $expected = <<<'EOF'
  829. <?php
  830. /**
  831. * @property string $foo Desc
  832. * @method int foo() Description
  833. */
  834. EOF;
  835. $input = <<<'EOF'
  836. <?php
  837. /**
  838. * @property string $foo Desc
  839. * @method int foo() Description
  840. */
  841. EOF;
  842. $this->doTest($expected, $input);
  843. }
  844. public function testAlignsMethodWithoutParametersLeftAlign()
  845. {
  846. $this->fixer->configure(['tags' => ['method', 'property'], 'align' => 'left']);
  847. $expected = <<<'EOF'
  848. <?php
  849. /**
  850. * @property string $foo Desc
  851. * @method int foo() Description
  852. */
  853. EOF;
  854. $input = <<<'EOF'
  855. <?php
  856. /**
  857. * @property string $foo Desc
  858. * @method int foo() Description
  859. */
  860. EOF;
  861. $this->doTest($expected, $input);
  862. }
  863. public function testDoesNotFormatMethod()
  864. {
  865. $this->fixer->configure(['tags' => ['method']]);
  866. $input = <<<'EOF'
  867. <?php
  868. /**
  869. * @method int foo( string $bar ) Description
  870. */
  871. EOF;
  872. $this->doTest($input);
  873. }
  874. public function testAlignsMethodWithoutReturnType()
  875. {
  876. $this->fixer->configure(['tags' => ['method', 'property']]);
  877. $expected = <<<'EOF'
  878. <?php
  879. /**
  880. * @property string $foo Desc
  881. * @method int foo() Description
  882. * @method bar() Descrip
  883. */
  884. EOF;
  885. $input = <<<'EOF'
  886. <?php
  887. /**
  888. * @property string $foo Desc
  889. * @method int foo() Description
  890. * @method bar() Descrip
  891. */
  892. EOF;
  893. $this->doTest($expected, $input);
  894. }
  895. public function testAlignsMethodsWithoutReturnType()
  896. {
  897. $this->fixer->configure(['tags' => ['method']]);
  898. $expected = <<<'EOF'
  899. <?php
  900. /**
  901. * @method fooBaz() Description
  902. * @method bar(string $foo) Descrip
  903. */
  904. EOF;
  905. $input = <<<'EOF'
  906. <?php
  907. /**
  908. * @method fooBaz() Description
  909. * @method bar(string $foo) Descrip
  910. */
  911. EOF;
  912. $this->doTest($expected, $input);
  913. }
  914. public function testDoesNotAlignWithEmptyConfig()
  915. {
  916. $this->fixer->configure(['tags' => []]);
  917. $input = <<<'EOF'
  918. <?php
  919. /**
  920. * @param int $foobar Description
  921. * @return int
  922. * @throws Exception
  923. * @var FooBar
  924. * @type BarFoo
  925. * @property string $foo Hello World
  926. * @method int bar() Description
  927. */
  928. EOF;
  929. $this->doTest($input);
  930. }
  931. /**
  932. * @param string $expected
  933. * @param string $input
  934. *
  935. * @dataProvider provideVariadicCases
  936. */
  937. public function testVariadicParams(array $config, $expected, $input)
  938. {
  939. $this->fixer->configure($config);
  940. $this->doTest($expected, $input);
  941. }
  942. public function provideVariadicCases()
  943. {
  944. return [
  945. [
  946. ['tags' => ['param']],
  947. '<?php
  948. final class Sample
  949. {
  950. /**
  951. * @param int[] $a A
  952. * @param int &$b B
  953. * @param array ...$c C
  954. */
  955. public function sample2($a, &$b, ...$c)
  956. {
  957. }
  958. }
  959. ',
  960. '<?php
  961. final class Sample
  962. {
  963. /**
  964. * @param int[] $a A
  965. * @param int &$b B
  966. * @param array ...$c C
  967. */
  968. public function sample2($a, &$b, ...$c)
  969. {
  970. }
  971. }
  972. ',
  973. ],
  974. [
  975. ['tags' => ['param']],
  976. '<?php
  977. final class Sample
  978. {
  979. /**
  980. * @param int $a
  981. * @param int $b
  982. * @param array[] ...$c
  983. */
  984. public function sample2($a, $b, ...$c)
  985. {
  986. }
  987. }
  988. ',
  989. '<?php
  990. final class Sample
  991. {
  992. /**
  993. * @param int $a
  994. * @param int $b
  995. * @param array[] ...$c
  996. */
  997. public function sample2($a, $b, ...$c)
  998. {
  999. }
  1000. }
  1001. ',
  1002. ],
  1003. [
  1004. ['tags' => ['param'], 'align' => 'left'],
  1005. '<?php
  1006. final class Sample
  1007. {
  1008. /**
  1009. * @param int $a
  1010. * @param int $b
  1011. * @param array[] ...$c
  1012. */
  1013. public function sample2($a, $b, ...$c)
  1014. {
  1015. }
  1016. }
  1017. ',
  1018. '<?php
  1019. final class Sample
  1020. {
  1021. /**
  1022. * @param int $a
  1023. * @param int $b
  1024. * @param array[] ...$c
  1025. */
  1026. public function sample2($a, $b, ...$c)
  1027. {
  1028. }
  1029. }
  1030. ',
  1031. ],
  1032. [
  1033. ['tags' => ['property', 'property-read', 'property-write']],
  1034. '<?php
  1035. /**
  1036. * @property string $myMagicProperty
  1037. * @property-read string $myMagicReadyProperty
  1038. * @property-write string $myMagicWriteProperty
  1039. */
  1040. class Foo {}
  1041. ',
  1042. '<?php
  1043. /**
  1044. * @property string $myMagicProperty
  1045. * @property-read string $myMagicReadyProperty
  1046. * @property-write string $myMagicWriteProperty
  1047. */
  1048. class Foo {}
  1049. ',
  1050. ],
  1051. ];
  1052. }
  1053. /**
  1054. * @param string $input
  1055. *
  1056. * @dataProvider provideInvalidPhpdocCases
  1057. */
  1058. public function testInvalidPhpdocsAreUnchanged(array $config, $input)
  1059. {
  1060. $this->fixer->configure($config);
  1061. $this->doTest($input);
  1062. }
  1063. public function provideInvalidPhpdocCases()
  1064. {
  1065. return [
  1066. [
  1067. ['tags' => ['param', 'return', 'throws', 'type', 'var']],
  1068. '<?php
  1069. /**
  1070. * @ Security("is_granted(\'CANCEL\', giftCard)")
  1071. */
  1072. ',
  1073. ],
  1074. [
  1075. ['tags' => ['param', 'return', 'throws', 'type', 'var', 'method']],
  1076. '<?php
  1077. /**
  1078. * @ Security("is_granted(\'CANCEL\', giftCard)")
  1079. */
  1080. ',
  1081. ],
  1082. [
  1083. ['tags' => ['param', 'return', 'throws', 'type', 'var']],
  1084. '<?php
  1085. /**
  1086. * @ Security("is_granted(\'CANCEL\', giftCard)")
  1087. * @ foo bar
  1088. * @ foo
  1089. */
  1090. ',
  1091. ],
  1092. ];
  1093. }
  1094. }