ExplicitStringVariableFixerTest.php 7.1 KB

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