PhpdocScalarFixerTest.php 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  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\AbstractPhpdocTypesFixer
  20. * @covers \PhpCsFixer\Fixer\Phpdoc\PhpdocScalarFixer
  21. *
  22. * @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\Phpdoc\PhpdocScalarFixer>
  23. *
  24. * @phpstan-import-type _AutogeneratedInputConfiguration from \PhpCsFixer\Fixer\Phpdoc\PhpdocScalarFixer
  25. */
  26. final class PhpdocScalarFixerTest extends AbstractFixerTestCase
  27. {
  28. /**
  29. * @param _AutogeneratedInputConfiguration $config
  30. *
  31. * @dataProvider provideFixCases
  32. */
  33. public function testFix(string $expected, ?string $input = null, array $config = []): void
  34. {
  35. $this->fixer->configure($config);
  36. $this->doTest($expected, $input);
  37. }
  38. public static function provideFixCases(): iterable
  39. {
  40. yield 'basic fix' => [
  41. '<?php
  42. /**
  43. * @return int
  44. */
  45. ',
  46. '<?php
  47. /**
  48. * @return integer
  49. */
  50. ',
  51. ];
  52. yield 'property fix' => [
  53. '<?php
  54. /**
  55. * @method int foo()
  56. * @property int $foo
  57. * @property callable $foo
  58. * @property-read bool $bar
  59. * @property-write float $baz
  60. */
  61. ',
  62. '<?php
  63. /**
  64. * @method integer foo()
  65. * @property integer $foo
  66. * @property callback $foo
  67. * @property-read boolean $bar
  68. * @property-write double $baz
  69. */
  70. ',
  71. ];
  72. yield 'do not modify variables' => [
  73. '<?php
  74. /**
  75. * @param int $integer
  76. */
  77. ',
  78. '<?php
  79. /**
  80. * @param integer $integer
  81. */
  82. ',
  83. ];
  84. yield 'fix with tabs on one line' => [
  85. "<?php /**\t@return\tbool\t*/",
  86. "<?php /**\t@return\tboolean\t*/",
  87. ];
  88. yield 'fix more things' => [
  89. '<?php
  90. /**
  91. * Hello there mr integer!
  92. *
  93. * @param int|float $integer
  94. * @param int|int[] $foo
  95. * @param string|null $bar
  96. *
  97. * @return string|bool
  98. */
  99. ',
  100. '<?php
  101. /**
  102. * Hello there mr integer!
  103. *
  104. * @param integer|real $integer
  105. * @param int|integer[] $foo
  106. * @param str|null $bar
  107. *
  108. * @return string|boolean
  109. */
  110. ',
  111. ];
  112. yield 'fix var' => [
  113. '<?php
  114. /**
  115. * @var int Some integer value.
  116. */
  117. ',
  118. '<?php
  119. /**
  120. * @var integer Some integer value.
  121. */
  122. ',
  123. ];
  124. yield 'fix var with more stuff' => [
  125. '<?php
  126. /**
  127. * @var bool|int|Double Booleans, integers and doubles.
  128. */
  129. ',
  130. '<?php
  131. /**
  132. * @var boolean|integer|Double Booleans, integers and doubles.
  133. */
  134. ',
  135. ];
  136. yield 'fix type' => [
  137. '<?php
  138. /**
  139. * @type float
  140. */
  141. ',
  142. '<?php
  143. /**
  144. * @type real
  145. */
  146. ',
  147. ];
  148. yield 'do not fix' => [
  149. '<?php
  150. /**
  151. * @var notaboolean
  152. */
  153. ',
  154. ];
  155. yield 'complex mix' => [
  156. '<?php
  157. /**
  158. * @var notabooleanthistime|bool|integerr
  159. */
  160. ',
  161. '<?php
  162. /**
  163. * @var notabooleanthistime|boolean|integerr
  164. */
  165. ',
  166. ];
  167. yield 'do not modify complex tag' => [
  168. '<?php
  169. /**
  170. * @Type("boolean")
  171. */
  172. ',
  173. ];
  174. yield 'do not modify strings' => [
  175. "<?php
  176. \$string = '
  177. /**
  178. * @var boolean
  179. */
  180. ';
  181. ",
  182. ];
  183. yield 'empty DocBlock' => [
  184. '<?php
  185. /**
  186. *
  187. */
  188. ',
  189. ];
  190. yield 'wrong cased Phpdoc tag is not altered' => [
  191. '<?php
  192. /**
  193. * @Param boolean
  194. *
  195. * @Return int
  196. */
  197. ',
  198. ];
  199. yield 'inline doc' => [
  200. '<?php
  201. /**
  202. * Does stuff with stuffs.
  203. *
  204. * @param array $stuffs {
  205. * @type bool $foo
  206. * @type int $bar
  207. * }
  208. */
  209. ',
  210. '<?php
  211. /**
  212. * Does stuff with stuffs.
  213. *
  214. * @param array $stuffs {
  215. * @type boolean $foo
  216. * @type integer $bar
  217. * }
  218. */
  219. ',
  220. ];
  221. yield 'fix callback' => [
  222. '<?php
  223. /**
  224. * @method int foo()
  225. * @property int $foo
  226. * @property callable $foo
  227. * @property-read bool $bar
  228. * @property-write float $baz
  229. */
  230. ',
  231. '<?php
  232. /**
  233. * @method integer foo()
  234. * @property integer $foo
  235. * @property callback $foo
  236. * @property-read boolean $bar
  237. * @property-write double $baz
  238. */
  239. ',
  240. ['types' => ['boolean', 'callback', 'double', 'integer', 'real', 'str']],
  241. ];
  242. yield 'fix Windows line endings' => [
  243. str_replace("\n", "\r\n", '<?php
  244. /**
  245. * @return int
  246. */
  247. '),
  248. str_replace("\n", "\r\n", '<?php
  249. /**
  250. * @return integer
  251. */
  252. '),
  253. ];
  254. yield [
  255. '<?php /** @var array<int, bool> */',
  256. '<?php /** @var array<integer, boolean> */',
  257. ];
  258. yield [
  259. '<?php /** @var array{bool, int, string} */',
  260. '<?php /** @var array{boolean, integer, str} */',
  261. ];
  262. yield [
  263. '<?php /** @var array{int, array{string, bool}} */',
  264. '<?php /** @var array{integer, array{str, boolean}} */',
  265. ];
  266. yield [
  267. '<?php /** @var array{index: int, isRequired: bool} */',
  268. '<?php /** @var array{index: integer, isRequired: boolean} */',
  269. ];
  270. }
  271. }