PhpdocVarWithoutNameFixerTest.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659
  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 Graham Campbell <hello@gjcampbell.co.uk>
  16. *
  17. * @internal
  18. *
  19. * @covers \PhpCsFixer\Fixer\Phpdoc\PhpdocVarWithoutNameFixer
  20. */
  21. final class PhpdocVarWithoutNameFixerTest extends AbstractFixerTestCase
  22. {
  23. /**
  24. * @dataProvider provideFixVarCases
  25. */
  26. public function testFixVar(string $expected, ?string $input = null): void
  27. {
  28. $this->doTest($expected, $input);
  29. }
  30. /**
  31. * @dataProvider provideFixVarCases
  32. */
  33. public function testFixType(string $expected, ?string $input = null): void
  34. {
  35. $expected = str_replace('@var', '@type', $expected);
  36. if (null !== $input) {
  37. $input = str_replace('@var', '@type', $input);
  38. }
  39. $this->doTest($expected, $input);
  40. }
  41. public static function provideFixVarCases(): iterable
  42. {
  43. yield 'testFixVar' => [
  44. <<<'EOF'
  45. <?php
  46. class Foo
  47. {
  48. /**
  49. * @var string Hello!
  50. */
  51. public $foo;
  52. }
  53. EOF
  54. ,
  55. <<<'EOF'
  56. <?php
  57. class Foo
  58. {
  59. /**
  60. * @var string $foo Hello!
  61. */
  62. public $foo;
  63. }
  64. EOF
  65. ,
  66. ];
  67. yield 'testFixType' => [
  68. <<<'EOF'
  69. <?php
  70. class Foo
  71. {
  72. /**
  73. * @var int|null
  74. */
  75. public $bar;
  76. }
  77. EOF
  78. ,
  79. <<<'EOF'
  80. <?php
  81. class Foo
  82. {
  83. /**
  84. * @var int|null $bar
  85. */
  86. public $bar;
  87. }
  88. EOF
  89. ,
  90. ];
  91. yield 'testDoNothing' => [
  92. <<<'EOF'
  93. <?php
  94. class Foo
  95. {
  96. /**
  97. * @var Foo\Bar This is a variable.
  98. */
  99. public $bar;
  100. }
  101. EOF
  102. ];
  103. yield 'testFixVarWithNestedKeys' => [
  104. <<<'EOF'
  105. <?php
  106. class Foo
  107. {
  108. /**
  109. * @var array {
  110. * @var bool $required Whether this element is required
  111. * @var string $label The display name for this element
  112. * }
  113. */
  114. public $options;
  115. }
  116. EOF
  117. ,
  118. <<<'EOF'
  119. <?php
  120. class Foo
  121. {
  122. /**
  123. * @var array $options {
  124. * @var bool $required Whether this element is required
  125. * @var string $label The display name for this element
  126. * }
  127. */
  128. public $options;
  129. }
  130. EOF
  131. ];
  132. yield 'testSingleLine' => [
  133. <<<'EOF'
  134. <?php
  135. class Foo
  136. {
  137. /** @var Foo\Bar */
  138. public $bar;
  139. }
  140. EOF
  141. ,
  142. <<<'EOF'
  143. <?php
  144. class Foo
  145. {
  146. /** @var Foo\Bar $bar */
  147. public $bar;
  148. }
  149. EOF
  150. ,
  151. ];
  152. yield 'testSingleLineProtected' => [
  153. <<<'EOF'
  154. <?php
  155. class Foo
  156. {
  157. /** @var Foo\Bar */
  158. protected $bar;
  159. }
  160. EOF
  161. ,
  162. <<<'EOF'
  163. <?php
  164. class Foo
  165. {
  166. /** @var Foo\Bar $bar */
  167. protected $bar;
  168. }
  169. EOF
  170. ,
  171. ];
  172. yield 'testSingleLinePrivate' => [
  173. <<<'EOF'
  174. <?php
  175. class Foo
  176. {
  177. /** @var Foo\Bar */
  178. private $bar;
  179. }
  180. EOF
  181. ,
  182. <<<'EOF'
  183. <?php
  184. class Foo
  185. {
  186. /** @var Foo\Bar $bar */
  187. private $bar;
  188. }
  189. EOF
  190. ,
  191. ];
  192. yield 'testSingleLineVar' => [
  193. <<<'EOF'
  194. <?php
  195. class Foo
  196. {
  197. /** @var Foo\Bar */
  198. var $bar;
  199. }
  200. EOF
  201. ,
  202. <<<'EOF'
  203. <?php
  204. class Foo
  205. {
  206. /** @var Foo\Bar $bar */
  207. var $bar;
  208. }
  209. EOF
  210. ,
  211. ];
  212. yield 'testSingleLineStatic' => [
  213. <<<'EOF'
  214. <?php
  215. class Foo
  216. {
  217. /** @var Foo\Bar */
  218. static public $bar;
  219. }
  220. EOF
  221. ,
  222. <<<'EOF'
  223. <?php
  224. class Foo
  225. {
  226. /** @var Foo\Bar $bar */
  227. static public $bar;
  228. }
  229. EOF
  230. ,
  231. ];
  232. yield 'testSingleLineNoSpace' => [
  233. <<<'EOF'
  234. <?php
  235. class Foo
  236. {
  237. /** @var Foo\Bar*/
  238. public $bar;
  239. }
  240. EOF
  241. ,
  242. <<<'EOF'
  243. <?php
  244. class Foo
  245. {
  246. /** @var Foo\Bar $bar*/
  247. public $bar;
  248. }
  249. EOF
  250. ,
  251. ];
  252. yield 'testInlineDoc' => [
  253. <<<'EOF'
  254. <?php
  255. class Foo
  256. {
  257. /**
  258. * Initializes this class with the given options.
  259. *
  260. * @param array $options {
  261. * @var bool $required Whether this element is required
  262. * @var string $label The display name for this element
  263. * }
  264. */
  265. public function init($options)
  266. {
  267. // Do something
  268. }
  269. }
  270. EOF
  271. ,
  272. ];
  273. yield 'testSingleLineNoProperty' => [
  274. <<<'EOF'
  275. <?php
  276. /** @var Foo\Bar $bar */
  277. $bar;
  278. EOF
  279. ];
  280. yield 'testMultiLineNoProperty' => [
  281. <<<'EOF'
  282. <?php
  283. /**
  284. * @var Foo\Bar $bar
  285. */
  286. $bar;
  287. EOF
  288. ];
  289. yield 'testVeryNestedInlineDoc' => [
  290. <<<'EOF'
  291. <?php
  292. class Foo
  293. {
  294. /**
  295. * @var array {
  296. * @var array $secondLevelOne {
  297. * {@internal This should not break}
  298. * @var int $thirdLevel
  299. * }
  300. * @var array $secondLevelTwo {
  301. * @var array $thirdLevel {
  302. * @var string $fourthLevel
  303. * }
  304. * @var int $moreThirdLevel
  305. * }
  306. * @var int $secondLevelThree
  307. * }
  308. */
  309. public $nestedFoo;
  310. }
  311. EOF
  312. ,
  313. <<<'EOF'
  314. <?php
  315. class Foo
  316. {
  317. /**
  318. * @var array $nestedFoo {
  319. * @var array $secondLevelOne {
  320. * {@internal This should not break}
  321. * @var int $thirdLevel
  322. * }
  323. * @var array $secondLevelTwo {
  324. * @var array $thirdLevel {
  325. * @var string $fourthLevel
  326. * }
  327. * @var int $moreThirdLevel
  328. * }
  329. * @var int $secondLevelThree
  330. * }
  331. */
  332. public $nestedFoo;
  333. }
  334. EOF
  335. ];
  336. yield [
  337. '<?php
  338. class Foo
  339. {
  340. /**
  341. * @no_candidate string Hello!
  342. */
  343. public $foo;
  344. }
  345. ',
  346. ];
  347. yield [
  348. '<?php
  349. class Foo{}
  350. /** */',
  351. ];
  352. yield 'anonymousClass' => [
  353. <<<'EOF'
  354. <?php
  355. class Anon
  356. {
  357. public function getNewAnon()
  358. {
  359. return new class()
  360. {
  361. /**
  362. * @var string
  363. */
  364. public $stringVar;
  365. public function getNewAnon()
  366. {
  367. return new class()
  368. {
  369. /**
  370. * @var string
  371. */
  372. public $stringVar;
  373. };
  374. }
  375. };
  376. }
  377. }
  378. EOF
  379. ,
  380. <<<'EOF'
  381. <?php
  382. class Anon
  383. {
  384. public function getNewAnon()
  385. {
  386. return new class()
  387. {
  388. /**
  389. * @var $stringVar string
  390. */
  391. public $stringVar;
  392. public function getNewAnon()
  393. {
  394. return new class()
  395. {
  396. /**
  397. * @var $stringVar string
  398. */
  399. public $stringVar;
  400. };
  401. }
  402. };
  403. }
  404. }
  405. EOF
  406. ,
  407. ];
  408. yield [
  409. '<?php
  410. /**
  411. * Header
  412. */
  413. class A {} // for the candidate check
  414. /**
  415. * @var ClassLoader $loader
  416. */
  417. $loader = require __DIR__.\'/../vendor/autoload.php\';
  418. /**
  419. * @var \Foo\Bar $bar
  420. */
  421. $bar->doSomething(1);
  422. /**
  423. * @var $bar \Foo\Bar
  424. */
  425. $bar->doSomething(2);
  426. /**
  427. * @var User $bar
  428. */
  429. ($bar = tmp())->doSomething(3);
  430. /**
  431. * @var User $bar
  432. */
  433. list($bar) = a();
  434. ',
  435. ];
  436. yield 'const are not handled by this fixer' => [
  437. '<?php
  438. class A
  439. {
  440. /**
  441. * @var array<string, true> SKIPPED_TYPES
  442. */
  443. private const SKIPPED_TYPES = ["a" => true];
  444. }
  445. ',
  446. ];
  447. yield 'trait' => [
  448. '<?php
  449. trait StaticExample {
  450. /**
  451. * @var string Hello!
  452. */
  453. public static $static = "foo";
  454. }',
  455. '<?php
  456. trait StaticExample {
  457. /**
  458. * @var string $static Hello!
  459. */
  460. public static $static = "foo";
  461. }',
  462. ];
  463. yield 'complex type with union containing callable that has `$this` in signature' => [
  464. <<<'EOF'
  465. <?php
  466. class Foo
  467. {
  468. /**
  469. * @var array<string, string|array{ string|\Closure(mixed, string, $this): int|float }>|false Hello!
  470. */
  471. public $foo;
  472. /** @var int Hello! */
  473. public $foo2;
  474. /** @var int Hello! */
  475. public $foo3;
  476. /** @var int Hello! */
  477. public $foo4;
  478. }
  479. EOF
  480. ,
  481. <<<'EOF'
  482. <?php
  483. class Foo
  484. {
  485. /**
  486. * @var array<string, string|array{ string|\Closure(mixed, string, $this): int|float }>|false $foo Hello!
  487. */
  488. public $foo;
  489. /** @var int $thi Hello! */
  490. public $foo2;
  491. /** @var int $thiss Hello! */
  492. public $foo3;
  493. /** @var int $this2 Hello! */
  494. public $foo4;
  495. }
  496. EOF
  497. ,
  498. ];
  499. yield 'testFixMultibyteVariableName' => [
  500. <<<'EOF'
  501. <?php
  502. class Foo
  503. {
  504. /** @var int Hello! */
  505. public $foo;
  506. /** @var ๐Ÿš€ ๐Ÿš€ */
  507. public $foo2;
  508. }
  509. EOF
  510. ,
  511. <<<'EOF'
  512. <?php
  513. class Foo
  514. {
  515. /** @var int $my๐Ÿš€ Hello! */
  516. public $foo;
  517. /** @var ๐Ÿš€ $my ๐Ÿš€ */
  518. public $foo2;
  519. }
  520. EOF
  521. ,
  522. ];
  523. }
  524. /**
  525. * @dataProvider provideFix81Cases
  526. *
  527. * @requires PHP 8.1
  528. */
  529. public function testFix81(string $expected, ?string $input = null): void
  530. {
  531. $this->doTest($expected, $input);
  532. }
  533. public static function provideFix81Cases(): iterable
  534. {
  535. yield 'readonly' => [
  536. '<?php
  537. class Foo
  538. {
  539. /** @var Foo */
  540. public $bar1;
  541. /** @var Foo */
  542. public readonly int $bar2;
  543. /** @var Foo */
  544. readonly public int $bar3;
  545. /** @var Foo */
  546. readonly int $bar4;
  547. }',
  548. '<?php
  549. class Foo
  550. {
  551. /** @var Foo $bar1 */
  552. public $bar1;
  553. /** @var Foo $bar2 */
  554. public readonly int $bar2;
  555. /** @var Foo $bar3 */
  556. readonly public int $bar3;
  557. /** @var Foo $bar4 */
  558. readonly int $bar4;
  559. }',
  560. ];
  561. yield 'final public const are not handled by this fixer' => [
  562. '<?php
  563. class A
  564. {
  565. /**
  566. * @var array<string, true> SKIPPED_TYPES
  567. */
  568. final public const SKIPPED_TYPES = ["a" => true];
  569. }
  570. ',
  571. ];
  572. }
  573. }