Skip to content

Commit b3525ee

Browse files
authored
Merge pull request #533 from asgrim/improve-ext-path-exception
Improve the exception_dir exception message with more details
2 parents a91ae7d + 362d34c commit b3525ee

File tree

4 files changed

+97
-8
lines changed

4 files changed

+97
-8
lines changed

phpstan-baseline.neon

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -294,12 +294,6 @@ parameters:
294294
count: 1
295295
path: src/Platform/InstalledPiePackages.php
296296

297-
-
298-
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\.$#'
299-
identifier: function.alreadyNarrowedType
300-
count: 1
301-
path: src/Platform/TargetPhp/PhpBinaryPath.php
302-
303297
-
304298
message: '#^Call to function array_key_exists\(\) with 2 and array\{non\-falsy\-string, string, string\} will always evaluate to true\.$#'
305299
identifier: function.alreadyNarrowedType
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Php\Pie\Platform\TargetPhp\Exception;
6+
7+
use Php\Pie\Platform\TargetPhp\PhpBinaryPath;
8+
use RuntimeException;
9+
10+
use function file_exists;
11+
use function is_dir;
12+
13+
class ExtensionPathProblem extends RuntimeException
14+
{
15+
public static function new(PhpBinaryPath $php, string|null $extensionPath): self
16+
{
17+
$message = 'Could not determine extension path for ' . $php->phpBinaryPath;
18+
19+
if ($extensionPath === null) {
20+
$message .= '; extension_dir => not set';
21+
} else {
22+
$message .= '; extension_dir => ' . $extensionPath;
23+
24+
if (file_exists($extensionPath)) {
25+
$message .= '; exists';
26+
27+
if (is_dir($extensionPath)) {
28+
$message .= ', is a directory';
29+
} else {
30+
$message .= ', not a directory';
31+
}
32+
} else {
33+
$message .= '; does not exist';
34+
}
35+
}
36+
37+
return new self($message);
38+
}
39+
}

src/Platform/TargetPhp/PhpBinaryPath.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use Php\Pie\Platform\DebugBuild;
1313
use Php\Pie\Platform\OperatingSystem;
1414
use Php\Pie\Platform\OperatingSystemFamily;
15+
use Php\Pie\Platform\TargetPhp\Exception\ExtensionPathProblem;
1516
use Php\Pie\Util\Process;
1617
use RuntimeException;
1718
use Symfony\Component\Process\PhpExecutableFinder;
@@ -109,9 +110,10 @@ public function extensionPath(string|null $prefixInstallRoot = null): string
109110
{
110111
$phpinfo = $this->phpinfo();
111112

113+
$extensionPath = null;
114+
112115
if (
113116
preg_match('#^extension_dir\s+=>\s+([^=]+)\s+=>\s+([^=]+)$#m', $phpinfo, $matches)
114-
&& array_key_exists(1, $matches)
115117
&& trim($matches[1]) !== ''
116118
&& trim($matches[1]) !== 'no value'
117119
) {
@@ -142,7 +144,7 @@ public function extensionPath(string|null $prefixInstallRoot = null): string
142144
}
143145
}
144146

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

148150
public function assertExtensionIsLoadedInRuntime(ExtensionName $extension, IOInterface|null $io = null): void
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Php\PieUnitTest\Platform\TargetPhp\Exception;
6+
7+
use Php\Pie\Platform\TargetPhp\Exception\ExtensionPathProblem;
8+
use Php\Pie\Platform\TargetPhp\PhpBinaryPath;
9+
use PHPUnit\Framework\Attributes\CoversClass;
10+
use PHPUnit\Framework\TestCase;
11+
12+
#[CoversClass(ExtensionPathProblem::class)]
13+
final class ExtensionPathProblemTest extends TestCase
14+
{
15+
public function testExtensionPathNotSet(): void
16+
{
17+
$php = PhpBinaryPath::fromCurrentProcess();
18+
19+
self::assertSame(
20+
'Could not determine extension path for ' . $php->phpBinaryPath . '; extension_dir => not set',
21+
ExtensionPathProblem::new($php, null)->getMessage(),
22+
);
23+
}
24+
25+
public function testExtensionPathDoesNotExist(): void
26+
{
27+
$php = PhpBinaryPath::fromCurrentProcess();
28+
29+
self::assertSame(
30+
'Could not determine extension path for ' . $php->phpBinaryPath . '; extension_dir => /path/does/not/exist; does not exist',
31+
ExtensionPathProblem::new($php, '/path/does/not/exist')->getMessage(),
32+
);
33+
}
34+
35+
public function testExtensionPathIsAFile(): void
36+
{
37+
$php = PhpBinaryPath::fromCurrentProcess();
38+
39+
self::assertSame(
40+
'Could not determine extension path for ' . $php->phpBinaryPath . '; extension_dir => ' . __FILE__ . '; exists, not a directory',
41+
ExtensionPathProblem::new($php, __FILE__)->getMessage(),
42+
);
43+
}
44+
45+
public function testExtensionPathIsADir(): void
46+
{
47+
$php = PhpBinaryPath::fromCurrentProcess();
48+
49+
self::assertSame(
50+
'Could not determine extension path for ' . $php->phpBinaryPath . '; extension_dir => ' . __DIR__ . '; exists, is a directory',
51+
ExtensionPathProblem::new($php, __DIR__)->getMessage(),
52+
);
53+
}
54+
}

0 commit comments

Comments
 (0)