SetTypeToCastFixerTest.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  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\Alias;
  13. use PhpCsFixer\Tests\Test\AbstractFixerTestCase;
  14. /**
  15. * @internal
  16. *
  17. * @covers \PhpCsFixer\Fixer\Alias\SetTypeToCastFixer
  18. *
  19. * @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\Alias\SetTypeToCastFixer>
  20. */
  21. final class SetTypeToCastFixerTest extends AbstractFixerTestCase
  22. {
  23. /**
  24. * @dataProvider provideFixCases
  25. */
  26. public function testFix(string $expected, ?string $input = null): void
  27. {
  28. $this->doTest($expected, $input);
  29. }
  30. /**
  31. * @return iterable<string, array{0: string, 1?: string}>
  32. */
  33. public static function provideFixCases(): iterable
  34. {
  35. yield 'null cast' => [
  36. '<?php $foo = null;',
  37. '<?php settype($foo, "null");',
  38. ];
  39. yield 'null cast comments' => [
  40. '<?php
  41. # 0
  42. $foo = null# 1
  43. # 2
  44. # 3
  45. # 4
  46. # 5
  47. # 6
  48. # 7
  49. # 8
  50. # 9
  51. # 10
  52. ;',
  53. '<?php
  54. # 0
  55. settype# 1
  56. # 2
  57. (# 3
  58. # 4
  59. $foo# 5
  60. # 6
  61. ,# 7
  62. # 8
  63. "null"# 9
  64. )# 10
  65. ;',
  66. ];
  67. yield 'array + spacing' => [
  68. '<?php $foo = (array) $foo;',
  69. '<?php settype ( $foo , \'array\');',
  70. ];
  71. yield 'bool + casing' => [
  72. '<?php $foo = (bool) $foo;',
  73. '<?php settype ( $foo , "Bool");',
  74. ];
  75. yield 'boolean with extra space' => [
  76. '<?php $foo = (bool) $foo;',
  77. '<?php settype ( $foo , "boolean");',
  78. ];
  79. yield 'double' => [
  80. '<?php $foo = (float) $foo;',
  81. '<?php settype($foo, "double");',
  82. ];
  83. yield 'float' => [
  84. '<?php $foo = (float) $foo;',
  85. '<?php settype($foo, "float");',
  86. ];
  87. yield 'float in loop' => [
  88. '<?php while(a()){$foo = (float) $foo;}',
  89. '<?php while(a()){settype($foo, "float");}',
  90. ];
  91. yield 'int full caps' => [
  92. '<?php $foo = (int) $foo;',
  93. '<?php settype($foo, "INT");',
  94. ];
  95. yield 'integer (simple)' => [
  96. '<?php $foo = (int) $foo;',
  97. '<?php settype($foo, "integer");',
  98. ];
  99. yield 'object' => [
  100. '<?php echo 1; $foo = (object) $foo;',
  101. '<?php echo 1; settype($foo, "object");',
  102. ];
  103. yield 'string' => [
  104. '<?php $foo = (string) $foo;',
  105. '<?php settype($foo, "string");',
  106. ];
  107. yield 'string in function body' => [
  108. '<?php function A(){ $foo = (string) $foo; return $foo; }',
  109. '<?php function A(){ settype($foo, "string"); return $foo; }',
  110. ];
  111. yield 'integer + no space' => [
  112. '<?php $foo = (int) $foo;',
  113. '<?php settype($foo,"integer");',
  114. ];
  115. yield 'no space comments' => [
  116. '<?php /*0*//*1*/$foo = (int) $foo/*2*//*3*//*4*//*5*//*6*//*7*/;/*8*/',
  117. '<?php /*0*//*1*/settype/*2*/(/*3*/$foo/*4*/,/*5*/"integer"/*6*/)/*7*/;/*8*/',
  118. ];
  119. yield 'comments with line breaks' => [
  120. '<?php #0
  121. #1
  122. $foo = (int) $foo#2
  123. #3
  124. #4
  125. #5
  126. #6
  127. #7
  128. #8
  129. ;#9',
  130. '<?php #0
  131. #1
  132. settype#2
  133. #3
  134. (#4
  135. $foo#5
  136. ,#6
  137. "integer"#7
  138. )#8
  139. ;#9',
  140. ];
  141. // do not fix cases
  142. yield 'first argument is not a variable' => [
  143. '<?php
  144. namespace A\B; // comment
  145. function settype($a, $b){} // "
  146. settype(1, "double");
  147. ',
  148. ];
  149. yield 'first argument is variable followed by operation' => [
  150. '<?php
  151. namespace A\B; // comment
  152. function settype($a, $b){} // "
  153. settype($foo + 1, "integer"); // function must be overridden, so do not fix it
  154. ',
  155. ];
  156. yield 'wrong numbers of arguments' => [
  157. '<?php settype($foo, "integer", $a);',
  158. ];
  159. yield 'other namespace I' => [
  160. '<?php a\b\settype($foo, "integer", $a);',
  161. ];
  162. yield 'other namespace II' => [
  163. '<?php \b\settype($foo, "integer", $a);',
  164. ];
  165. yield 'static call' => [
  166. '<?php A::settype($foo, "integer");',
  167. ];
  168. yield 'member call' => [
  169. '<?php $a->settype($foo, "integer");',
  170. ];
  171. yield 'unknown type' => [
  172. '<?php $a->settype($foo, "foo");',
  173. ];
  174. yield 'return value used I' => [
  175. '<?php $a = settype($foo, "integer");',
  176. ];
  177. yield 'return value used II' => [
  178. '<?php a(settype($foo, "integer"));',
  179. ];
  180. yield 'return value used III' => [
  181. '<?php $a = "123"; $b = [settype($a, "integer")];',
  182. ];
  183. yield 'return value used IV' => [
  184. '<?php $a = "123"; $b = [3 => settype($a, "integer")];',
  185. ];
  186. yield 'return value used V' => [
  187. '<?= settype($foo, "object");',
  188. ];
  189. yield 'wrapped statements, fixable after removing the useless parenthesis brace' => [
  190. '<?php
  191. settype(/*1*//*2*/($a), \'int\');
  192. settype($b, (\'int\'));
  193. settype(($c), (\'int\'));
  194. settype((($d)), ((\'int\')));
  195. ',
  196. ];
  197. yield 'wrapped statements, not-fixable, even after removing the useless parenthesis brace' => [
  198. '<?php
  199. namespace A\B; // comment
  200. function settype($a, $b){} // "
  201. settype($foo1, (("integer")."1"));
  202. settype($foo2, ("1".("integer")));
  203. settype($foo3, ((("integer")."1")));
  204. settype((($foo)+1), "integer");
  205. ',
  206. ];
  207. yield 'nested is not an issue for this fixer, as these non may be fixed' => [
  208. '<?php
  209. settype($foo, settype($foo, "double"));
  210. settype(settype($foo, "double"), "double");
  211. ',
  212. ];
  213. yield 'unknown type II' => [
  214. '<?php settype($foo, "stringX");',
  215. ];
  216. yield 'boolean' => [
  217. '<?php $foo = (bool) $foo;',
  218. '<?php settype($foo, "boolean", );',
  219. ];
  220. yield 'comments with line breaks II' => [
  221. '<?php #0
  222. #1
  223. $foo = (int) $foo#2
  224. #3
  225. #4
  226. #5
  227. #6
  228. #7
  229. #8
  230. #9
  231. ;#10',
  232. '<?php #0
  233. #1
  234. settype#2
  235. #3
  236. (#4
  237. $foo#5
  238. ,#6
  239. "integer"#7
  240. ,#8
  241. )#9
  242. ;#10',
  243. ];
  244. }
  245. }