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
6 changes: 0 additions & 6 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -294,12 +294,6 @@ parameters:
count: 1
path: src/Platform/InstalledPiePackages.php

-
message: '#^Call to function array_key_exists\(\) with 1 and array\{non\-falsy\-string, non\-empty\-string, non\-empty\-string\} will always evaluate to true\.$#'
identifier: function.alreadyNarrowedType
count: 1
path: src/Platform/TargetPhp/PhpBinaryPath.php

-
message: '#^Call to function array_key_exists\(\) with 2 and array\{non\-falsy\-string, string, string\} will always evaluate to true\.$#'
identifier: function.alreadyNarrowedType
Expand Down
39 changes: 39 additions & 0 deletions src/Platform/TargetPhp/Exception/ExtensionPathProblem.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

declare(strict_types=1);

namespace Php\Pie\Platform\TargetPhp\Exception;

use Php\Pie\Platform\TargetPhp\PhpBinaryPath;
use RuntimeException;

use function file_exists;
use function is_dir;

class ExtensionPathProblem extends RuntimeException
{
public static function new(PhpBinaryPath $php, string|null $extensionPath): self
{
$message = 'Could not determine extension path for ' . $php->phpBinaryPath;

if ($extensionPath === null) {
$message .= '; extension_dir => not set';
} else {
$message .= '; extension_dir => ' . $extensionPath;

if (file_exists($extensionPath)) {
$message .= '; exists';

if (is_dir($extensionPath)) {
$message .= ', is a directory';
} else {
$message .= ', not a directory';
}
} else {
$message .= '; does not exist';
}
}

return new self($message);
}
}
6 changes: 4 additions & 2 deletions src/Platform/TargetPhp/PhpBinaryPath.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use Php\Pie\Platform\DebugBuild;
use Php\Pie\Platform\OperatingSystem;
use Php\Pie\Platform\OperatingSystemFamily;
use Php\Pie\Platform\TargetPhp\Exception\ExtensionPathProblem;
use Php\Pie\Util\Process;
use RuntimeException;
use Symfony\Component\Process\PhpExecutableFinder;
Expand Down Expand Up @@ -109,9 +110,10 @@ public function extensionPath(string|null $prefixInstallRoot = null): string
{
$phpinfo = $this->phpinfo();

$extensionPath = null;

if (
preg_match('#^extension_dir\s+=>\s+([^=]+)\s+=>\s+([^=]+)$#m', $phpinfo, $matches)
&& array_key_exists(1, $matches)
&& trim($matches[1]) !== ''
&& trim($matches[1]) !== 'no value'
) {
Expand Down Expand Up @@ -142,7 +144,7 @@ public function extensionPath(string|null $prefixInstallRoot = null): string
}
}

throw new RuntimeException('Could not determine extension path for ' . $this->phpBinaryPath);
throw ExtensionPathProblem::new($this, $extensionPath);
}

public function assertExtensionIsLoadedInRuntime(ExtensionName $extension, IOInterface|null $io = null): void
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php

declare(strict_types=1);

namespace Php\PieUnitTest\Platform\TargetPhp\Exception;

use Php\Pie\Platform\TargetPhp\Exception\ExtensionPathProblem;
use Php\Pie\Platform\TargetPhp\PhpBinaryPath;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\TestCase;

#[CoversClass(ExtensionPathProblem::class)]
final class ExtensionPathProblemTest extends TestCase
{
public function testExtensionPathNotSet(): void
{
$php = PhpBinaryPath::fromCurrentProcess();

self::assertSame(
'Could not determine extension path for ' . $php->phpBinaryPath . '; extension_dir => not set',
ExtensionPathProblem::new($php, null)->getMessage(),
);
}

public function testExtensionPathDoesNotExist(): void
{
$php = PhpBinaryPath::fromCurrentProcess();

self::assertSame(
'Could not determine extension path for ' . $php->phpBinaryPath . '; extension_dir => /path/does/not/exist; does not exist',
ExtensionPathProblem::new($php, '/path/does/not/exist')->getMessage(),
);
}

public function testExtensionPathIsAFile(): void
{
$php = PhpBinaryPath::fromCurrentProcess();

self::assertSame(
'Could not determine extension path for ' . $php->phpBinaryPath . '; extension_dir => ' . __FILE__ . '; exists, not a directory',
ExtensionPathProblem::new($php, __FILE__)->getMessage(),
);
}

public function testExtensionPathIsADir(): void
{
$php = PhpBinaryPath::fromCurrentProcess();

self::assertSame(
'Could not determine extension path for ' . $php->phpBinaryPath . '; extension_dir => ' . __DIR__ . '; exists, is a directory',
ExtensionPathProblem::new($php, __DIR__)->getMessage(),
);
}
}