SetTypeToCastFixerTest.php 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  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\Alias;
  12. use PhpCsFixer\Tests\Test\AbstractFixerTestCase;
  13. /**
  14. * @author SpacePossum
  15. *
  16. * @internal
  17. *
  18. * @covers \PhpCsFixer\Fixer\Alias\SetTypeToCastFixer
  19. */
  20. final class SetTypeToCastFixerTest extends AbstractFixerTestCase
  21. {
  22. /**
  23. * @param string $expected
  24. * @param null|string $input
  25. *
  26. * @dataProvider provideFixCases
  27. */
  28. public function testFix($expected, $input = null)
  29. {
  30. $this->doTest($expected, $input);
  31. }
  32. public function provideFixCases()
  33. {
  34. return [
  35. 'null cast' => [
  36. '<?php $foo = null;',
  37. '<?php settype($foo, "null");',
  38. ],
  39. '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. 'array + spacing' => [
  68. '<?php $foo = (array) $foo;',
  69. '<?php settype ( $foo , \'array\');',
  70. ],
  71. 'bool + casing' => [
  72. '<?php $foo = (bool) $foo;',
  73. '<?php settype ( $foo , "Bool");',
  74. ],
  75. 'boolean' => [
  76. '<?php $foo = (bool) $foo;',
  77. '<?php settype ( $foo , "boolean");',
  78. ],
  79. 'double' => [
  80. '<?php $foo = (float) $foo;',
  81. '<?php settype($foo, "double");',
  82. ],
  83. 'float' => [
  84. '<?php $foo = (float) $foo;',
  85. '<?php settype($foo, "float");',
  86. ],
  87. 'float in loop' => [
  88. '<?php while(a()){$foo = (float) $foo;}',
  89. '<?php while(a()){settype($foo, "float");}',
  90. ],
  91. 'int full caps' => [
  92. '<?php $foo = (int) $foo;',
  93. '<?php settype($foo, "INT");',
  94. ],
  95. 'integer (simple)' => [
  96. '<?php $foo = (int) $foo;',
  97. '<?php settype($foo, "integer");',
  98. ],
  99. 'object' => [
  100. '<?php echo 1; $foo = (object) $foo;',
  101. '<?php echo 1; settype($foo, "object");',
  102. ],
  103. 'string' => [
  104. '<?php $foo = (string) $foo;',
  105. '<?php settype($foo, "string");',
  106. ],
  107. 'string in function body' => [
  108. '<?php function A(){ $foo = (string) $foo; return $foo; }',
  109. '<?php function A(){ settype($foo, "string"); return $foo; }',
  110. ],
  111. 'integer + no space' => [
  112. '<?php $foo = (int) $foo;',
  113. '<?php settype($foo,"integer");',
  114. ],
  115. '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. '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. 'first argument is not a variable' => [
  143. '<?php
  144. namespace A\B; // needed to keep the linter happy on PHP5.6
  145. function settype($a, $b){} // "
  146. settype(1, "double");
  147. ',
  148. ],
  149. 'first argument is variable followed by operation' => [
  150. '<?php
  151. namespace A\B; // needed to keep the linter happy on PHP5.6
  152. function settype($a, $b){} // "
  153. settype($foo + 1, "integer"); // function must be overridden, so do not fix it
  154. ',
  155. ],
  156. 'wrong numbers of arguments' => [
  157. '<?php settype($foo, "integer", $a);',
  158. ],
  159. 'other namespace I' => [
  160. '<?php a\b\settype($foo, "integer", $a);',
  161. ],
  162. 'other namespace II' => [
  163. '<?php \b\settype($foo, "integer", $a);',
  164. ],
  165. 'static call' => [
  166. '<?php A::settype($foo, "integer");',
  167. ],
  168. 'member call' => [
  169. '<?php $a->settype($foo, "integer");',
  170. ],
  171. 'unknown type' => [
  172. '<?php $a->settype($foo, "foo");',
  173. ],
  174. 'return value used I' => [
  175. '<?php $a = settype($foo, "integer");',
  176. ],
  177. 'return value used II' => [
  178. '<?php a(settype($foo, "integer"));',
  179. ],
  180. 'return value used III' => [
  181. '<?php $a = "123"; $b = [settype($a, "integer")];',
  182. ],
  183. 'return value used IV' => [
  184. '<?php $a = "123"; $b = [3 => settype($a, "integer")];',
  185. ],
  186. 'return value used V' => [
  187. '<?= settype($foo, "object");',
  188. ],
  189. '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. 'wrapped statements, not-fixable, even after removing the useless parenthesis brace' => [
  198. '<?php
  199. namespace A\B; // needed to keep the linter happy on PHP5.6
  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. '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. ];
  214. }
  215. /**
  216. * @param string $expected
  217. * @param string $input
  218. *
  219. * @requires PHP 7.3
  220. * @dataProvider provideFix73Cases
  221. */
  222. public function testFix73($expected, $input)
  223. {
  224. $this->doTest($expected, $input);
  225. }
  226. public function provideFix73Cases()
  227. {
  228. return [
  229. 'null cast' => [
  230. '<?php $foo = null;',
  231. '<?php settype($foo, "null");',
  232. ],
  233. 'boolean' => [
  234. '<?php $foo = (bool) $foo;',
  235. '<?php settype($foo, "boolean", );',
  236. ],
  237. 'comments with line breaks' => [
  238. '<?php #0
  239. #1
  240. $foo = (int) $foo#2
  241. #3
  242. #4
  243. #5
  244. #6
  245. #7
  246. #8
  247. #9
  248. ;#10',
  249. '<?php #0
  250. #1
  251. settype#2
  252. #3
  253. (#4
  254. $foo#5
  255. ,#6
  256. "integer"#7
  257. ,#8
  258. )#9
  259. ;#10',
  260. ],
  261. ];
  262. }
  263. }