Skip to content

Commit 2443663

Browse files
committed
DI Container - throw unprefixed exception
1 parent 3f70082 commit 2443663

File tree

5 files changed

+61
-2
lines changed

5 files changed

+61
-2
lines changed

build/phpstan.neon

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ parameters:
5858
- 'PHPStan\Broker\ClassNotFoundException'
5959
- 'PHPStan\Broker\FunctionNotFoundException'
6060
- 'PHPStan\Broker\ConstantNotFoundException'
61+
- 'PHPStan\DependencyInjection\MissingServiceException'
6162
- 'PHPStan\Reflection\MissingMethodFromReflectionException'
6263
- 'PHPStan\Reflection\MissingPropertyFromReflectionException'
6364
- 'PHPStan\Reflection\MissingConstantFromReflectionException'

src/DependencyInjection/Container.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,15 @@ public function hasService(string $serviceName): bool;
1010

1111
/**
1212
* @return mixed
13+
* @throws MissingServiceException
1314
*/
1415
public function getService(string $serviceName);
1516

1617
/**
1718
* @template T of object
1819
* @param class-string<T> $className
1920
* @return T
21+
* @throws MissingServiceException
2022
*/
2123
public function getByType(string $className);
2224

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace PHPStan\DependencyInjection;
4+
5+
use Exception;
6+
7+
final class MissingServiceException extends Exception
8+
{
9+
10+
}

src/DependencyInjection/Nette/NetteContainer.php

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace PHPStan\DependencyInjection\Nette;
44

55
use PHPStan\DependencyInjection\Container;
6+
use PHPStan\DependencyInjection\MissingServiceException;
67
use PHPStan\DependencyInjection\ParameterNotFoundException;
78
use function array_key_exists;
89
use function array_keys;
@@ -28,7 +29,11 @@ public function hasService(string $serviceName): bool
2829
*/
2930
public function getService(string $serviceName)
3031
{
31-
return $this->container->getService($serviceName);
32+
try {
33+
return $this->container->getService($serviceName);
34+
} catch (\Nette\DI\MissingServiceException $e) {
35+
throw new MissingServiceException($e->getMessage(), previous: $e);
36+
}
3237
}
3338

3439
/**
@@ -38,7 +43,11 @@ public function getService(string $serviceName)
3843
*/
3944
public function getByType(string $className)
4045
{
41-
return $this->container->getByType($className);
46+
try {
47+
return $this->container->getByType($className);
48+
} catch (\Nette\DI\MissingServiceException $e) {
49+
throw new MissingServiceException($e->getMessage(), previous: $e);
50+
}
4251
}
4352

4453
/**
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace PHPStan\DependencyInjection\Nette;
4+
5+
use PHPStan\DependencyInjection\MissingServiceException;
6+
use PHPStan\Testing\PHPStanTestCase;
7+
use PHPStan\TrinaryLogic;
8+
use PHPStan\Type\Php\ReflectionGetAttributesMethodReturnTypeExtension;
9+
10+
class NetteContainerTest extends PHPStanTestCase
11+
{
12+
13+
public function testGetServiceThrows(): void
14+
{
15+
$container = self::getContainer();
16+
17+
$this->expectException(MissingServiceException::class);
18+
$container->getService('nonexistent');
19+
}
20+
21+
public function testGetByTypeNotFoundThrows(): void
22+
{
23+
$container = self::getContainer();
24+
25+
$this->expectException(MissingServiceException::class);
26+
$container->getByType(TrinaryLogic::class);
27+
}
28+
29+
public function testGetByTypeNotUniqueThrows(): void
30+
{
31+
$container = self::getContainer();
32+
33+
$this->expectException(MissingServiceException::class);
34+
$container->getByType(ReflectionGetAttributesMethodReturnTypeExtension::class);
35+
}
36+
37+
}

0 commit comments

Comments
 (0)