ExplicitStringVariableFixerTest.php 6.9 KB

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