ExplicitStringVariableFixerTest.php 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  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\StringNotation;
  12. use PhpCsFixer\Tests\Test\AbstractFixerTestCase;
  13. /**
  14. * @author Filippo Tessarotto <zoeslam@gmail.com>
  15. *
  16. * @internal
  17. *
  18. * @covers \PhpCsFixer\Fixer\StringNotation\ExplicitStringVariableFixer
  19. */
  20. final class ExplicitStringVariableFixerTest extends AbstractFixerTestCase
  21. {
  22. /**
  23. * @param string $expected
  24. * @param null|string $input
  25. *
  26. * @dataProvider provideTestFixCases
  27. */
  28. public function testFix($expected, $input = null)
  29. {
  30. $this->doTest($expected, $input);
  31. }
  32. public function provideTestFixCases()
  33. {
  34. $input = $expected = '<?php';
  35. for ($inc = 1; $inc < 15; ++$inc) {
  36. $input .= " \$var{$inc} = \"My name is \$name!\";";
  37. $expected .= " \$var{$inc} = \"My name is \${name}!\";";
  38. }
  39. return [
  40. [
  41. $expected,
  42. $input,
  43. ],
  44. [
  45. '<?php $a = "My name is ${name}!";',
  46. '<?php $a = "My name is $name!";',
  47. ],
  48. [
  49. '<?php "My name is ${james}${bond}!";',
  50. '<?php "My name is $james$bond!";',
  51. ],
  52. [
  53. '<?php $a = <<<EOF
  54. My name is ${name}!
  55. EOF;
  56. ',
  57. '<?php $a = <<<EOF
  58. My name is $name!
  59. EOF;
  60. ',
  61. ],
  62. [
  63. '<?php $a = "${b}";',
  64. '<?php $a = "$b";',
  65. ],
  66. [
  67. '<?php $a = "${b} start";',
  68. '<?php $a = "$b start";',
  69. ],
  70. [
  71. '<?php $a = "end ${b}";',
  72. '<?php $a = "end $b";',
  73. ],
  74. [
  75. '<?php $a = <<<EOF
  76. ${b}
  77. EOF;
  78. ',
  79. '<?php $a = <<<EOF
  80. $b
  81. EOF;
  82. ',
  83. ],
  84. ['<?php $a = \'My name is $name!\';'],
  85. ['<?php $a = "My name is " . $name;'],
  86. ['<?php $a = "My name is {$name}!";'],
  87. [
  88. '<?php $a = <<<EOF
  89. My name is {$name}!
  90. EOF;
  91. ',
  92. ],
  93. ['<?php $a = "My name is {$user->name}";'],
  94. [
  95. '<?php $a = <<<EOF
  96. My name is {$user->name}
  97. EOF;
  98. ',
  99. ],
  100. [
  101. '<?php $a = <<<\'EOF\'
  102. $b
  103. EOF;
  104. ',
  105. ],
  106. [
  107. '<?php $a = "My name is {$object->property} !";',
  108. '<?php $a = "My name is $object->property !";',
  109. ],
  110. [
  111. '<?php $a = "My name is {$array[1]} !";',
  112. '<?php $a = "My name is $array[1] !";',
  113. ],
  114. [
  115. '<?php $a = "My name is {$array[\'foo\']} !";',
  116. '<?php $a = "My name is $array[foo] !";',
  117. ],
  118. [
  119. '<?php $a = "My name is {$array[$foo]} !";',
  120. '<?php $a = "My name is $array[$foo] !";',
  121. ],
  122. [
  123. '<?php $a = "My name is {$array[$foo]}[${bar}] !";',
  124. '<?php $a = "My name is $array[$foo][$bar] !";',
  125. ],
  126. [
  127. '<?php $a = "Closure not allowed ${closure}() text";',
  128. '<?php $a = "Closure not allowed $closure() text";',
  129. ],
  130. [
  131. '<?php $a = "Complex object chaining not allowed {$object->property}->method()->array[1] text";',
  132. '<?php $a = "Complex object chaining not allowed $object->property->method()->array[1] text";',
  133. ],
  134. [
  135. '<?php $a = "Complex array chaining not allowed {$array[1]}[2][MY_CONSTANT] text";',
  136. '<?php $a = "Complex array chaining not allowed $array[1][2][MY_CONSTANT] text";',
  137. ],
  138. [
  139. '<?php $a = "Concatenation: ${james}${bond}{$object->property}{$array[1]}!";',
  140. '<?php $a = "Concatenation: $james$bond$object->property$array[1]!";',
  141. ],
  142. [
  143. '<?php $a = "{$a->b} start";',
  144. '<?php $a = "$a->b start";',
  145. ],
  146. [
  147. '<?php $a = "end {$a->b}";',
  148. '<?php $a = "end $a->b";',
  149. ],
  150. [
  151. '<?php $a = "{$a[1]} start";',
  152. '<?php $a = "$a[1] start";',
  153. ],
  154. [
  155. '<?php $a = "end {$a[1]}";',
  156. '<?php $a = "end $a[1]";',
  157. ],
  158. [
  159. '<?php $a = b"{$a->b} start";',
  160. '<?php $a = b"$a->b start";',
  161. ],
  162. [
  163. '<?php $a = b"end {$a->b}";',
  164. '<?php $a = b"end $a->b";',
  165. ],
  166. [
  167. '<?php $a = b"{$a[1]} start";',
  168. '<?php $a = b"$a[1] start";',
  169. ],
  170. [
  171. '<?php $a = b"end {$a[1]}";',
  172. '<?php $a = b"end $a[1]";',
  173. ],
  174. [
  175. '<?php $a = B"{$a->b} start";',
  176. '<?php $a = B"$a->b start";',
  177. ],
  178. [
  179. '<?php $a = B"end {$a->b}";',
  180. '<?php $a = B"end $a->b";',
  181. ],
  182. [
  183. '<?php $a = B"{$a[1]} start";',
  184. '<?php $a = B"$a[1] start";',
  185. ],
  186. [
  187. '<?php $a = B"end {$a[1]}";',
  188. '<?php $a = B"end $a[1]";',
  189. ],
  190. [
  191. '<?php $a = "*{$a[0]}{$b[1]}X{$c[2]}{$d[3]}";',
  192. '<?php $a = "*$a[0]$b[1]X$c[2]$d[3]";',
  193. ],
  194. [
  195. '<?php $a = `echo $foo`;',
  196. ],
  197. [
  198. '<?php $a = "My name is ${name}!"; $a = `echo $foo`; $a = "{$a->b} start";',
  199. '<?php $a = "My name is $name!"; $a = `echo $foo`; $a = "$a->b start";',
  200. ],
  201. [
  202. '<?php $mobileNumberVisible = "***-***-{$last4Digits[0]}{$last4Digits[1]}-{$last4Digits[2]}{$last4Digits[3]}";',
  203. '<?php $mobileNumberVisible = "***-***-$last4Digits[0]$last4Digits[1]-$last4Digits[2]$last4Digits[3]";',
  204. ],
  205. [
  206. '<?php $pair = "${foo} {$bar[0]}";',
  207. '<?php $pair = "$foo {$bar[0]}";',
  208. ],
  209. [
  210. '<?php $pair = "${foo}{$bar[0]}";',
  211. '<?php $pair = "$foo{$bar[0]}";',
  212. ],
  213. ];
  214. }
  215. /**
  216. * @param string $expected
  217. * @param null|string $input
  218. *
  219. * @dataProvider provideTestFix71Cases
  220. * @requires PHP 7.1
  221. */
  222. public function testFix71($expected, $input = null)
  223. {
  224. $this->doTest($expected, $input);
  225. }
  226. public function provideTestFix71Cases()
  227. {
  228. return [
  229. [
  230. '<?php $a = "My name is {$array[-1]} !";',
  231. '<?php $a = "My name is $array[-1] !";',
  232. ],
  233. [
  234. '<?php $a = "{$a[-1]} start";',
  235. '<?php $a = "$a[-1] start";',
  236. ],
  237. [
  238. '<?php $a = "end {$a[-1]}";',
  239. '<?php $a = "end $a[-1]";',
  240. ],
  241. [
  242. '<?php $a = b"end {$a[-1]}";',
  243. '<?php $a = b"end $a[-1]";',
  244. ],
  245. [
  246. '<?php $a = B"end {$a[-1]}";',
  247. '<?php $a = B"end $a[-1]";',
  248. ],
  249. ];
  250. }
  251. }