PhpdocLineSpanFixerTest.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922
  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\Phpdoc;
  13. use PhpCsFixer\Tests\Test\AbstractFixerTestCase;
  14. /**
  15. * @author Gert de Pagter <BackEndTea@gmail.com>
  16. *
  17. * @internal
  18. *
  19. * @covers \PhpCsFixer\Fixer\Phpdoc\PhpdocLineSpanFixer
  20. *
  21. * @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\Phpdoc\PhpdocLineSpanFixer>
  22. *
  23. * @phpstan-import-type _AutogeneratedInputConfiguration from \PhpCsFixer\Fixer\Phpdoc\PhpdocLineSpanFixer
  24. */
  25. final class PhpdocLineSpanFixerTest extends AbstractFixerTestCase
  26. {
  27. /**
  28. * @dataProvider provideFixCases
  29. *
  30. * @param _AutogeneratedInputConfiguration $config
  31. */
  32. public function testFix(string $expected, ?string $input = null, array $config = []): void
  33. {
  34. $this->fixer->configure($config);
  35. $this->doTest($expected, $input);
  36. }
  37. public static function provideFixCases(): iterable
  38. {
  39. yield 'It does not change doc blocks if not needed' => [
  40. '<?php
  41. class Foo
  42. {
  43. /**
  44. * Important
  45. */
  46. const FOO_BAR = "foobar";
  47. /**
  48. * @var bool
  49. */
  50. public $variable = true;
  51. /**
  52. * @var bool
  53. */
  54. private $var = false;
  55. /**
  56. * @return void
  57. */
  58. public function hello() {}
  59. }
  60. ',
  61. ];
  62. yield 'It does change doc blocks to multi by default' => [
  63. '<?php
  64. class Foo
  65. {
  66. /**
  67. * Important
  68. */
  69. const FOO_BAR = "foobar";
  70. /**
  71. * @var bool
  72. */
  73. public $variable = true;
  74. /**
  75. * @var bool
  76. */
  77. private $var = false;
  78. /**
  79. * @return void
  80. */
  81. public function hello() {}
  82. }
  83. ',
  84. '<?php
  85. class Foo
  86. {
  87. /** Important */
  88. const FOO_BAR = "foobar";
  89. /** @var bool */
  90. public $variable = true;
  91. /** @var bool */
  92. private $var = false;
  93. /** @return void */
  94. public function hello() {}
  95. }
  96. ',
  97. ];
  98. yield 'It does change doc blocks to single if configured to do so' => [
  99. '<?php
  100. class Foo
  101. {
  102. /** Important */
  103. const FOO_BAR = "foobar";
  104. /** @var bool */
  105. public $variable = true;
  106. /** @var bool */
  107. private $var = false;
  108. /** @return void */
  109. public function hello() {}
  110. }
  111. ',
  112. '<?php
  113. class Foo
  114. {
  115. /**
  116. * Important
  117. */
  118. const FOO_BAR = "foobar";
  119. /**
  120. * @var bool
  121. */
  122. public $variable = true;
  123. /**
  124. * @var bool
  125. */
  126. private $var = false;
  127. /**
  128. * @return void
  129. */
  130. public function hello() {}
  131. }
  132. ',
  133. [
  134. 'property' => 'single',
  135. 'const' => 'single',
  136. 'method' => 'single',
  137. ],
  138. ];
  139. yield 'It does change complicated doc blocks to single if configured to do so' => [
  140. '<?php
  141. class Foo
  142. {
  143. /** @var bool */
  144. public $variable1 = true;
  145. /** @var bool */
  146. public $variable2 = true;
  147. /** @Assert\File(mimeTypes={ "image/jpeg", "image/png" }) */
  148. public $imageFileObject;
  149. }
  150. ',
  151. '<?php
  152. class Foo
  153. {
  154. /**
  155. * @var bool */
  156. public $variable1 = true;
  157. /** @var bool
  158. */
  159. public $variable2 = true;
  160. /**
  161. * @Assert\File(mimeTypes={ "image/jpeg", "image/png" })
  162. */
  163. public $imageFileObject;
  164. }
  165. ',
  166. [
  167. 'property' => 'single',
  168. ],
  169. ];
  170. yield 'It does not changes doc blocks from single if configured to do so' => [
  171. '<?php
  172. class Foo
  173. {
  174. /** Important */
  175. const FOO_BAR = "foobar";
  176. /** @var bool */
  177. public $variable = true;
  178. /** @var bool */
  179. private $var = false;
  180. /** @return void */
  181. public function hello() {}
  182. }
  183. ',
  184. null,
  185. [
  186. 'property' => 'single',
  187. 'const' => 'single',
  188. 'method' => 'single',
  189. ],
  190. ];
  191. yield 'It can be configured to change certain elements to single line' => [
  192. '<?php
  193. class Foo
  194. {
  195. /**
  196. * Important
  197. */
  198. const FOO_BAR = "foobar";
  199. /** @var bool */
  200. public $variable = true;
  201. /** @var bool */
  202. private $var = false;
  203. /**
  204. * @return void
  205. */
  206. public function hello() {}
  207. }
  208. ',
  209. '<?php
  210. class Foo
  211. {
  212. /**
  213. * Important
  214. */
  215. const FOO_BAR = "foobar";
  216. /**
  217. * @var bool
  218. */
  219. public $variable = true;
  220. /**
  221. * @var bool
  222. */
  223. private $var = false;
  224. /**
  225. * @return void
  226. */
  227. public function hello() {}
  228. }
  229. ',
  230. [
  231. 'property' => 'single',
  232. ],
  233. ];
  234. yield 'It wont change a doc block to single line if it has multiple useful lines' => [
  235. '<?php
  236. class Foo
  237. {
  238. /**
  239. * Important
  240. * Really important
  241. */
  242. const FOO_BAR = "foobar";
  243. }
  244. ',
  245. null,
  246. [
  247. 'const' => 'single',
  248. ],
  249. ];
  250. yield 'It updates doc blocks correctly, even with more indentation' => [
  251. '<?php
  252. if (false) {
  253. class Foo
  254. {
  255. /** @var bool */
  256. public $var = true;
  257. /**
  258. * @return void
  259. */
  260. public function hello () {}
  261. }
  262. }
  263. ',
  264. '<?php
  265. if (false) {
  266. class Foo
  267. {
  268. /**
  269. * @var bool
  270. */
  271. public $var = true;
  272. /** @return void */
  273. public function hello () {}
  274. }
  275. }
  276. ',
  277. [
  278. 'property' => 'single',
  279. ],
  280. ];
  281. yield 'It can convert empty doc blocks' => [
  282. '<?php
  283. class Foo
  284. {
  285. /**
  286. *
  287. */
  288. const FOO = "foobar";
  289. /** */
  290. private $foo;
  291. }',
  292. '<?php
  293. class Foo
  294. {
  295. /** */
  296. const FOO = "foobar";
  297. /**
  298. *
  299. */
  300. private $foo;
  301. }',
  302. [
  303. 'property' => 'single',
  304. ],
  305. ];
  306. yield 'It can update doc blocks of static properties' => [
  307. '<?php
  308. class Bar
  309. {
  310. /**
  311. * Important
  312. */
  313. public static $variable = "acme";
  314. }
  315. ',
  316. '<?php
  317. class Bar
  318. {
  319. /** Important */
  320. public static $variable = "acme";
  321. }
  322. ',
  323. ];
  324. yield 'It can update doc blocks of properties that use the var keyword instead of public' => [
  325. '<?php
  326. class Bar
  327. {
  328. /**
  329. * Important
  330. */
  331. var $variable = "acme";
  332. }
  333. ',
  334. '<?php
  335. class Bar
  336. {
  337. /** Important */
  338. var $variable = "acme";
  339. }
  340. ',
  341. ];
  342. yield 'It can update doc blocks of static that do not declare visibility' => [
  343. '<?php
  344. class Bar
  345. {
  346. /**
  347. * Important
  348. */
  349. static $variable = "acme";
  350. }
  351. ',
  352. '<?php
  353. class Bar
  354. {
  355. /** Important */
  356. static $variable = "acme";
  357. }
  358. ',
  359. ];
  360. yield 'It does not change method doc blocks if configured to do so' => [
  361. '<?php
  362. class Foo
  363. {
  364. /** @return mixed */
  365. public function bar() {}
  366. /**
  367. * @return void
  368. */
  369. public function baz() {}
  370. }',
  371. null,
  372. [
  373. 'method' => null,
  374. ],
  375. ];
  376. yield 'It does not change property doc blocks if configured to do so' => [
  377. '<?php
  378. class Foo
  379. {
  380. /**
  381. * @var int
  382. */
  383. public $foo;
  384. /** @var mixed */
  385. public $bar;
  386. }',
  387. null,
  388. [
  389. 'property' => null,
  390. ],
  391. ];
  392. yield 'It does not change const doc blocks if configured to do so' => [
  393. '<?php
  394. class Foo
  395. {
  396. /**
  397. * @var int
  398. */
  399. public const FOO = 1;
  400. /** @var mixed */
  401. public const BAR = null;
  402. }',
  403. null,
  404. [
  405. 'const' => null,
  406. ],
  407. ];
  408. yield 'It can handle constants with visibility, does not crash on trait imports' => [
  409. '<?php
  410. trait Bar
  411. {}
  412. class Foo
  413. {
  414. /** whatever */
  415. use Bar;
  416. /**
  417. *
  418. */
  419. public const FOO = "foobar";
  420. /** */
  421. private $foo;
  422. }',
  423. '<?php
  424. trait Bar
  425. {}
  426. class Foo
  427. {
  428. /** whatever */
  429. use Bar;
  430. /** */
  431. public const FOO = "foobar";
  432. /**
  433. *
  434. */
  435. private $foo;
  436. }',
  437. [
  438. 'property' => 'single',
  439. ],
  440. ];
  441. yield 'It can handle properties with type declaration' => [
  442. '<?php
  443. class Foo
  444. {
  445. /** */
  446. private ?string $foo;
  447. }',
  448. '<?php
  449. class Foo
  450. {
  451. /**
  452. *
  453. */
  454. private ?string $foo;
  455. }',
  456. [
  457. 'property' => 'single',
  458. ],
  459. ];
  460. yield 'It can handle properties with array type declaration' => [
  461. '<?php
  462. class Foo
  463. {
  464. /** @var string[] */
  465. private array $foo;
  466. }',
  467. '<?php
  468. class Foo
  469. {
  470. /**
  471. * @var string[]
  472. */
  473. private array $foo;
  474. }',
  475. [
  476. 'property' => 'single',
  477. ],
  478. ];
  479. }
  480. /**
  481. * @dataProvider provideFix80Cases
  482. *
  483. * @requires PHP 8.0
  484. *
  485. * @param _AutogeneratedInputConfiguration $config
  486. */
  487. public function testFix80(string $expected, ?string $input = null, array $config = []): void
  488. {
  489. $this->fixer->configure($config);
  490. $this->doTest($expected, $input);
  491. }
  492. public static function provideFix80Cases(): iterable
  493. {
  494. yield 'It detects attributes between docblock and token' => [
  495. '<?php
  496. class Foo
  497. {
  498. /** @var string[] */
  499. #[Attribute1]
  500. private array $foo1;
  501. /** @var string[] */
  502. #[Attribute1]
  503. #[Attribute2]
  504. private array $foo2;
  505. /** @var string[] */
  506. #[Attribute1, Attribute2]
  507. public array $foo3;
  508. }',
  509. '<?php
  510. class Foo
  511. {
  512. /**
  513. * @var string[]
  514. */
  515. #[Attribute1]
  516. private array $foo1;
  517. /**
  518. * @var string[]
  519. */
  520. #[Attribute1]
  521. #[Attribute2]
  522. private array $foo2;
  523. /**
  524. * @var string[]
  525. */
  526. #[Attribute1, Attribute2]
  527. public array $foo3;
  528. }',
  529. [
  530. 'property' => 'single',
  531. ],
  532. ];
  533. yield 'It handles class constants correctly' => [
  534. '<?php
  535. class Foo
  536. {
  537. /**
  538. * 0
  539. */
  540. #[Attribute1]
  541. const B0 = "0";
  542. /**
  543. * 1
  544. */
  545. #[Attribute1]
  546. #[Attribute2]
  547. public const B1 = "1";
  548. /**
  549. * 2
  550. */
  551. #[Attribute1, Attribute2]
  552. public const B2 = "2";
  553. }
  554. ',
  555. '<?php
  556. class Foo
  557. {
  558. /** 0 */
  559. #[Attribute1]
  560. const B0 = "0";
  561. /** 1 */
  562. #[Attribute1]
  563. #[Attribute2]
  564. public const B1 = "1";
  565. /** 2 */
  566. #[Attribute1, Attribute2]
  567. public const B2 = "2";
  568. }
  569. ',
  570. ];
  571. yield 'It handles class functions correctly' => [
  572. '<?php
  573. class Foo
  574. {
  575. /**
  576. * @return void
  577. */
  578. #[Attribute1]
  579. public function hello1() {}
  580. /**
  581. * @return void
  582. */
  583. #[Attribute1]
  584. #[Attribute2]
  585. public function hello2() {}
  586. /**
  587. * @return void
  588. */
  589. #[Attribute1, Attribute2]
  590. public function hello3() {}
  591. }
  592. ',
  593. '<?php
  594. class Foo
  595. {
  596. /** @return void */
  597. #[Attribute1]
  598. public function hello1() {}
  599. /** @return void */
  600. #[Attribute1]
  601. #[Attribute2]
  602. public function hello2() {}
  603. /** @return void */
  604. #[Attribute1, Attribute2]
  605. public function hello3() {}
  606. }
  607. ',
  608. ];
  609. }
  610. /**
  611. * @dataProvider provideFix81Cases
  612. *
  613. * @requires PHP 8.1
  614. *
  615. * @param _AutogeneratedInputConfiguration $config
  616. */
  617. public function testFix81(string $expected, ?string $input = null, array $config = []): void
  618. {
  619. $this->fixer->configure($config);
  620. $this->doTest($expected, $input);
  621. }
  622. public static function provideFix81Cases(): iterable
  623. {
  624. yield 'It handles readonly properties correctly' => [
  625. '<?php
  626. class Foo
  627. {
  628. /** @var string[] */
  629. private readonly array $foo1;
  630. /** @var string[] */
  631. readonly private array $foo2;
  632. /** @var string[] */
  633. readonly array $foo3;
  634. }',
  635. '<?php
  636. class Foo
  637. {
  638. /**
  639. * @var string[]
  640. */
  641. private readonly array $foo1;
  642. /**
  643. * @var string[]
  644. */
  645. readonly private array $foo2;
  646. /**
  647. * @var string[]
  648. */
  649. readonly array $foo3;
  650. }',
  651. [
  652. 'property' => 'single',
  653. ],
  654. ];
  655. yield 'It handles class constant correctly' => [
  656. '<?php
  657. class Foo
  658. {
  659. /**
  660. * 0
  661. */
  662. const B0 = "0";
  663. /**
  664. * 1
  665. */
  666. final public const B1 = "1";
  667. /**
  668. * 2
  669. */
  670. public final const B2 = "2";
  671. /**
  672. * 3
  673. */
  674. final const B3 = "3";
  675. }
  676. ',
  677. '<?php
  678. class Foo
  679. {
  680. /** 0 */
  681. const B0 = "0";
  682. /** 1 */
  683. final public const B1 = "1";
  684. /** 2 */
  685. public final const B2 = "2";
  686. /** 3 */
  687. final const B3 = "3";
  688. }
  689. ',
  690. ];
  691. yield 'It handles enum functions correctly' => [
  692. '<?php
  693. enum Foo
  694. {
  695. /**
  696. * @return void
  697. */
  698. public function hello() {}
  699. }
  700. ',
  701. '<?php
  702. enum Foo
  703. {
  704. /** @return void */
  705. public function hello() {}
  706. }
  707. ',
  708. ];
  709. yield 'It handles enum function with attributes correctly' => [
  710. '<?php
  711. enum Foo
  712. {
  713. /**
  714. * @return void
  715. */
  716. #[Attribute1]
  717. public function hello1() {}
  718. /**
  719. * @return void
  720. */
  721. #[Attribute1]
  722. #[Attribute2]
  723. public function hello2() {}
  724. /**
  725. * @return void
  726. */
  727. #[Attribute1, Attribute2]
  728. public function hello3() {}
  729. }
  730. ',
  731. '<?php
  732. enum Foo
  733. {
  734. /** @return void */
  735. #[Attribute1]
  736. public function hello1() {}
  737. /** @return void */
  738. #[Attribute1]
  739. #[Attribute2]
  740. public function hello2() {}
  741. /** @return void */
  742. #[Attribute1, Attribute2]
  743. public function hello3() {}
  744. }
  745. ',
  746. ];
  747. }
  748. /**
  749. * @dataProvider provideFix82Cases
  750. *
  751. * @requires PHP 8.2
  752. */
  753. public function testFix82(string $expected, ?string $input = null): void
  754. {
  755. $this->doTest($expected, $input);
  756. }
  757. /**
  758. * @return iterable<string, array{string, string}>
  759. */
  760. public static function provideFix82Cases(): iterable
  761. {
  762. yield 'constant in trait' => [
  763. <<<'PHP'
  764. <?php
  765. trait Foo {
  766. /**
  767. * @var string
  768. */
  769. const Foo = 'foo';
  770. }
  771. PHP,
  772. <<<'PHP'
  773. <?php
  774. trait Foo {
  775. /** @var string */
  776. const Foo = 'foo';
  777. }
  778. PHP,
  779. ];
  780. }
  781. }