Skip to content

Commit 44c91bd

Browse files
committed
refactor(container): replace anonymous exception classes with proper Exception types
Add dedicated ContainerException and NotFoundException in Container\Exception namespace, replacing inline anonymous classes.
1 parent 83f12c9 commit 44c91bd

3 files changed

Lines changed: 30 additions & 7 deletions

File tree

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace WebProject\PhpOpenApiMockServer\Container\Exception;
6+
7+
use Psr\Container\ContainerExceptionInterface;
8+
use RuntimeException;
9+
10+
final class ContainerException extends RuntimeException implements ContainerExceptionInterface
11+
{
12+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace WebProject\PhpOpenApiMockServer\Container\Exception;
6+
7+
use Psr\Container\NotFoundExceptionInterface;
8+
use RuntimeException;
9+
10+
final class NotFoundException extends RuntimeException implements NotFoundExceptionInterface
11+
{
12+
}

src/Container/SimpleContainer.php

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,9 @@
99
use function is_callable;
1010
use function is_int;
1111
use function is_string;
12-
use Psr\Container\ContainerExceptionInterface;
1312
use Psr\Container\ContainerInterface;
14-
use Psr\Container\NotFoundExceptionInterface;
15-
use RuntimeException;
13+
use WebProject\PhpOpenApiMockServer\Container\Exception\ContainerException;
14+
use WebProject\PhpOpenApiMockServer\Container\Exception\NotFoundException;
1615

1716
/**
1817
* Lightweight PSR-11 container that supports the Mezzio ConfigProvider format
@@ -41,7 +40,7 @@ public function get(string $id): mixed
4140
}
4241

4342
if (!isset($this->factories[$resolved])) {
44-
throw new class("Service '{$id}' not found in container.") extends RuntimeException implements NotFoundExceptionInterface {};
43+
throw new NotFoundException("Service '{$id}' not found in container.");
4544
}
4645

4746
$factory = $this->factories[$resolved];
@@ -51,7 +50,7 @@ public function get(string $id): mixed
5150
}
5251

5352
if (!is_callable($factory)) {
54-
throw new class("Factory for '{$resolved}' is not callable.") extends RuntimeException implements ContainerExceptionInterface {};
53+
throw new ContainerException("Factory for '{$resolved}' is not callable.");
5554
}
5655

5756
$instance = $factory($this, $resolved);
@@ -65,7 +64,7 @@ public function get(string $id): mixed
6564
}
6665

6766
if (!is_callable($delegator)) {
68-
throw new class("Delegator for '{$resolved}' is not callable.") extends RuntimeException implements ContainerExceptionInterface {};
67+
throw new ContainerException("Delegator for '{$resolved}' is not callable.");
6968
}
7069

7170
$instance = $delegator($this, $resolved, static fn (): mixed => $current);
@@ -149,7 +148,7 @@ private function resolveAlias(string $id): string
149148

150149
while (isset($this->aliases[$id])) {
151150
if (isset($seen[$id])) {
152-
throw new RuntimeException("Circular alias detected for '{$id}'.");
151+
throw new ContainerException("Circular alias detected for '{$id}'.");
153152
}
154153

155154
$seen[$id] = true;

0 commit comments

Comments
 (0)