diff --git a/src/Scoper/PhpScoper.php b/src/Scoper/PhpScoper.php index cab5f9df..66afa1a8 100644 --- a/src/Scoper/PhpScoper.php +++ b/src/Scoper/PhpScoper.php @@ -23,6 +23,7 @@ final class PhpScoper implements Scoper { private const FILE_PATH_PATTERN = '/.*\.php$/'; private const NOT_FILE_BINARY = '/\..+?$/'; + private const PHP_TAG = '/^<\?php/'; private const PHP_BINARY = '/^#!.+?php.*\n{1,}<\?php/'; private $parser; @@ -70,6 +71,10 @@ private function isPhpFile(string $filePath, string $contents): bool return false; } + if (1 === preg_match(self::PHP_TAG, ltrim($contents))) { + return true; + } + return 1 === preg_match(self::PHP_BINARY, $contents); } } diff --git a/src/functions.php b/src/functions.php index ffcc2d4c..a205a85f 100644 --- a/src/functions.php +++ b/src/functions.php @@ -37,6 +37,8 @@ use Symfony\Component\Console\Application as SymfonyApplication; use Symfony\Component\Filesystem\Filesystem; +// TODO: register this file to the list of functions if possible to be autoloaded + /** * @private */ @@ -87,14 +89,14 @@ function get_version(): string function create_scoper(): Scoper { return new PatchScoper( - new JsonFileScoper( - new InstalledPackagesScoper( - new PhpScoper( - create_parser(), - new NullScoper(), - new TraverserFactory() + new PhpScoper( + create_parser(), + new JsonFileScoper( + new InstalledPackagesScoper( + new NullScoper() ) - ) + ), + new TraverserFactory() ) ); } diff --git a/tests/Scoper/PhpScoperTest.php b/tests/Scoper/PhpScoperTest.php index 1fd0f5f1..a983715e 100644 --- a/tests/Scoper/PhpScoperTest.php +++ b/tests/Scoper/PhpScoperTest.php @@ -177,6 +177,33 @@ public function test_does_not_scope_file_if_is_not_a_PHP_file() $this->decoratedScoperProphecy->scope(Argument::cetera())->shouldHaveBeenCalledTimes(1); } + public function test_can_scope_a_PHP_file_with_the_wrong_extension() + { + $prefix = 'Humbug'; + $filePath = escape_path($this->tmp.'/file'); + $patchers = [create_fake_patcher()]; + $whitelist = ['Foo']; + $whitelister = create_fake_whitelister(); + + $contents = <<<'PHP' +scoper->scope($filePath, $contents, $prefix, $patchers, $whitelist, $whitelister); + + $this->assertSame($expected, $actual); + } + public function test_can_scope_PHP_binary_files() { $prefix = 'Humbug';