SetTypeToCastFixerTest.php 6.4 KB

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