PhpdocAddMissingParamAnnotationFixerTest.php 12 KB

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