PhpdocTypesFixerTest.php 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  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\ConfigurationException\InvalidFixerConfigurationException;
  14. use PhpCsFixer\Tests\Test\AbstractFixerTestCase;
  15. /**
  16. * @author Graham Campbell <hello@gjcampbell.co.uk>
  17. * @author Dariusz Rumiński <dariusz.ruminski@gmail.com>
  18. *
  19. * @internal
  20. *
  21. * @covers \PhpCsFixer\AbstractPhpdocTypesFixer
  22. * @covers \PhpCsFixer\Fixer\Phpdoc\PhpdocTypesFixer
  23. *
  24. * @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\Phpdoc\PhpdocTypesFixer>
  25. *
  26. * @phpstan-import-type _AutogeneratedInputConfiguration from \PhpCsFixer\Fixer\Phpdoc\PhpdocTypesFixer
  27. */
  28. final class PhpdocTypesFixerTest extends AbstractFixerTestCase
  29. {
  30. /**
  31. * @dataProvider provideFixCases
  32. *
  33. * @param _AutogeneratedInputConfiguration $configuration
  34. */
  35. public function testFix(string $expected, ?string $input = null, array $configuration = []): void
  36. {
  37. $this->fixer->configure($configuration);
  38. $this->doTest($expected, $input);
  39. }
  40. public static function provideFixCases(): iterable
  41. {
  42. yield 'windows line breaks' => [
  43. "<?php /**\r\n * @param string|string[] \$bar\r\n *\r\n * @return int[]\r\n */\r\n",
  44. "<?php /**\r\n * @param STRING|String[] \$bar\r\n *\r\n * @return inT[]\r\n */\r\n",
  45. ];
  46. yield 'conversion' => [
  47. '<?php
  48. /**
  49. * @param boolean|array|Foo $bar
  50. *
  51. * @return int|float
  52. */
  53. ',
  54. '<?php
  55. /**
  56. * @param Boolean|Array|Foo $bar
  57. *
  58. * @return inT|Float
  59. */
  60. ',
  61. ];
  62. yield 'array stuff' => [
  63. '<?php
  64. /**
  65. * @param string|string[] $bar
  66. *
  67. * @return int[]
  68. */
  69. ',
  70. '<?php
  71. /**
  72. * @param STRING|String[] $bar
  73. *
  74. * @return inT[]
  75. */
  76. ',
  77. ];
  78. yield 'nested array stuff' => [
  79. '<?php
  80. /**
  81. * @return int[][][]
  82. */
  83. ',
  84. '<?php
  85. /**
  86. * @return INT[][][]
  87. */
  88. ',
  89. ];
  90. yield 'mixed and void' => [
  91. '<?php
  92. /**
  93. * @param mixed $foo
  94. *
  95. * @return void
  96. */
  97. ',
  98. '<?php
  99. /**
  100. * @param Mixed $foo
  101. *
  102. * @return Void
  103. */
  104. ',
  105. ];
  106. yield 'iterable' => [
  107. '<?php
  108. /**
  109. * @param iterable $foo
  110. *
  111. * @return Itter
  112. */
  113. ',
  114. '<?php
  115. /**
  116. * @param Iterable $foo
  117. *
  118. * @return Itter
  119. */
  120. ',
  121. ];
  122. yield 'method and property' => [
  123. '<?php
  124. /**
  125. * @method self foo()
  126. * @property int $foo
  127. * @property-read boolean $bar
  128. * @property-write mixed $baz
  129. */
  130. ',
  131. '<?php
  132. /**
  133. * @method Self foo()
  134. * @property Int $foo
  135. * @property-read Boolean $bar
  136. * @property-write MIXED $baz
  137. */
  138. ',
  139. ];
  140. yield 'throws' => [
  141. '<?php
  142. /**
  143. * @throws static
  144. */
  145. ',
  146. '<?php
  147. /**
  148. * @throws STATIC
  149. */
  150. ',
  151. ];
  152. yield 'inline doc' => [
  153. '<?php
  154. /**
  155. * Does stuff with stuffs.
  156. *
  157. * @param array $stuffs {
  158. * @var bool $foo
  159. * @var int $bar
  160. * }
  161. */
  162. ',
  163. '<?php
  164. /**
  165. * Does stuff with stuffs.
  166. *
  167. * @param array $stuffs {
  168. * @var Bool $foo
  169. * @var INT $bar
  170. * }
  171. */
  172. ',
  173. ];
  174. yield 'with config' => [
  175. '<?php
  176. /**
  177. * @param self|array|Foo $bar
  178. *
  179. * @return int|float|boolean|Double
  180. */
  181. ',
  182. '<?php
  183. /**
  184. * @param SELF|Array|Foo $bar
  185. *
  186. * @return inT|Float|boolean|Double
  187. */
  188. ',
  189. ['groups' => ['simple', 'meta']],
  190. ];
  191. yield 'generics' => [
  192. '<?php
  193. /**
  194. * @param array<int, object> $a
  195. * @param array<iterable> $b
  196. * @param array<parent|$this|self> $c
  197. * @param iterable<Foo\Int\Bar|Foo\Int|Int\Bar> $thisShouldNotBeChanged
  198. * @param iterable<BOOLBOOLBOOL|INTINTINT|ARRAY_BOOL_INT_STRING_> $thisShouldNotBeChangedNeither
  199. *
  200. * @return array<int, array<string, array<int, DoNotChangeThisAsThisIsAClass>>>
  201. */',
  202. '<?php
  203. /**
  204. * @param ARRAY<INT, OBJECT> $a
  205. * @param ARRAY<ITERABLE> $b
  206. * @param array<Parent|$This|Self> $c
  207. * @param iterable<Foo\Int\Bar|Foo\Int|Int\Bar> $thisShouldNotBeChanged
  208. * @param iterable<BOOLBOOLBOOL|INTINTINT|ARRAY_BOOL_INT_STRING_> $thisShouldNotBeChangedNeither
  209. *
  210. * @return ARRAY<INT, ARRAY<STRING, ARRAY<INT, DoNotChangeThisAsThisIsAClass>>>
  211. */',
  212. ['groups' => ['simple', 'meta']],
  213. ];
  214. yield 'callable' => [
  215. '<?php /**
  216. * @param callable() $a
  217. * @param callable(): void $b
  218. * @param callable(bool, int, string): float $c
  219. */',
  220. '<?php /**
  221. * @param CALLABLE() $a
  222. * @param Callable(): VOID $b
  223. * @param CALLABLE(BOOL, INT, STRING): FLOAT $c
  224. */',
  225. ];
  226. yield 'array shape with key name being also type name' => [
  227. '<?php /**
  228. * @return array{FOO: bool, NULL: null|int, BAR: string|BAZ}
  229. */',
  230. '<?php /**
  231. * @return ARRAY{FOO: BOOL, NULL: NULL|INT, BAR: STRING|BAZ}
  232. */',
  233. ];
  234. yield 'union with \'NULL\'' => [
  235. '<?php /**
  236. * @return \'NULL\'|null|false
  237. */',
  238. '<?php /**
  239. * @return \'NULL\'|NULL|false
  240. */',
  241. ];
  242. yield 'union with "NULL"' => [
  243. '<?php /**
  244. * @return null|"NULL"|false
  245. */',
  246. '<?php /**
  247. * @return NULL|"NULL"|false
  248. */',
  249. ];
  250. yield 'method with reserved identifier' => [
  251. '<?php /**
  252. * @method bool BOOL(): void
  253. */',
  254. '<?php /**
  255. * @method BOOL BOOL(): void
  256. */',
  257. ];
  258. yield 'no space between type and variable' => [
  259. '<?php /** @param null|string$foo */',
  260. '<?php /** @param NULL|STRING$foo */',
  261. ];
  262. yield '"Callback" class in phpdoc must not be lowered' => [
  263. '<?php
  264. /**
  265. * @param Callback $foo
  266. *
  267. * @return Callback
  268. */
  269. ',
  270. ];
  271. yield 'param with extra chevrons' => [
  272. '<?php /** @param array <3> $value */',
  273. '<?php /** @param ARRAY <3> $value */',
  274. ];
  275. yield 'param with extra parentheses' => [
  276. '<?php /** @param \Closure (int) $value */',
  277. '<?php /** @param \Closure (INT) $value */',
  278. ];
  279. yield 'param with union type and extra parentheses' => [
  280. '<?php /** @param \Closure (float|int) $value */',
  281. '<?php /** @param \Closure (FLOAT|INT) $value */',
  282. ];
  283. yield 'return with union type and extra parentheses' => [
  284. '<?php /** @return float|int (number) count of something */',
  285. '<?php /** @return FLOAT|INT (number) count of something */',
  286. ];
  287. }
  288. public function testInvalidConfiguration(): void
  289. {
  290. $this->expectException(InvalidFixerConfigurationException::class);
  291. $this->expectExceptionMessageMatches('/^\[phpdoc_types\] Invalid configuration: The option "groups" .*\.$/');
  292. $this->fixer->configure(['groups' => ['__TEST__']]); // @phpstan-ignore-line
  293. }
  294. }