PhpdocLineSpanFixerTest.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675
  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. final class PhpdocLineSpanFixerTest extends AbstractFixerTestCase
  22. {
  23. /**
  24. * @dataProvider provideFixCases
  25. */
  26. public function testFix(string $expected, ?string $input = null, array $config = []): void
  27. {
  28. $this->fixer->configure($config);
  29. $this->doTest($expected, $input);
  30. }
  31. public function provideFixCases(): array
  32. {
  33. return [
  34. 'It does not change doc blocks if not needed' => [
  35. '<?php
  36. class Foo
  37. {
  38. /**
  39. * Important
  40. */
  41. const FOO_BAR = "foobar";
  42. /**
  43. * @var bool
  44. */
  45. public $variable = true;
  46. /**
  47. * @var bool
  48. */
  49. private $var = false;
  50. /**
  51. * @return void
  52. */
  53. public function hello() {}
  54. }
  55. ',
  56. ],
  57. 'It does change doc blocks to multi by default' => [
  58. '<?php
  59. class Foo
  60. {
  61. /**
  62. * Important
  63. */
  64. const FOO_BAR = "foobar";
  65. /**
  66. * @var bool
  67. */
  68. public $variable = true;
  69. /**
  70. * @var bool
  71. */
  72. private $var = false;
  73. /**
  74. * @return void
  75. */
  76. public function hello() {}
  77. }
  78. ',
  79. '<?php
  80. class Foo
  81. {
  82. /** Important */
  83. const FOO_BAR = "foobar";
  84. /** @var bool */
  85. public $variable = true;
  86. /** @var bool */
  87. private $var = false;
  88. /** @return void */
  89. public function hello() {}
  90. }
  91. ',
  92. ],
  93. 'It does change doc blocks to single if configured to do so' => [
  94. '<?php
  95. class Foo
  96. {
  97. /** Important */
  98. const FOO_BAR = "foobar";
  99. /** @var bool */
  100. public $variable = true;
  101. /** @var bool */
  102. private $var = false;
  103. /** @return void */
  104. public function hello() {}
  105. }
  106. ',
  107. '<?php
  108. class Foo
  109. {
  110. /**
  111. * Important
  112. */
  113. const FOO_BAR = "foobar";
  114. /**
  115. * @var bool
  116. */
  117. public $variable = true;
  118. /**
  119. * @var bool
  120. */
  121. private $var = false;
  122. /**
  123. * @return void
  124. */
  125. public function hello() {}
  126. }
  127. ',
  128. [
  129. 'property' => 'single',
  130. 'const' => 'single',
  131. 'method' => 'single',
  132. ],
  133. ],
  134. 'It does change complicated doc blocks to single if configured to do so' => [
  135. '<?php
  136. class Foo
  137. {
  138. /** @var bool */
  139. public $variable1 = true;
  140. /** @var bool */
  141. public $variable2 = true;
  142. /** @Assert\File(mimeTypes={ "image/jpeg", "image/png" }) */
  143. public $imageFileObject;
  144. }
  145. ',
  146. '<?php
  147. class Foo
  148. {
  149. /**
  150. * @var bool */
  151. public $variable1 = true;
  152. /** @var bool
  153. */
  154. public $variable2 = true;
  155. /**
  156. * @Assert\File(mimeTypes={ "image/jpeg", "image/png" })
  157. */
  158. public $imageFileObject;
  159. }
  160. ',
  161. [
  162. 'property' => 'single',
  163. ],
  164. ],
  165. 'It does not changes doc blocks from single if configured to do so' => [
  166. '<?php
  167. class Foo
  168. {
  169. /** Important */
  170. const FOO_BAR = "foobar";
  171. /** @var bool */
  172. public $variable = true;
  173. /** @var bool */
  174. private $var = false;
  175. /** @return void */
  176. public function hello() {}
  177. }
  178. ',
  179. null,
  180. [
  181. 'property' => 'single',
  182. 'const' => 'single',
  183. 'method' => 'single',
  184. ],
  185. ],
  186. 'It can be configured to change certain elements to single line' => [
  187. '<?php
  188. class Foo
  189. {
  190. /**
  191. * Important
  192. */
  193. const FOO_BAR = "foobar";
  194. /** @var bool */
  195. public $variable = true;
  196. /** @var bool */
  197. private $var = false;
  198. /**
  199. * @return void
  200. */
  201. public function hello() {}
  202. }
  203. ',
  204. '<?php
  205. class Foo
  206. {
  207. /**
  208. * Important
  209. */
  210. const FOO_BAR = "foobar";
  211. /**
  212. * @var bool
  213. */
  214. public $variable = true;
  215. /**
  216. * @var bool
  217. */
  218. private $var = false;
  219. /**
  220. * @return void
  221. */
  222. public function hello() {}
  223. }
  224. ',
  225. [
  226. 'property' => 'single',
  227. ],
  228. ],
  229. 'It wont change a doc block to single line if it has multiple useful lines' => [
  230. '<?php
  231. class Foo
  232. {
  233. /**
  234. * Important
  235. * Really important
  236. */
  237. const FOO_BAR = "foobar";
  238. }
  239. ',
  240. null,
  241. [
  242. 'const' => 'single',
  243. ],
  244. ],
  245. 'It updates doc blocks correctly, even with more indentation' => [
  246. '<?php
  247. if (false) {
  248. class Foo
  249. {
  250. /** @var bool */
  251. public $var = true;
  252. /**
  253. * @return void
  254. */
  255. public function hello () {}
  256. }
  257. }
  258. ',
  259. '<?php
  260. if (false) {
  261. class Foo
  262. {
  263. /**
  264. * @var bool
  265. */
  266. public $var = true;
  267. /** @return void */
  268. public function hello () {}
  269. }
  270. }
  271. ',
  272. [
  273. 'property' => 'single',
  274. ],
  275. ],
  276. 'It can convert empty doc blocks' => [
  277. '<?php
  278. class Foo
  279. {
  280. /**
  281. *
  282. */
  283. const FOO = "foobar";
  284. /** */
  285. private $foo;
  286. }',
  287. '<?php
  288. class Foo
  289. {
  290. /** */
  291. const FOO = "foobar";
  292. /**
  293. *
  294. */
  295. private $foo;
  296. }',
  297. [
  298. 'property' => 'single',
  299. ],
  300. ],
  301. 'It can update doc blocks of static properties' => [
  302. '<?php
  303. class Bar
  304. {
  305. /**
  306. * Important
  307. */
  308. public static $variable = "acme";
  309. }
  310. ',
  311. '<?php
  312. class Bar
  313. {
  314. /** Important */
  315. public static $variable = "acme";
  316. }
  317. ',
  318. ],
  319. 'It can update doc blocks of properties that use the var keyword instead of public' => [
  320. '<?php
  321. class Bar
  322. {
  323. /**
  324. * Important
  325. */
  326. var $variable = "acme";
  327. }
  328. ',
  329. '<?php
  330. class Bar
  331. {
  332. /** Important */
  333. var $variable = "acme";
  334. }
  335. ',
  336. ],
  337. 'It can update doc blocks of static that do not declare visibility' => [
  338. '<?php
  339. class Bar
  340. {
  341. /**
  342. * Important
  343. */
  344. static $variable = "acme";
  345. }
  346. ',
  347. '<?php
  348. class Bar
  349. {
  350. /** Important */
  351. static $variable = "acme";
  352. }
  353. ',
  354. ],
  355. 'It does not change method doc blocks if configured to do so' => [
  356. '<?php
  357. class Foo
  358. {
  359. /** @return mixed */
  360. public function bar() {}
  361. /**
  362. * @return void
  363. */
  364. public function baz() {}
  365. }',
  366. null,
  367. [
  368. 'method' => null,
  369. ],
  370. ],
  371. 'It does not change property doc blocks if configured to do so' => [
  372. '<?php
  373. class Foo
  374. {
  375. /**
  376. * @var int
  377. */
  378. public $foo;
  379. /** @var mixed */
  380. public $bar;
  381. }',
  382. null,
  383. [
  384. 'property' => null,
  385. ],
  386. ],
  387. 'It does not change const doc blocks if configured to do so' => [
  388. '<?php
  389. class Foo
  390. {
  391. /**
  392. * @var int
  393. */
  394. public const FOO = 1;
  395. /** @var mixed */
  396. public const BAR = null;
  397. }',
  398. null,
  399. [
  400. 'const' => null,
  401. ],
  402. ],
  403. ];
  404. }
  405. /**
  406. * @requires PHP 7.1
  407. * @dataProvider provideFix71Cases
  408. */
  409. public function testFix71(string $expected, string $input = null, array $config = []): void
  410. {
  411. $this->fixer->configure($config);
  412. $this->doTest($expected, $input);
  413. }
  414. public function provideFix71Cases(): array
  415. {
  416. return [
  417. 'It can handle constants with visibility' => [
  418. '<?php
  419. class Foo
  420. {
  421. /**
  422. *
  423. */
  424. public const FOO = "foobar";
  425. /** */
  426. private $foo;
  427. }',
  428. '<?php
  429. class Foo
  430. {
  431. /** */
  432. public const FOO = "foobar";
  433. /**
  434. *
  435. */
  436. private $foo;
  437. }',
  438. [
  439. 'property' => 'single',
  440. ],
  441. ],
  442. ];
  443. }
  444. /**
  445. * @requires PHP 7.4
  446. * @dataProvider provideFixPhp74Cases
  447. */
  448. public function testFixPhp74(string $expected, string $input = null, array $config = []): void
  449. {
  450. $this->fixer->configure($config);
  451. $this->doTest($expected, $input);
  452. }
  453. public function provideFixPhp74Cases(): array
  454. {
  455. return [
  456. 'It can handle properties with type declaration' => [
  457. '<?php
  458. class Foo
  459. {
  460. /** */
  461. private ?string $foo;
  462. }',
  463. '<?php
  464. class Foo
  465. {
  466. /**
  467. *
  468. */
  469. private ?string $foo;
  470. }',
  471. [
  472. 'property' => 'single',
  473. ],
  474. ],
  475. 'It can handle properties with array type declaration' => [
  476. '<?php
  477. class Foo
  478. {
  479. /** @var string[] */
  480. private array $foo;
  481. }',
  482. '<?php
  483. class Foo
  484. {
  485. /**
  486. * @var string[]
  487. */
  488. private array $foo;
  489. }',
  490. [
  491. 'property' => 'single',
  492. ],
  493. ],
  494. ];
  495. }
  496. /**
  497. * @dataProvider provideFix81Cases
  498. * @requires PHP 8.1
  499. */
  500. public function testFix81(string $expected, string $input = null, array $config = []): void
  501. {
  502. $this->fixer->configure($config);
  503. $this->doTest($expected, $input);
  504. }
  505. public function provideFix81Cases(): \Generator
  506. {
  507. yield 'readonly' => [
  508. '<?php
  509. class Foo
  510. {
  511. /** @var string[] */
  512. private readonly array $foo1;
  513. /** @var string[] */
  514. readonly private array $foo2;
  515. /** @var string[] */
  516. readonly array $foo3;
  517. }',
  518. '<?php
  519. class Foo
  520. {
  521. /**
  522. * @var string[]
  523. */
  524. private readonly array $foo1;
  525. /**
  526. * @var string[]
  527. */
  528. readonly private array $foo2;
  529. /**
  530. * @var string[]
  531. */
  532. readonly array $foo3;
  533. }',
  534. [
  535. 'property' => 'single',
  536. ],
  537. ];
  538. yield [
  539. '<?php
  540. class Foo
  541. {
  542. /**
  543. * 0
  544. */
  545. const B0 = "0";
  546. /**
  547. * 1
  548. */
  549. final public const B1 = "1";
  550. /**
  551. * 2
  552. */
  553. public final const B2 = "2";
  554. /**
  555. * 3
  556. */
  557. final const B3 = "3";
  558. }
  559. ',
  560. '<?php
  561. class Foo
  562. {
  563. /** 0 */
  564. const B0 = "0";
  565. /** 1 */
  566. final public const B1 = "1";
  567. /** 2 */
  568. public final const B2 = "2";
  569. /** 3 */
  570. final const B3 = "3";
  571. }
  572. ',
  573. ];
  574. }
  575. }