PhpdocAlignFixerTest.php 28 KB

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