PhpdocToCommentFixerTest.php 11 KB

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