From 7489e29c58fd4f4b6c8391378b7c9555fa606c25 Mon Sep 17 00:00:00 2001 From: Thomas Calvet Date: Tue, 8 Mar 2022 18:16:11 +0100 Subject: [PATCH] [Process] Don't return executable directories in PhpExecutableFinder --- PhpExecutableFinder.php | 10 +++++++--- Tests/PhpExecutableFinderTest.php | 30 +++++++++++++++++++++++++----- 2 files changed, 32 insertions(+), 8 deletions(-) diff --git a/PhpExecutableFinder.php b/PhpExecutableFinder.php index 3d5eabd9..92e0262a 100644 --- a/PhpExecutableFinder.php +++ b/PhpExecutableFinder.php @@ -47,6 +47,10 @@ public function find($includeArgs = true) } } + if (@is_dir($php)) { + return false; + } + return $php; } @@ -59,7 +63,7 @@ public function find($includeArgs = true) } if ($php = getenv('PHP_PATH')) { - if (!@is_executable($php)) { + if (!@is_executable($php) || @is_dir($php)) { return false; } @@ -67,12 +71,12 @@ public function find($includeArgs = true) } if ($php = getenv('PHP_PEAR_PHP_BIN')) { - if (@is_executable($php)) { + if (@is_executable($php) && !@is_dir($php)) { return $php; } } - if (@is_executable($php = \PHP_BINDIR.('\\' === \DIRECTORY_SEPARATOR ? '\\php.exe' : '/php'))) { + if (@is_executable($php = \PHP_BINDIR.('\\' === \DIRECTORY_SEPARATOR ? '\\php.exe' : '/php')) && !@is_dir($php)) { return $php; } diff --git a/Tests/PhpExecutableFinderTest.php b/Tests/PhpExecutableFinderTest.php index cf3ffb55..0ef4cf09 100644 --- a/Tests/PhpExecutableFinderTest.php +++ b/Tests/PhpExecutableFinderTest.php @@ -50,12 +50,32 @@ public function testFindArguments() public function testNotExitsBinaryFile() { $f = new PhpExecutableFinder(); - $phpBinaryEnv = \PHP_BINARY; - putenv('PHP_BINARY=/usr/local/php/bin/php-invalid'); - $this->assertFalse($f->find(), '::find() returns false because of not exist file'); - $this->assertFalse($f->find(false), '::find(false) returns false because of not exist file'); + $originalPhpBinary = getenv('PHP_BINARY'); - putenv('PHP_BINARY='.$phpBinaryEnv); + try { + putenv('PHP_BINARY=/usr/local/php/bin/php-invalid'); + + $this->assertFalse($f->find(), '::find() returns false because of not exist file'); + $this->assertFalse($f->find(false), '::find(false) returns false because of not exist file'); + } finally { + putenv('PHP_BINARY='.$originalPhpBinary); + } + } + + public function testFindWithExecutableDirectory() + { + $originalPhpBinary = getenv('PHP_BINARY'); + + try { + $executableDirectoryPath = sys_get_temp_dir().'/PhpExecutableFinderTest_testFindWithExecutableDirectory'; + @mkdir($executableDirectoryPath); + $this->assertTrue(is_executable($executableDirectoryPath)); + putenv('PHP_BINARY='.$executableDirectoryPath); + + $this->assertFalse((new PhpExecutableFinder())->find()); + } finally { + putenv('PHP_BINARY='.$originalPhpBinary); + } } }