Skip to content

Commit fbe47f2

Browse files
author
Kirill Nesmeyanov
committed
A little exceptions and codestyle (120 chars per line) improvements
1 parent 2300c3c commit fbe47f2

File tree

2 files changed

+63
-9
lines changed

2 files changed

+63
-9
lines changed

src/Type/Builder/CallableTypeBuilder.php

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,30 @@
44

55
namespace TypeLang\Mapper\Type\Builder;
66

7+
use TypeLang\Mapper\Exception\Definition\InternalTypeException;
78
use TypeLang\Mapper\Runtime\Parser\TypeParserInterface;
89
use TypeLang\Mapper\Runtime\Repository\TypeRepositoryInterface;
910
use TypeLang\Mapper\Type\TypeInterface;
1011
use TypeLang\Parser\Node\Stmt\TypeStatement;
1112

1213
/**
14+
* Creates a new type from a callback.
15+
*
16+
* ```
17+
* // ExamplePlatform.php
18+
* yield new CallableTypeBuilder(
19+
* names: 'custom-string',
20+
* factory: static fn(): TypeInterface => new CustomString(),
21+
* );
22+
* ```
23+
*
1324
* @template-extends NamedTypeBuilder<TypeInterface>
1425
*/
1526
class CallableTypeBuilder extends NamedTypeBuilder
1627
{
1728
/**
1829
* @param non-empty-array<non-empty-string>|non-empty-string $names
19-
* @param \Closure(): TypeInterface $factory
30+
* @param \Closure(): TypeInterface $factory
2031
*/
2132
public function __construct(
2233
array|string $names,
@@ -25,11 +36,22 @@ public function __construct(
2536
parent::__construct($names);
2637
}
2738

28-
public function build(TypeStatement $statement, TypeRepositoryInterface $types, TypeParserInterface $parser): TypeInterface
29-
{
39+
public function build(
40+
TypeStatement $statement,
41+
TypeRepositoryInterface $types,
42+
TypeParserInterface $parser,
43+
): TypeInterface {
3044
$this->expectNoShapeFields($statement);
3145
$this->expectNoTemplateArguments($statement);
3246

33-
return ($this->factory)();
47+
try {
48+
return ($this->factory)();
49+
} catch (\Throwable $e) {
50+
throw InternalTypeException::becauseInternalTypeErrorOccurs(
51+
type: $statement,
52+
message: 'An error occurred while trying to fetch {{type}} type from callback',
53+
previous: $e,
54+
);
55+
}
3456
}
3557
}

src/Type/Builder/PsrContainerTypeBuilder.php

Lines changed: 37 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,33 @@
55
namespace TypeLang\Mapper\Type\Builder;
66

77
use Psr\Container\ContainerInterface;
8+
use TypeLang\Mapper\Exception\Definition\InternalTypeException;
89
use TypeLang\Mapper\Runtime\Parser\TypeParserInterface;
910
use TypeLang\Mapper\Runtime\Repository\TypeRepositoryInterface;
1011
use TypeLang\Mapper\Type\TypeInterface;
1112
use TypeLang\Parser\Node\Stmt\TypeStatement;
1213

1314
/**
15+
* Creates a new type from a PSR-compatible container using service-location.
16+
*
17+
* ```
18+
* // ExamplePlatform.php
19+
* yield new CallableTypeBuilder(
20+
* names: 'custom-string',
21+
* serviceId: CustomString::class,
22+
* container: $psrCompatibleContainer,
23+
* );
24+
* ```
25+
*
26+
* Tip: When using Symfony, make sure the service is not private.
27+
*
1428
* @template-extends NamedTypeBuilder<TypeInterface>
1529
*/
1630
class PsrContainerTypeBuilder extends NamedTypeBuilder
1731
{
1832
/**
1933
* @param non-empty-array<non-empty-string>|non-empty-string $names
20-
* @param class-string<TypeInterface> $serviceId
34+
* @param class-string<TypeInterface> $serviceId
2135
*/
2236
public function __construct(
2337
array|string $names,
@@ -27,15 +41,33 @@ public function __construct(
2741
parent::__construct($names);
2842
}
2943

30-
public function build(TypeStatement $statement, TypeRepositoryInterface $types, TypeParserInterface $parser): TypeInterface
31-
{
44+
public function build(
45+
TypeStatement $statement,
46+
TypeRepositoryInterface $types,
47+
TypeParserInterface $parser,
48+
): TypeInterface {
3249
$this->expectNoShapeFields($statement);
3350
$this->expectNoTemplateArguments($statement);
3451

35-
$service = $this->container->get($this->serviceId);
52+
try {
53+
$service = $this->container->get($this->serviceId);
54+
} catch (\Throwable $e) {
55+
throw InternalTypeException::becauseInternalTypeErrorOccurs(
56+
type: $statement,
57+
message: 'An error occurred while trying to fetch {{type}} type from service container',
58+
previous: $e,
59+
);
60+
}
3661

3762
if (!$service instanceof TypeInterface) {
38-
throw new \RuntimeException("Type service '{$this->serviceId}' does not implement TypeLang\Mapper\Type\TypeInterface");
63+
throw InternalTypeException::becauseInternalTypeErrorOccurs(
64+
type: $statement,
65+
message: \sprintf(
66+
'Received service from service container defined as {{type}} must be instanceof %s, but %s given',
67+
TypeInterface::class,
68+
\get_debug_type($service),
69+
),
70+
);
3971
}
4072

4173
return $service;

0 commit comments

Comments
 (0)