Skip to content

Commit 70110ac

Browse files
author
Kirill Nesmeyanov
committed
Improve type decorators logic
1 parent d3450c0 commit 70110ac

File tree

3 files changed

+20
-6
lines changed

3 files changed

+20
-6
lines changed

src/Runtime/Repository/LoggableTypeRepository.php

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
use Psr\Log\LoggerInterface;
88
use TypeLang\Mapper\Runtime\Repository\TypeDecorator\LoggableType;
9+
use TypeLang\Mapper\Runtime\Repository\TypeDecorator\TypeDecoratorInterface;
910
use TypeLang\Mapper\Type\TypeInterface;
1011
use TypeLang\Parser\Node\Stmt\TypeStatement;
1112

@@ -27,13 +28,17 @@ public function getTypeByStatement(TypeStatement $statement, ?\ReflectionClass $
2728
'context' => $context,
2829
]);
2930

30-
$result = parent::getTypeByStatement($statement, $context);
31+
$type = $result = parent::getTypeByStatement($statement, $context);
32+
33+
if ($type instanceof TypeDecoratorInterface) {
34+
$type = $type->getDecoratedType();
35+
}
3136

3237
$this->logger->info('The {type_name} was fetched by the AST statement {statement_name}', [
3338
'statement' => $statement,
3439
'statement_name' => $statement::class . '#' . \spl_object_id($statement),
35-
'type' => $result,
36-
'type_name' => $result::class . '#' . \spl_object_id($result),
40+
'type' => $type,
41+
'type_name' => $type::class . '#' . \spl_object_id($type),
3742
'context' => $context,
3843
]);
3944

src/Runtime/Repository/TraceableTypeRepository.php

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,10 @@ public function __construct(
2121
) {
2222
parent::__construct($delegate);
2323

24-
$this->printer = new PrettyPrinter();
24+
$this->printer = new PrettyPrinter(
25+
wrapUnionType: false,
26+
multilineShape: \PHP_INT_MAX,
27+
);
2528
}
2629

2730
public function getTypeByStatement(TypeStatement $statement, ?\ReflectionClass $context = null): TypeInterface
@@ -44,6 +47,10 @@ public function getTypeByStatement(TypeStatement $statement, ?\ReflectionClass $
4447
return $result;
4548
}
4649

47-
return new TraceableType($this->tracer, $result);
50+
return new TraceableType(
51+
definition: $this->printer->print($statement),
52+
tracer: $this->tracer,
53+
delegate: $result,
54+
);
4855
}
4956
}

src/Runtime/Repository/TypeDecorator/TraceableType.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ final class TraceableType extends TypeDecorator
2121
private readonly string $name;
2222

2323
public function __construct(
24+
private readonly string $definition,
2425
private readonly TracerInterface $tracer,
2526
TypeInterface $delegate,
2627
) {
@@ -29,7 +30,8 @@ public function __construct(
2930
$inner = $this->getDecoratedType();
3031

3132
$this->name = self::getShortName($inner::class)
32-
. '#' . \spl_object_id($inner);
33+
. '#' . \spl_object_id($inner)
34+
. ' as "' . $this->definition . '"';
3335
}
3436

3537
/**

0 commit comments

Comments
 (0)