PhpdocToCommentFixerTest.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707
  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. /**
  14. * @author Ceeram <ceeram@cakephp.org>
  15. *
  16. * @internal
  17. *
  18. * @covers \PhpCsFixer\Fixer\Phpdoc\PhpdocToCommentFixer
  19. */
  20. final class PhpdocToCommentFixerTest extends AbstractFixerTestCase
  21. {
  22. /**
  23. * @param string $expected
  24. * @param null|string $input
  25. *
  26. * @dataProvider provideDocblocksCases
  27. */
  28. public function testFix($expected, $input = null)
  29. {
  30. $this->doTest($expected, $input);
  31. }
  32. /**
  33. * @param string $expected
  34. * @param null|string $input
  35. *
  36. * @dataProvider provideTraitsCases
  37. */
  38. public function testFixTraits($expected, $input = null)
  39. {
  40. $this->doTest($expected, $input);
  41. }
  42. public function provideDocblocksCases()
  43. {
  44. $cases = [];
  45. $cases[] = [
  46. '<?php
  47. /**
  48. * Do not convert this
  49. */
  50. namespace Docs;
  51. /**
  52. * Do not convert this
  53. */
  54. class DocBlocks
  55. {
  56. /**
  57. * Do not convert this
  58. */
  59. const STRUCTURAL = true;
  60. /**
  61. * Do not convert this
  62. */
  63. protected $indent = false;
  64. /**
  65. * Do not convert this
  66. */
  67. var $oldPublicStyle;
  68. /**
  69. * Do not convert this
  70. */
  71. public function test() {}
  72. /**
  73. * Do not convert this
  74. */
  75. private function testPrivate() {}
  76. /**
  77. * Do not convert this
  78. */
  79. function testNoVisibility() {}
  80. }',
  81. ];
  82. $cases[] = [
  83. '<?php namespace Docs;
  84. /**
  85. * Do not convert this
  86. */
  87. /**
  88. * Do not convert this
  89. */
  90. class DocBlocks{}
  91. ',
  92. ];
  93. $cases[] = [
  94. '<?php
  95. /**
  96. * Do not convert this
  97. */
  98. namespace Foo;
  99. ',
  100. ];
  101. $cases[] = [
  102. '<?php
  103. $first = true;// needed because by default first docblock is never fixed.
  104. /**
  105. * Do not convert this
  106. */
  107. abstract class DocBlocks
  108. {
  109. /**
  110. * Do not convert this
  111. */
  112. abstract public function test();
  113. }',
  114. ];
  115. $cases[] = [
  116. '<?php
  117. $first = true;// needed because by default first docblock is never fixed.
  118. /**
  119. * Do not convert this
  120. */
  121. interface DocBlocks
  122. {
  123. public function test();
  124. }',
  125. ];
  126. $cases[] = [
  127. '<?php
  128. namespace NS;
  129. /**
  130. * Do not
  131. */
  132. final class Foo
  133. {
  134. }',
  135. ];
  136. $cases[] = [
  137. '<?php
  138. $first = true;// needed because by default first docblock is never fixed.
  139. /**
  140. * Do not convert this
  141. */
  142. require "require.php";
  143. /**
  144. * Do not convert this
  145. */
  146. require_once "require_once.php";
  147. /**
  148. * Do not convert this
  149. */
  150. include "include.php";
  151. /**
  152. * Do not convert this
  153. */
  154. include_once "include_once.php";
  155. ',
  156. ];
  157. $cases[] = [
  158. '<?php
  159. $first = true;// needed because by default first docblock is never fixed.
  160. /**
  161. * Do not convert this
  162. *
  163. * @var int
  164. */
  165. $a = require "require.php";
  166. /**
  167. * Do not convert this
  168. *
  169. * @var int
  170. */
  171. $b = require_once "require_once.php";
  172. /**
  173. * Do not convert this
  174. *
  175. * @var int
  176. */
  177. $c = include "include.php";
  178. /**
  179. * Do not convert this
  180. *
  181. * @var int
  182. */
  183. $d = include_once "include_once.php";
  184. /**
  185. * @var Composer\Autoload\ClassLoader $loader
  186. */
  187. $loader = require_once __DIR__."/vendor/autoload.php";
  188. ',
  189. ];
  190. $cases[] = [
  191. '<?php
  192. $first = true;// needed because by default first docblock is never fixed.
  193. /**
  194. * @var ClassLoader $loader
  195. */
  196. $loader = require_once __DIR__."/../app/autoload.php";
  197. ',
  198. ];
  199. $cases[] = [
  200. '<?php
  201. $first = true;// needed because by default first docblock is never fixed.
  202. /**
  203. * Do not convert this
  204. *
  205. * @var Foo
  206. */
  207. $foo = createFoo();
  208. ',
  209. ];
  210. $cases[] = [
  211. '<?php
  212. $first = true;// needed because by default first docblock is never fixed.
  213. /**
  214. * Do not convert this
  215. *
  216. * @var bool $local
  217. */
  218. $local = true;
  219. ',
  220. ];
  221. $cases[] = [
  222. '<?php
  223. $first = true;// needed because by default first docblock is never fixed.
  224. /**
  225. * Comment
  226. */
  227. $local = true;
  228. ',
  229. ];
  230. $cases[] = [
  231. '<?php
  232. $first = true;// needed because by default first docblock is never fixed.
  233. /** @var \Sqlite3 $sqlite */
  234. foreach($connections as $sqlite) {
  235. $sqlite->open($path);
  236. }',
  237. ];
  238. $cases[] = [
  239. '<?php
  240. $first = true;// needed because by default first docblock is never fixed.
  241. /** @var \Sqlite3 $sqlite */
  242. foreach($connections as $key => $sqlite) {
  243. $sqlite->open($path);
  244. }',
  245. ];
  246. $cases[] = [
  247. '<?php
  248. $first = true;// needed because by default first docblock is never fixed.
  249. /** @var int $key */
  250. foreach($connections as $key => $sqlite) {
  251. $sqlite->open($path);
  252. }',
  253. ];
  254. $cases[] = [
  255. '<?php
  256. $first = true;// needed because by default first docblock is never fixed.
  257. /* This should not be a docblock */
  258. foreach($connections as $key => $sqlite) {
  259. $sqlite->open($path);
  260. }',
  261. '<?php
  262. $first = true;// needed because by default first docblock is never fixed.
  263. /** This should not be a docblock */
  264. foreach($connections as $key => $sqlite) {
  265. $sqlite->open($path);
  266. }',
  267. ];
  268. $cases[] = [
  269. '<?php
  270. $first = true;// needed because by default first docblock is never fixed.
  271. /* there should be no docblock here */
  272. $sqlite1->open($path);
  273. ',
  274. '<?php
  275. $first = true;// needed because by default first docblock is never fixed.
  276. /** there should be no docblock here */
  277. $sqlite1->open($path);
  278. ',
  279. ];
  280. $cases[] = [
  281. '<?php
  282. $first = true;// needed because by default first docblock is never fixed.
  283. /* there should be no docblock here */
  284. $i++;
  285. ',
  286. '<?php
  287. $first = true;// needed because by default first docblock is never fixed.
  288. /** there should be no docblock here */
  289. $i++;
  290. ',
  291. ];
  292. $cases[] = [
  293. '<?php
  294. $first = true;// needed because by default first docblock is never fixed.
  295. /** @var int $index */
  296. $index = $a[\'number\'];
  297. ',
  298. ];
  299. $cases[] = [
  300. '<?php
  301. $first = true;// needed because by default first docblock is never fixed.
  302. /** @var string $two */
  303. list($one, $two) = explode("," , $csvLines);
  304. ',
  305. ];
  306. $cases[] = [
  307. '<?php
  308. $first = true;// needed because by default first docblock is never fixed.
  309. /* This should be a comment */
  310. list($one, $two) = explode("," , $csvLines);
  311. ',
  312. '<?php
  313. $first = true;// needed because by default first docblock is never fixed.
  314. /** This should be a comment */
  315. list($one, $two) = explode("," , $csvLines);
  316. ',
  317. ];
  318. $cases[] = [
  319. '<?php
  320. $first = true;// needed because by default first docblock is never fixed.
  321. /** @var int $index */
  322. foreach ($foo->getPairs($c->bar(), $bar) as $index => list($a, $b)) {
  323. // Do something with $index, $a and $b
  324. }
  325. /** @var \Closure $value */
  326. if (!$value = $this->getValue()) {
  327. return false;
  328. }
  329. /** @var string $name */
  330. switch ($name = $this->getName()) {
  331. case "John":
  332. return false;
  333. case "Jane":
  334. return true;
  335. }
  336. /** @var string $content */
  337. while ($content = $this->getContent()) {
  338. $name .= $content;
  339. }
  340. /** @var int $size */
  341. for($i = 0, $size = count($people); $i < $size; ++$i) {
  342. $people[$i][\'salt\'] = mt_rand(000000, 999999);
  343. }',
  344. ];
  345. $cases[] = [
  346. '<?php
  347. $first = true;// needed because by default first docblock is never fixed.
  348. /* @var int $wrong */
  349. foreach ($foo->getPairs($c->bar(), $bar) as $index => list($a, $b)) {
  350. // Do something with $index, $a and $b
  351. }
  352. /* @var \Closure $notValue */
  353. if (!$value = $this->getValue()) {
  354. return false;
  355. }
  356. /* @var string $notName */
  357. switch ($name = $this->getName()) {
  358. case "John":
  359. return false;
  360. case "Jane":
  361. return true;
  362. }
  363. /* @var string $notContent */
  364. while ($content = $this->getContent()) {
  365. $name .= $content;
  366. }
  367. /* @var int $notSize */
  368. for($i = 0, $size = count($people); $i < $size; ++$i) {
  369. $people[$i][\'salt\'] = mt_rand(000000, 999999);
  370. }',
  371. '<?php
  372. $first = true;// needed because by default first docblock is never fixed.
  373. /** @var int $wrong */
  374. foreach ($foo->getPairs($c->bar(), $bar) as $index => list($a, $b)) {
  375. // Do something with $index, $a and $b
  376. }
  377. /** @var \Closure $notValue */
  378. if (!$value = $this->getValue()) {
  379. return false;
  380. }
  381. /** @var string $notName */
  382. switch ($name = $this->getName()) {
  383. case "John":
  384. return false;
  385. case "Jane":
  386. return true;
  387. }
  388. /** @var string $notContent */
  389. while ($content = $this->getContent()) {
  390. $name .= $content;
  391. }
  392. /** @var int $notSize */
  393. for($i = 0, $size = count($people); $i < $size; ++$i) {
  394. $people[$i][\'salt\'] = mt_rand(000000, 999999);
  395. }',
  396. ];
  397. $cases[] = [
  398. '<?php
  399. /* This should be a comment */
  400. ',
  401. '<?php
  402. /** This should be a comment */
  403. ',
  404. ];
  405. $cases[] = [
  406. '<?php
  407. /**
  408. * This is a page level docblock should stay untouched
  409. */
  410. echo "Some string";
  411. ',
  412. ];
  413. $cases[] = [
  414. '<?php
  415. $first = true;// needed because by default first docblock is never fixed.
  416. /** @var \NumberFormatter $formatter */
  417. static $formatter;
  418. ',
  419. ];
  420. $cases[] = [
  421. '<?php
  422. $first = true;// needed because by default first docblock is never fixed.
  423. function getNumberFormatter()
  424. {
  425. /** @var \NumberFormatter $formatter */
  426. static $formatter;
  427. }
  428. ',
  429. ];
  430. $cases[] = [
  431. '<?php
  432. class A
  433. {
  434. public function b()
  435. {
  436. /** @var int $c */
  437. print($c = 0);
  438. }
  439. }
  440. ',
  441. ];
  442. $cases[] = ['<?php
  443. /** header */
  444. echo 123;
  445. /** @var int $bar1 */
  446. (print($bar1 = 0));
  447. ',
  448. ];
  449. $cases[] = [
  450. '<?php
  451. /** header */
  452. echo 123;
  453. /** @var ClassLoader $loader */
  454. $loader = require __DIR__.\'/../vendor/autoload.php\';
  455. ',
  456. ];
  457. return $cases;
  458. }
  459. public function provideTraitsCases()
  460. {
  461. return [
  462. [
  463. '<?php
  464. $first = true;// needed because by default first docblock is never fixed.
  465. /**
  466. * Do not convert this
  467. */
  468. trait DocBlocks
  469. {
  470. public function test() {}
  471. }',
  472. ],
  473. ];
  474. }
  475. /**
  476. * @param string $expected
  477. * @param null|string $input
  478. *
  479. * @dataProvider provideFix70Cases
  480. * @requires PHP 7.0
  481. */
  482. public function testFix70($expected, $input = null)
  483. {
  484. $this->doTest($expected, $input);
  485. }
  486. public function provideFix70Cases()
  487. {
  488. return [
  489. [
  490. '<?php
  491. /** header */
  492. echo 123;
  493. /** @var User $bar3 */
  494. ($bar3 = tmp())->doSomething();
  495. /** @var Session $session */ # test
  496. $session = new Session();
  497. ',
  498. ],
  499. ];
  500. }
  501. /**
  502. * @param string $expected
  503. * @param null|string $input
  504. *
  505. * @dataProvider provideFix71Cases
  506. * @requires PHP 7.1
  507. */
  508. public function testFix71($expected, $input = null)
  509. {
  510. $this->doTest($expected, $input);
  511. }
  512. public function provideFix71Cases()
  513. {
  514. return [
  515. [
  516. '<?php
  517. $first = true;// needed because by default first docblock is never fixed.
  518. /** @var int $a */
  519. [$a] = $b;
  520. /* @var int $c */
  521. [$a] = $c;
  522. ',
  523. '<?php
  524. $first = true;// needed because by default first docblock is never fixed.
  525. /** @var int $a */
  526. [$a] = $b;
  527. /** @var int $c */
  528. [$a] = $c;
  529. ',
  530. ],
  531. [
  532. '<?php
  533. $first = true;// needed because by default first docblock is never fixed.
  534. /**
  535. * @var int $a
  536. */
  537. [$a] = $b;
  538. ',
  539. ],
  540. ];
  541. }
  542. /**
  543. * @param string $expected
  544. * @param null|string $input
  545. *
  546. * @dataProvider provideFix74Cases
  547. * @requires PHP 7.4
  548. */
  549. public function testFix74($expected, $input = null)
  550. {
  551. $this->doTest($expected, $input);
  552. }
  553. public function provideFix74Cases()
  554. {
  555. return [
  556. [
  557. '<?php
  558. class Foo {
  559. /**
  560. * Do not convert this
  561. */
  562. private int $foo;
  563. }',
  564. ],
  565. [
  566. '<?php
  567. class Foo {
  568. /**
  569. * Do not convert this
  570. */
  571. protected ?string $foo;
  572. }',
  573. ],
  574. [
  575. '<?php
  576. class Foo {
  577. /**
  578. * Do not convert this
  579. */
  580. public ? float $foo;
  581. }',
  582. ],
  583. [
  584. '<?php
  585. class Foo {
  586. /**
  587. * Do not convert this
  588. */
  589. var ? Foo\Bar $foo;
  590. }',
  591. ],
  592. ];
  593. }
  594. }