PhpdocScalarFixerTest.php 6.2 KB

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