ExplicitStringVariableFixerTest.php 6.7 KB

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