Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/Rule/Rules/File/Psr1PhpTagsRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ private function replacementForInvalidTag(int $id, string $text, int $tokenLine,
return [
'offset' => $tagOffset,
'length' => 2,
'text' => '<?php',
'text' => preg_match('/^\s/', substr($text, $tagOffset + 2)) === 1 ? '<?php' : '<?php ',
];
}

Expand Down
39 changes: 39 additions & 0 deletions tests/Rule/File/Psr1PhpTagsRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -29,6 +31,7 @@
use function unlink;

use const DIRECTORY_SEPARATOR;
use const PHP_BINARY;

#[CoversClass(PhpFileFinder::class)]
#[CoversClass(Psr1PhpTagsRule::class)]
Expand All @@ -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', "<?echo 'test';?>");
$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("<?php echo 'test';?>", 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();
Expand Down
Loading