PhpdocAddMissingParamAnnotationFixerTest.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537
  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. use PhpCsFixer\WhitespacesFixerConfig;
  14. /**
  15. * @author Dariusz Rumiński <dariusz.ruminski@gmail.com>
  16. *
  17. * @internal
  18. *
  19. * @covers \PhpCsFixer\Fixer\Phpdoc\PhpdocAddMissingParamAnnotationFixer
  20. */
  21. final class PhpdocAddMissingParamAnnotationFixerTest extends AbstractFixerTestCase
  22. {
  23. public function testConfigureRejectsUnknownConfigurationKey()
  24. {
  25. $key = 'foo';
  26. $this->expectException(\PhpCsFixer\ConfigurationException\InvalidConfigurationException::class);
  27. $this->expectExceptionMessage(sprintf(
  28. '[phpdoc_add_missing_param_annotation] Invalid configuration: The option "%s" does not exist.',
  29. $key
  30. ));
  31. $this->fixer->configure([
  32. $key => 'bar',
  33. ]);
  34. }
  35. /**
  36. * @dataProvider provideConfigureRejectsInvalidConfigurationValueCases
  37. *
  38. * @param mixed $value
  39. * @param string $expectedMessage
  40. */
  41. public function testConfigureRejectsInvalidConfigurationValue($value, $expectedMessage)
  42. {
  43. $this->expectException(\PhpCsFixer\ConfigurationException\InvalidConfigurationException::class);
  44. $this->expectExceptionMessageRegExp($expectedMessage);
  45. $this->fixer->configure([
  46. 'only_untyped' => $value,
  47. ]);
  48. }
  49. /**
  50. * @return iterable<string, array>
  51. */
  52. public function provideConfigureRejectsInvalidConfigurationValueCases()
  53. {
  54. yield 'null' => [
  55. null,
  56. '#expected to be of type "bool", but is of type "(null|NULL)"\.$#',
  57. ];
  58. yield 'int' => [
  59. 1,
  60. '#expected to be of type "bool", but is of type "(int|integer)"\.$#',
  61. ];
  62. yield 'array' => [
  63. [],
  64. '#expected to be of type "bool", but is of type "array"\.$#',
  65. ];
  66. yield 'float' => [
  67. 0.1,
  68. '#expected to be of type "bool", but is of type "(float|double)"\.$#',
  69. ];
  70. yield 'object' => [
  71. new \stdClass(),
  72. '#expected to be of type "bool", but is of type "stdClass"\.$#',
  73. ];
  74. }
  75. /**
  76. * @param string $expected
  77. * @param null|string $input
  78. *
  79. * @dataProvider provideFixCases
  80. */
  81. public function testFix($expected, $input = null, array $config = [])
  82. {
  83. $this->fixer->configure($config ?: ['only_untyped' => false]);
  84. $this->doTest($expected, $input);
  85. }
  86. public function provideFixCases()
  87. {
  88. return [
  89. [
  90. '<?php
  91. /**
  92. *
  93. */',
  94. ],
  95. [
  96. '<?php
  97. /**
  98. * @param int $foo
  99. * @param mixed $bar
  100. */
  101. function f1($foo, $bar) {}',
  102. '<?php
  103. /**
  104. * @param int $foo
  105. */
  106. function f1($foo, $bar) {}',
  107. ],
  108. [
  109. '<?php
  110. /**
  111. * @param int $bar
  112. * @param mixed $foo
  113. */
  114. function f2($foo, $bar) {}',
  115. '<?php
  116. /**
  117. * @param int $bar
  118. */
  119. function f2($foo, $bar) {}',
  120. ],
  121. [
  122. '<?php
  123. /**
  124. * @return void
  125. * @param mixed $foo
  126. * @param mixed $bar
  127. */
  128. function f3($foo, $bar) {}',
  129. '<?php
  130. /**
  131. * @return void
  132. */
  133. function f3($foo, $bar) {}',
  134. ],
  135. [
  136. '<?php
  137. abstract class Foo {
  138. /**
  139. * @param int $bar
  140. * @param mixed $foo
  141. */
  142. abstract public function f4a($foo, $bar);
  143. }',
  144. '<?php
  145. abstract class Foo {
  146. /**
  147. * @param int $bar
  148. */
  149. abstract public function f4a($foo, $bar);
  150. }',
  151. ],
  152. [
  153. '<?php
  154. class Foo {
  155. /**
  156. * @param int $bar
  157. * @param mixed $foo
  158. */
  159. static final public function f4b($foo, $bar) {}
  160. }',
  161. '<?php
  162. class Foo {
  163. /**
  164. * @param int $bar
  165. */
  166. static final public function f4b($foo, $bar) {}
  167. }',
  168. ],
  169. [
  170. '<?php
  171. class Foo {
  172. /**
  173. * @var int
  174. */
  175. private $foo;
  176. }',
  177. ],
  178. [
  179. '<?php
  180. /**
  181. * @param $bar No type !!
  182. * @param mixed $foo
  183. */
  184. function f5($foo, $bar) {}',
  185. '<?php
  186. /**
  187. * @param $bar No type !!
  188. */
  189. function f5($foo, $bar) {}',
  190. ],
  191. [
  192. '<?php
  193. /**
  194. * @param int
  195. * @param int $bar
  196. * @param Foo\Bar $foo
  197. */
  198. function f6(Foo\Bar $foo, $bar) {}',
  199. '<?php
  200. /**
  201. * @param int
  202. * @param int $bar
  203. */
  204. function f6(Foo\Bar $foo, $bar) {}',
  205. ],
  206. [
  207. '<?php
  208. /**
  209. * @param int $bar
  210. * @param null|string $foo
  211. */
  212. function f7(string $foo = nuLl, $bar) {}',
  213. '<?php
  214. /**
  215. * @param int $bar
  216. */
  217. function f7(string $foo = nuLl, $bar) {}',
  218. ],
  219. [
  220. '<?php
  221. /**
  222. * @param int $bar
  223. * @param mixed $baz
  224. *
  225. * @return void
  226. */
  227. function f9(string $foo, $bar, $baz) {}',
  228. '<?php
  229. /**
  230. * @param int $bar
  231. *
  232. * @return void
  233. */
  234. function f9(string $foo, $bar, $baz) {}',
  235. ['only_untyped' => true],
  236. ],
  237. [
  238. '<?php
  239. /**
  240. * @param bool|bool[] $caseSensitive Line 1
  241. * Line 2
  242. */
  243. function f11($caseSensitive) {}
  244. ',
  245. ],
  246. [
  247. '<?php
  248. /** @return string */
  249. function hello($string)
  250. {
  251. return $string;
  252. }',
  253. ],
  254. [
  255. '<?php
  256. /** @return string
  257. * @param mixed $string
  258. */
  259. function hello($string)
  260. {
  261. return $string;
  262. }',
  263. '<?php
  264. /** @return string
  265. */
  266. function hello($string)
  267. {
  268. return $string;
  269. }',
  270. ],
  271. [
  272. '<?php
  273. /**
  274. * @param mixed $string
  275. * @return string */
  276. function hello($string)
  277. {
  278. return $string;
  279. }',
  280. '<?php
  281. /**
  282. * @return string */
  283. function hello($string)
  284. {
  285. return $string;
  286. }',
  287. ],
  288. ];
  289. }
  290. /**
  291. * @param string $expected
  292. * @param null|string $input
  293. *
  294. * @dataProvider provideFix70Cases
  295. * @requires PHP 7.0
  296. */
  297. public function testFix70($expected, $input = null, array $config = [])
  298. {
  299. $this->fixer->configure($config ?: ['only_untyped' => false]);
  300. $this->doTest($expected, $input);
  301. }
  302. public function provideFix70Cases()
  303. {
  304. return [
  305. [
  306. '<?php
  307. /**
  308. * @param int $bar
  309. * @param string $foo
  310. */
  311. function f8(string $foo = "null", $bar) {}',
  312. '<?php
  313. /**
  314. * @param int $bar
  315. */
  316. function f8(string $foo = "null", $bar) {}',
  317. ],
  318. [
  319. '<?php
  320. /**
  321. * @{inheritdoc}
  322. */
  323. function f10(string $foo = "null", $bar) {}',
  324. ],
  325. ];
  326. }
  327. /**
  328. * @param string $expected
  329. * @param null|string $input
  330. *
  331. * @dataProvider provideFix71Cases
  332. * @requires PHP 7.1
  333. */
  334. public function testFix71($expected, $input, array $config)
  335. {
  336. $this->fixer->configure($config);
  337. $this->doTest($expected, $input);
  338. }
  339. public function provideFix71Cases()
  340. {
  341. return [
  342. [
  343. '<?php
  344. /**
  345. * @param int $bar
  346. * @param ?array $foo
  347. */
  348. function p1(?array $foo = null, $bar) {}',
  349. '<?php
  350. /**
  351. * @param int $bar
  352. */
  353. function p1(?array $foo = null, $bar) {}',
  354. ['only_untyped' => false],
  355. ],
  356. [
  357. '<?php
  358. /**
  359. * Foo
  360. * @param mixed $bar
  361. */
  362. function p1(?int $foo = 0, $bar) {}',
  363. '<?php
  364. /**
  365. * Foo
  366. */
  367. function p1(?int $foo = 0, $bar) {}',
  368. ['only_untyped' => true],
  369. ],
  370. [
  371. '<?php
  372. /**
  373. * Foo
  374. * @return int
  375. */
  376. function p1(?int $foo = 0) {}',
  377. null,
  378. ['only_untyped' => true],
  379. ],
  380. ];
  381. }
  382. /**
  383. * @param string $expected
  384. * @param null|string $input
  385. *
  386. * @dataProvider provideMessyWhitespacesCases
  387. */
  388. public function testMessyWhitespaces($expected, $input = null, array $config = [])
  389. {
  390. $this->fixer->setWhitespacesConfig(new WhitespacesFixerConfig("\t", "\r\n"));
  391. $this->fixer->configure($config ?: ['only_untyped' => false]);
  392. $this->doTest($expected, $input);
  393. }
  394. public function provideMessyWhitespacesCases()
  395. {
  396. return [
  397. [
  398. "<?php\r\n\t/**\r\n\t * @param int \$bar\r\n\t * @param null|string \$foo\r\n\t */\r\n\tfunction f7(string \$foo = nuLl, \$bar) {}",
  399. "<?php\r\n\t/**\r\n\t * @param int \$bar\r\n\t */\r\n\tfunction f7(string \$foo = nuLl, \$bar) {}",
  400. ],
  401. ];
  402. }
  403. /**
  404. * @param string $expected
  405. * @param string $input
  406. *
  407. * @dataProvider provideByReferenceCases
  408. */
  409. public function testByReference($expected, $input)
  410. {
  411. $this->fixer->configure(['only_untyped' => false]);
  412. $this->doTest($expected, $input);
  413. }
  414. public function provideByReferenceCases()
  415. {
  416. return [
  417. [
  418. '<?php
  419. /**
  420. * something
  421. * @param mixed $numbers
  422. */
  423. function add(&$numbers) {}
  424. ',
  425. '<?php
  426. /**
  427. * something
  428. */
  429. function add(&$numbers) {}
  430. ',
  431. ],
  432. [
  433. '<?php
  434. /**
  435. * something
  436. * @param null|array $numbers
  437. */
  438. function add(array &$numbers = null) {}
  439. ',
  440. '<?php
  441. /**
  442. * something
  443. */
  444. function add(array &$numbers = null) {}
  445. ',
  446. ],
  447. ];
  448. }
  449. /**
  450. * @param string $expected
  451. * @param string $input
  452. *
  453. * @dataProvider provideVariableNumberOfArgumentsCases
  454. */
  455. public function testVariableNumberOfArguments($expected, $input)
  456. {
  457. $this->fixer->configure(['only_untyped' => false]);
  458. $this->doTest($expected, $input);
  459. }
  460. public function provideVariableNumberOfArgumentsCases()
  461. {
  462. return [
  463. [
  464. '<?php
  465. /**
  466. * something
  467. * @param array $numbers
  468. */
  469. function sum(...$numbers) {}
  470. ',
  471. '<?php
  472. /**
  473. * something
  474. */
  475. function sum(...$numbers) {}
  476. ',
  477. ],
  478. [
  479. '<?php
  480. /**
  481. * @param int $a
  482. * @param array $numbers
  483. */
  484. function sum($a, ...$numbers) {}
  485. ',
  486. '<?php
  487. /**
  488. * @param int $a
  489. */
  490. function sum($a, ...$numbers) {}
  491. ',
  492. ],
  493. [
  494. '<?php
  495. /**
  496. * @param \Date[] $numbers
  497. */
  498. function sum(\Date ...$numbers) {}
  499. ',
  500. '<?php
  501. /**
  502. */
  503. function sum(\Date ...$numbers) {}
  504. ',
  505. ],
  506. ];
  507. }
  508. }