From 43ab6955580b2948cd5b54913b7a276d0316f9e5 Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Sat, 11 Jul 2026 15:41:17 +0700 Subject: [PATCH 1/2] add test for violate immediatelly after short open tag --- src/Rule/Rules/File/Psr1PhpTagsRule.php | 2 +- test.php | 1 + tests/Rule/File/Psr1PhpTagsRuleTest.php | 39 +++++++++++++++++++++++++ 3 files changed, 41 insertions(+), 1 deletion(-) create mode 100644 test.php diff --git a/src/Rule/Rules/File/Psr1PhpTagsRule.php b/src/Rule/Rules/File/Psr1PhpTagsRule.php index be1919e..8b83bea 100644 --- a/src/Rule/Rules/File/Psr1PhpTagsRule.php +++ b/src/Rule/Rules/File/Psr1PhpTagsRule.php @@ -157,7 +157,7 @@ private function replacementForInvalidTag(int $id, string $text, int $tokenLine, return [ 'offset' => $tagOffset, 'length' => 2, - 'text' => ' preg_match('/^\s/', substr($text, $tagOffset + 2)) === 1 ? ' diff --git a/tests/Rule/File/Psr1PhpTagsRuleTest.php b/tests/Rule/File/Psr1PhpTagsRuleTest.php index 611729d..9d619e1 100644 --- a/tests/Rule/File/Psr1PhpTagsRuleTest.php +++ b/tests/Rule/File/Psr1PhpTagsRuleTest.php @@ -16,6 +16,8 @@ use PHPUnit\Framework\TestCase; use function bin2hex; +use function escapeshellarg; +use function exec; use function file_get_contents; use function file_put_contents; use function mkdir; @@ -29,6 +31,7 @@ use function unlink; use const DIRECTORY_SEPARATOR; +use const PHP_BINARY; #[CoversClass(PhpFileFinder::class)] #[CoversClass(Psr1PhpTagsRule::class)] @@ -53,6 +56,42 @@ public function testViolatesShortOpenTag(): void } } + public function testViolatesEchoImmediatelyAfterShortOpenTag(): void + { + $basePath = $this->makeTempDir(); + + try { + mkdir($basePath . '/src'); + file_put_contents($basePath . '/src/Foo.php', ""); + $command = escapeshellarg(PHP_BINARY) + . ' -d short_open_tag=1 ' + . escapeshellarg($basePath . '/src/Foo.php'); + + exec($command, $output, $exitCode); + + $this->assertSame(0, $exitCode); + $this->assertSame(['test'], $output); + + $psr1PhpTagsRule = new Psr1PhpTagsRule(['src/']); + $violations = $psr1PhpTagsRule->evaluateProjectAll($basePath, Architecture::define()); + + $this->assertCount(1, $violations); + $this->assertSame(1, $violations[0]->line); + $this->assertTrue($psr1PhpTagsRule->fix($violations[0])); + $this->assertSame("", file_get_contents($basePath . '/src/Foo.php')); + + $output = []; + exec($command, $output, $exitCode); + + $this->assertSame(0, $exitCode); + $this->assertSame(['test'], $output); + } finally { + unlink($basePath . '/src/Foo.php'); + rmdir($basePath . '/src'); + rmdir($basePath); + } + } + public function testViolatesAssignmentUsingShortOpenTag(): void { $basePath = $this->makeTempDir(); From dccbad18c3e19e9ed56cdb27f314c1f432aa10ec Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Sat, 11 Jul 2026 15:42:04 +0700 Subject: [PATCH 2/2] add test for violate immediatelly after short open tag --- test.php | 1 - 1 file changed, 1 deletion(-) delete mode 100644 test.php diff --git a/test.php b/test.php deleted file mode 100644 index d4b667b..0000000 --- a/test.php +++ /dev/null @@ -1 +0,0 @@ -