PhpdocScalarFixerTest.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  1. <?php
  2. /*
  3. * This file is part of PHP CS Fixer.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. * Dariusz Rumiński <dariusz.ruminski@gmail.com>
  7. *
  8. * This source file is subject to the MIT license that is bundled
  9. * with this source code in the file LICENSE.
  10. */
  11. namespace PhpCsFixer\Tests\Fixer\Phpdoc;
  12. use PhpCsFixer\Tests\Test\AbstractFixerTestCase;
  13. /**
  14. * @author Graham Campbell <graham@alt-three.com>
  15. *
  16. * @internal
  17. *
  18. * @covers \PhpCsFixer\AbstractPhpdocTypesFixer
  19. * @covers \PhpCsFixer\Fixer\Phpdoc\PhpdocScalarFixer
  20. */
  21. final class PhpdocScalarFixerTest extends AbstractFixerTestCase
  22. {
  23. public function testBasicFix()
  24. {
  25. $expected = <<<'EOF'
  26. <?php
  27. /**
  28. * @return int
  29. */
  30. EOF;
  31. $input = <<<'EOF'
  32. <?php
  33. /**
  34. * @return integer
  35. */
  36. EOF;
  37. $this->doTest($expected, $input);
  38. }
  39. public function testPropertyFix()
  40. {
  41. $expected = <<<'EOF'
  42. <?php
  43. /**
  44. * @method int foo()
  45. * @property int $foo
  46. * @property callback $foo
  47. * @property-read bool $bar
  48. * @property-write float $baz
  49. */
  50. EOF;
  51. $input = <<<'EOF'
  52. <?php
  53. /**
  54. * @method integer foo()
  55. * @property integer $foo
  56. * @property callback $foo
  57. * @property-read boolean $bar
  58. * @property-write double $baz
  59. */
  60. EOF;
  61. $this->doTest($expected, $input);
  62. }
  63. public function testDoNotModifyVariables()
  64. {
  65. $expected = <<<'EOF'
  66. <?php
  67. /**
  68. * @param int $integer
  69. */
  70. EOF;
  71. $input = <<<'EOF'
  72. <?php
  73. /**
  74. * @param integer $integer
  75. */
  76. EOF;
  77. $this->doTest($expected, $input);
  78. }
  79. public function testFixWithTabsOnOneLine()
  80. {
  81. $expected = "<?php /**\t@return\tbool\t*/";
  82. $input = "<?php /**\t@return\tboolean\t*/";
  83. $this->doTest($expected, $input);
  84. }
  85. public function testFixMoreThings()
  86. {
  87. $expected = <<<'EOF'
  88. <?php
  89. /**
  90. * Hello there mr integer!
  91. *
  92. * @param int|float $integer
  93. * @param int|int[] $foo
  94. * @param string|null $bar
  95. *
  96. * @return string|bool
  97. */
  98. EOF;
  99. $input = <<<'EOF'
  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. EOF;
  111. $this->doTest($expected, $input);
  112. }
  113. public function testFixVar()
  114. {
  115. $expected = <<<'EOF'
  116. <?php
  117. /**
  118. * @var int Some integer value.
  119. */
  120. EOF;
  121. $input = <<<'EOF'
  122. <?php
  123. /**
  124. * @var integer Some integer value.
  125. */
  126. EOF;
  127. $this->doTest($expected, $input);
  128. }
  129. public function testFixVarWithMoreStuff()
  130. {
  131. $expected = <<<'EOF'
  132. <?php
  133. /**
  134. * @var bool|int|Double Booleans, integers and doubles.
  135. */
  136. EOF;
  137. $input = <<<'EOF'
  138. <?php
  139. /**
  140. * @var boolean|integer|Double Booleans, integers and doubles.
  141. */
  142. EOF;
  143. $this->doTest($expected, $input);
  144. }
  145. public function testFixType()
  146. {
  147. $expected = <<<'EOF'
  148. <?php
  149. /**
  150. * @type float
  151. */
  152. EOF;
  153. $input = <<<'EOF'
  154. <?php
  155. /**
  156. * @type real
  157. */
  158. EOF;
  159. $this->doTest($expected, $input);
  160. }
  161. public function testDoNotFix()
  162. {
  163. $expected = <<<'EOF'
  164. <?php
  165. /**
  166. * @var notaboolean
  167. */
  168. EOF;
  169. $this->doTest($expected);
  170. }
  171. public function testComplexMix()
  172. {
  173. $expected = <<<'EOF'
  174. <?php
  175. /**
  176. * @var notabooleanthistime|bool|integerr
  177. */
  178. EOF;
  179. $input = <<<'EOF'
  180. <?php
  181. /**
  182. * @var notabooleanthistime|boolean|integerr
  183. */
  184. EOF;
  185. $this->doTest($expected, $input);
  186. }
  187. public function testDoNotModifyComplexTag()
  188. {
  189. $expected = <<<'EOF'
  190. <?php
  191. /**
  192. * @Type("boolean")
  193. */
  194. EOF;
  195. $this->doTest($expected);
  196. }
  197. public function testDoNotModifyStrings()
  198. {
  199. $expected = <<<'EOF'
  200. <?php
  201. $string = '
  202. /**
  203. * @var boolean
  204. */
  205. ';
  206. EOF;
  207. $this->doTest($expected);
  208. }
  209. public function testEmptyDocBlock()
  210. {
  211. $expected = <<<'EOF'
  212. <?php
  213. /**
  214. *
  215. */
  216. EOF;
  217. $this->doTest($expected);
  218. }
  219. public function testWrongCasedPhpdocTagIsNotAltered()
  220. {
  221. $expected = <<<'EOF'
  222. <?php
  223. /**
  224. * @Param boolean
  225. *
  226. * @Return int
  227. */
  228. EOF;
  229. $this->doTest($expected);
  230. }
  231. public function testInlineDoc()
  232. {
  233. $expected = <<<'EOF'
  234. <?php
  235. /**
  236. * Does stuffs with stuffs.
  237. *
  238. * @param array $stuffs {
  239. * @type bool $foo
  240. * @type int $bar
  241. * }
  242. */
  243. EOF;
  244. $input = <<<'EOF'
  245. <?php
  246. /**
  247. * Does stuffs with stuffs.
  248. *
  249. * @param array $stuffs {
  250. * @type boolean $foo
  251. * @type integer $bar
  252. * }
  253. */
  254. EOF;
  255. $this->doTest($expected, $input);
  256. }
  257. public function testFixCallback()
  258. {
  259. $expected = <<<'EOF'
  260. <?php
  261. /**
  262. * @method int foo()
  263. * @property int $foo
  264. * @property callable $foo
  265. * @property-read bool $bar
  266. * @property-write float $baz
  267. */
  268. EOF;
  269. $input = <<<'EOF'
  270. <?php
  271. /**
  272. * @method integer foo()
  273. * @property integer $foo
  274. * @property callback $foo
  275. * @property-read boolean $bar
  276. * @property-write double $baz
  277. */
  278. EOF;
  279. $this->fixer->configure(['types' => ['boolean', 'callback', 'double', 'integer', 'real', 'str']]);
  280. $this->doTest($expected, $input);
  281. }
  282. }