123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410 |
- <?php
- declare(strict_types=1);
- /*
- * This file is part of PHP CS Fixer.
- *
- * (c) Fabien Potencier <fabien@symfony.com>
- * Dariusz Rumiński <dariusz.ruminski@gmail.com>
- *
- * This source file is subject to the MIT license that is bundled
- * with this source code in the file LICENSE.
- */
- namespace PhpCsFixer\Tests\Fixer\Alias;
- use PhpCsFixer\ConfigurationException\InvalidFixerConfigurationException;
- use PhpCsFixer\Fixer\Alias\NoMixedEchoPrintFixer;
- use PhpCsFixer\Tests\Test\AbstractFixerTestCase;
- /**
- * @author Sullivan Senechal <soullivaneuh@gmail.com>
- *
- * @internal
- *
- * @covers \PhpCsFixer\Fixer\Alias\NoMixedEchoPrintFixer
- *
- * @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\Alias\NoMixedEchoPrintFixer>
- *
- * @phpstan-import-type _AutogeneratedInputConfiguration from \PhpCsFixer\Fixer\Alias\NoMixedEchoPrintFixer
- */
- final class NoMixedEchoPrintFixerTest extends AbstractFixerTestCase
- {
- /**
- * @param _AutogeneratedInputConfiguration $configuration
- *
- * @dataProvider provideFixCases
- */
- public function testFix(string $expected, ?string $input = null, array $configuration = []): void
- {
- $this->fixer->configure($configuration);
- $this->doTest($expected, $input);
- }
- /**
- * @return iterable<array{string, null|string, array{use: string}}>
- */
- public static function provideFixCases(): iterable
- {
- yield [
- '<?php
- print "test";
- ',
- null,
- ['use' => 'print'],
- ];
- yield [
- '<?php
- print ("test");
- ',
- null,
- ['use' => 'print'],
- ];
- yield [
- '<?php
- print("test");
- ',
- null,
- ['use' => 'print'],
- ];
- // `echo` can take multiple parameters (although such usage is rare) while `print` can take only one argument,
- // @see https://php.net/manual/en/function.echo.php and @see https://php.net/manual/en/function.print.php
- yield [
- '<?php
- echo "This ", "string ", "was ", "made ", "with multiple parameters.";
- ',
- null,
- ['use' => 'print'],
- ];
- yield [
- '<?php
- print "test";
- ',
- '<?php
- echo "test";
- ',
- ['use' => 'print'],
- ];
- yield [
- '<?php
- print ("test");
- ',
- '<?php
- echo ("test");
- ',
- ['use' => 'print'],
- ];
- yield [
- '<?php
- print("test");
- ',
- '<?php
- echo("test");
- ',
- ['use' => 'print'],
- ];
- yield [
- '<?php
- print foo(1, 2);
- ',
- '<?php
- echo foo(1, 2);
- ',
- ['use' => 'print'],
- ];
- yield [
- '<?php
- print ["foo", "bar", "baz"][$x];
- ',
- '<?php
- echo ["foo", "bar", "baz"][$x];
- ',
- ['use' => 'print'],
- ];
- yield [
- '<?php
- print $foo ? "foo" : "bar";
- ',
- '<?php
- echo $foo ? "foo" : "bar";
- ',
- ['use' => 'print'],
- ];
- yield [
- "<?php print 'foo' ?>...<?php echo 'bar', 'baz' ?>",
- "<?php echo 'foo' ?>...<?php echo 'bar', 'baz' ?>",
- ['use' => 'print'],
- ];
- yield [
- '<?php
- if ($foo) {
- print "foo";
- }
- print "bar";
- ',
- '<?php
- if ($foo) {
- echo "foo";
- }
- echo "bar";
- ',
- ['use' => 'print'],
- ];
- yield [
- '<?=$foo?>',
- null,
- ['use' => 'print'],
- ];
- foreach (self::getCodeSnippetsToConvertBothWays() as $codeSnippet) {
- yield [
- \sprintf($codeSnippet, 'print'),
- \sprintf($codeSnippet, 'echo'),
- ['use' => 'print'],
- ];
- }
- yield [
- '<?php
- echo "test";
- ',
- null,
- ['use' => 'echo'],
- ];
- yield [
- '<?php
- echo ("test");
- ',
- null,
- ['use' => 'echo'],
- ];
- yield [
- '<?php
- echo("test");
- ',
- null,
- ['use' => 'echo'],
- ];
- // https://github.com/PHP-CS-Fixer/PHP-CS-Fixer/issues/1502#issuecomment-156436229
- yield [
- '<?php
- ($some_var) ? print "true" : print "false";
- ',
- null,
- ['use' => 'echo'],
- ];
- // echo has no return value while print has a return value of 1 so it can be used in expressions.
- // https://www.w3schools.com/php/php_echo_print.asp
- yield [
- '<?php
- $ret = print "test";
- ',
- null,
- ['use' => 'echo'],
- ];
- yield [
- '<?php
- @print foo();
- ',
- null,
- ['use' => 'echo'],
- ];
- yield [
- '<?php
- function testFunction() {
- return print("test");
- }
- $a = testFunction();
- $b += print($a);
- $c=\'\';
- $c .= $b.print($a);
- $d = print($c) > 0 ? \'a\' : \'b\';
- switch(print(\'a\')) {}
- if (1 === print($a)) {}
- ',
- null,
- ['use' => 'echo'],
- ];
- yield [
- '<?php
- some_function_call();
- echo "test";
- ',
- '<?php
- some_function_call();
- print "test";
- ',
- ['use' => 'echo'],
- ];
- yield [
- '<?php
- echo "test";
- ',
- '<?php
- print "test";
- ',
- ['use' => 'echo'],
- ];
- yield [
- '<?php
- echo ("test");
- ',
- '<?php
- print ("test");
- ',
- ['use' => 'echo'],
- ];
- yield [
- '<?php
- echo("test");
- ',
- '<?php
- print("test");
- ',
- ['use' => 'echo'],
- ];
- yield [
- '<?php
- echo foo(1, 2);
- ',
- '<?php
- print foo(1, 2);
- ',
- ['use' => 'echo'],
- ];
- yield [
- '<?php
- echo $foo ? "foo" : "bar";
- ',
- '<?php
- print $foo ? "foo" : "bar";
- ',
- ['use' => 'echo'],
- ];
- yield [
- '<?php
- if ($foo) {
- echo "foo";
- }
- echo "bar";
- ',
- '<?php
- if ($foo) {
- print "foo";
- }
- print "bar";
- ',
- ['use' => 'echo'],
- ];
- foreach (self::getCodeSnippetsToConvertBothWays() as $codeSnippet) {
- yield [
- \sprintf($codeSnippet, 'echo'),
- \sprintf($codeSnippet, 'print'),
- ['use' => 'echo'],
- ];
- }
- }
- public function testConfigure(): void
- {
- $this->fixer->configure([]);
- self::assertCandidateTokenType(T_PRINT, $this->fixer);
- }
- /**
- * @param array<string, mixed> $wrongConfig
- *
- * @dataProvider provideInvalidConfigurationCases
- */
- public function testInvalidConfiguration(array $wrongConfig, string $expectedMessage): void
- {
- $this->expectException(InvalidFixerConfigurationException::class);
- $this->expectExceptionMessageMatches($expectedMessage);
- $this->fixer->configure($wrongConfig);
- }
- public static function provideInvalidConfigurationCases(): iterable
- {
- yield [
- ['a' => 'b'],
- '#^\[no_mixed_echo_print\] Invalid configuration: The option "a" does not exist\. (Known|Defined) options are: "use"\.$#',
- ];
- yield [
- ['a' => 'b', 'b' => 'c'],
- '#^\[no_mixed_echo_print\] Invalid configuration: The options "a", "b" do not exist\. (Known|Defined) options are: "use"\.$#',
- ];
- yield [
- [1],
- '#^\[no_mixed_echo_print\] Invalid configuration: The option "0" does not exist\. (Known|Defined) options are: "use"\.$#',
- ];
- yield [
- ['use' => '_invalid_'],
- '#^\[no_mixed_echo_print\] Invalid configuration: The option "use" with value "_invalid_" is invalid\. Accepted values are: "print", "echo"\.$#',
- ];
- }
- private static function assertCandidateTokenType(int $expected, NoMixedEchoPrintFixer $fixer): void
- {
- self::assertSame(
- $expected,
- \Closure::bind(static fn (NoMixedEchoPrintFixer $fixer): int => $fixer->candidateTokenType, null, NoMixedEchoPrintFixer::class)($fixer),
- );
- }
- /**
- * @return iterable<non-empty-string>
- */
- private static function getCodeSnippetsToConvertBothWays(): iterable
- {
- yield 'inside of HTML' => '<div><?php %1$s "foo" ?></div>';
- yield 'foreach without curly brackets' => '<?php
- %1$s "There will be foos: ";
- foreach ($foos as $foo)
- %1$s $foo;
- %1$s "End of foos";
- ';
- yield 'if and else without curly brackets' => '<?php
- if ($foo)
- %1$s "One";
- elseif ($bar)
- %1$s "Two";
- else
- %1$s "Three";
- ';
- }
- }
|