NoMixedEchoPrintFixerTest.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410
  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\Alias;
  13. use PhpCsFixer\AbstractFixer;
  14. use PhpCsFixer\ConfigurationException\InvalidFixerConfigurationException;
  15. use PhpCsFixer\Tests\Test\AbstractFixerTestCase;
  16. /**
  17. * @author Sullivan Senechal <soullivaneuh@gmail.com>
  18. *
  19. * @internal
  20. *
  21. * @covers \PhpCsFixer\Fixer\Alias\NoMixedEchoPrintFixer
  22. *
  23. * @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\Alias\NoMixedEchoPrintFixer>
  24. *
  25. * @phpstan-import-type _AutogeneratedInputConfiguration from \PhpCsFixer\Fixer\Alias\NoMixedEchoPrintFixer
  26. */
  27. final class NoMixedEchoPrintFixerTest extends AbstractFixerTestCase
  28. {
  29. /**
  30. * @param _AutogeneratedInputConfiguration $configuration
  31. *
  32. * @dataProvider provideFixCases
  33. */
  34. public function testFix(string $expected, ?string $input = null, array $configuration = []): void
  35. {
  36. $this->fixer->configure($configuration);
  37. $this->doTest($expected, $input);
  38. }
  39. /**
  40. * @return iterable<array{string, null|string, array{use: string}}>
  41. */
  42. public static function provideFixCases(): iterable
  43. {
  44. yield [
  45. '<?php
  46. print "test";
  47. ',
  48. null,
  49. ['use' => 'print'],
  50. ];
  51. yield [
  52. '<?php
  53. print ("test");
  54. ',
  55. null,
  56. ['use' => 'print'],
  57. ];
  58. yield [
  59. '<?php
  60. print("test");
  61. ',
  62. null,
  63. ['use' => 'print'],
  64. ];
  65. // `echo` can take multiple parameters (although such usage is rare) while `print` can take only one argument,
  66. // @see https://php.net/manual/en/function.echo.php and @see https://php.net/manual/en/function.print.php
  67. yield [
  68. '<?php
  69. echo "This ", "string ", "was ", "made ", "with multiple parameters.";
  70. ',
  71. null,
  72. ['use' => 'print'],
  73. ];
  74. yield [
  75. '<?php
  76. print "test";
  77. ',
  78. '<?php
  79. echo "test";
  80. ',
  81. ['use' => 'print'],
  82. ];
  83. yield [
  84. '<?php
  85. print ("test");
  86. ',
  87. '<?php
  88. echo ("test");
  89. ',
  90. ['use' => 'print'],
  91. ];
  92. yield [
  93. '<?php
  94. print("test");
  95. ',
  96. '<?php
  97. echo("test");
  98. ',
  99. ['use' => 'print'],
  100. ];
  101. yield [
  102. '<?php
  103. print foo(1, 2);
  104. ',
  105. '<?php
  106. echo foo(1, 2);
  107. ',
  108. ['use' => 'print'],
  109. ];
  110. yield [
  111. '<?php
  112. print ["foo", "bar", "baz"][$x];
  113. ',
  114. '<?php
  115. echo ["foo", "bar", "baz"][$x];
  116. ',
  117. ['use' => 'print'],
  118. ];
  119. yield [
  120. '<?php
  121. print $foo ? "foo" : "bar";
  122. ',
  123. '<?php
  124. echo $foo ? "foo" : "bar";
  125. ',
  126. ['use' => 'print'],
  127. ];
  128. yield [
  129. "<?php print 'foo' ?>...<?php echo 'bar', 'baz' ?>",
  130. "<?php echo 'foo' ?>...<?php echo 'bar', 'baz' ?>",
  131. ['use' => 'print'],
  132. ];
  133. yield [
  134. '<?php
  135. if ($foo) {
  136. print "foo";
  137. }
  138. print "bar";
  139. ',
  140. '<?php
  141. if ($foo) {
  142. echo "foo";
  143. }
  144. echo "bar";
  145. ',
  146. ['use' => 'print'],
  147. ];
  148. yield [
  149. '<?=$foo?>',
  150. null,
  151. ['use' => 'print'],
  152. ];
  153. foreach (self::getCodeSnippetsToConvertBothWays() as $codeSnippet) {
  154. yield [
  155. \sprintf($codeSnippet, 'print'),
  156. \sprintf($codeSnippet, 'echo'),
  157. ['use' => 'print'],
  158. ];
  159. }
  160. yield [
  161. '<?php
  162. echo "test";
  163. ',
  164. null,
  165. ['use' => 'echo'],
  166. ];
  167. yield [
  168. '<?php
  169. echo ("test");
  170. ',
  171. null,
  172. ['use' => 'echo'],
  173. ];
  174. yield [
  175. '<?php
  176. echo("test");
  177. ',
  178. null,
  179. ['use' => 'echo'],
  180. ];
  181. // https://github.com/PHP-CS-Fixer/PHP-CS-Fixer/issues/1502#issuecomment-156436229
  182. yield [
  183. '<?php
  184. ($some_var) ? print "true" : print "false";
  185. ',
  186. null,
  187. ['use' => 'echo'],
  188. ];
  189. // echo has no return value while print has a return value of 1 so it can be used in expressions.
  190. // https://www.w3schools.com/php/php_echo_print.asp
  191. yield [
  192. '<?php
  193. $ret = print "test";
  194. ',
  195. null,
  196. ['use' => 'echo'],
  197. ];
  198. yield [
  199. '<?php
  200. @print foo();
  201. ',
  202. null,
  203. ['use' => 'echo'],
  204. ];
  205. yield [
  206. '<?php
  207. function testFunction() {
  208. return print("test");
  209. }
  210. $a = testFunction();
  211. $b += print($a);
  212. $c=\'\';
  213. $c .= $b.print($a);
  214. $d = print($c) > 0 ? \'a\' : \'b\';
  215. switch(print(\'a\')) {}
  216. if (1 === print($a)) {}
  217. ',
  218. null,
  219. ['use' => 'echo'],
  220. ];
  221. yield [
  222. '<?php
  223. some_function_call();
  224. echo "test";
  225. ',
  226. '<?php
  227. some_function_call();
  228. print "test";
  229. ',
  230. ['use' => 'echo'],
  231. ];
  232. yield [
  233. '<?php
  234. echo "test";
  235. ',
  236. '<?php
  237. print "test";
  238. ',
  239. ['use' => 'echo'],
  240. ];
  241. yield [
  242. '<?php
  243. echo ("test");
  244. ',
  245. '<?php
  246. print ("test");
  247. ',
  248. ['use' => 'echo'],
  249. ];
  250. yield [
  251. '<?php
  252. echo("test");
  253. ',
  254. '<?php
  255. print("test");
  256. ',
  257. ['use' => 'echo'],
  258. ];
  259. yield [
  260. '<?php
  261. echo foo(1, 2);
  262. ',
  263. '<?php
  264. print foo(1, 2);
  265. ',
  266. ['use' => 'echo'],
  267. ];
  268. yield [
  269. '<?php
  270. echo $foo ? "foo" : "bar";
  271. ',
  272. '<?php
  273. print $foo ? "foo" : "bar";
  274. ',
  275. ['use' => 'echo'],
  276. ];
  277. yield [
  278. '<?php
  279. if ($foo) {
  280. echo "foo";
  281. }
  282. echo "bar";
  283. ',
  284. '<?php
  285. if ($foo) {
  286. print "foo";
  287. }
  288. print "bar";
  289. ',
  290. ['use' => 'echo'],
  291. ];
  292. foreach (self::getCodeSnippetsToConvertBothWays() as $codeSnippet) {
  293. yield [
  294. \sprintf($codeSnippet, 'echo'),
  295. \sprintf($codeSnippet, 'print'),
  296. ['use' => 'echo'],
  297. ];
  298. }
  299. }
  300. public function testConfigure(): void
  301. {
  302. $this->fixer->configure([]);
  303. self::assertCandidateTokenType(T_PRINT, $this->fixer);
  304. }
  305. /**
  306. * @param array<string, mixed> $wrongConfig
  307. *
  308. * @dataProvider provideInvalidConfigurationCases
  309. */
  310. public function testInvalidConfiguration(array $wrongConfig, string $expectedMessage): void
  311. {
  312. $this->expectException(InvalidFixerConfigurationException::class);
  313. $this->expectExceptionMessageMatches($expectedMessage);
  314. $this->fixer->configure($wrongConfig);
  315. }
  316. public static function provideInvalidConfigurationCases(): iterable
  317. {
  318. yield [
  319. ['a' => 'b'],
  320. '#^\[no_mixed_echo_print\] Invalid configuration: The option "a" does not exist\. (Known|Defined) options are: "use"\.$#',
  321. ];
  322. yield [
  323. ['a' => 'b', 'b' => 'c'],
  324. '#^\[no_mixed_echo_print\] Invalid configuration: The options "a", "b" do not exist\. (Known|Defined) options are: "use"\.$#',
  325. ];
  326. yield [
  327. [1],
  328. '#^\[no_mixed_echo_print\] Invalid configuration: The option "0" does not exist\. (Known|Defined) options are: "use"\.$#',
  329. ];
  330. yield [
  331. ['use' => '_invalid_'],
  332. '#^\[no_mixed_echo_print\] Invalid configuration: The option "use" with value "_invalid_" is invalid\. Accepted values are: "print", "echo"\.$#',
  333. ];
  334. }
  335. private static function assertCandidateTokenType(int $expected, AbstractFixer $fixer): void
  336. {
  337. $reflectionProperty = new \ReflectionProperty($fixer, 'candidateTokenType');
  338. $reflectionProperty->setAccessible(true);
  339. self::assertSame($expected, $reflectionProperty->getValue($fixer));
  340. }
  341. /**
  342. * @return iterable<non-empty-string>
  343. */
  344. private static function getCodeSnippetsToConvertBothWays(): iterable
  345. {
  346. yield 'inside of HTML' => '<div><?php %1$s "foo" ?></div>';
  347. yield 'foreach without curly brackets' => '<?php
  348. %1$s "There will be foos: ";
  349. foreach ($foos as $foo)
  350. %1$s $foo;
  351. %1$s "End of foos";
  352. ';
  353. yield 'if and else without curly brackets' => '<?php
  354. if ($foo)
  355. %1$s "One";
  356. elseif ($bar)
  357. %1$s "Two";
  358. else
  359. %1$s "Three";
  360. ';
  361. }
  362. }