Skip to content

Commit 7325f42

Browse files
author
Kirill Nesmeyanov
committed
Improve type repositories
1 parent c4c7156 commit 7325f42

File tree

5 files changed

+36
-22
lines changed

5 files changed

+36
-22
lines changed

src/Runtime/Repository/InMemoryTypeRepository.php

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
namespace TypeLang\Mapper\Runtime\Repository;
66

7-
use JetBrains\PhpStorm\Language;
87
use TypeLang\Mapper\Type\TypeInterface;
98
use TypeLang\Parser\Node\Stmt\TypeStatement;
109

@@ -23,20 +22,10 @@ public function __construct(
2322
$this->types = new \WeakMap();
2423
}
2524

26-
public function getTypeByDefinition(#[Language('PHP')] string $definition, ?\ReflectionClass $context = null): TypeInterface
27-
{
28-
return $this->delegate->getTypeByDefinition($definition, $context);
29-
}
30-
31-
public function getTypeByValue(mixed $value, ?\ReflectionClass $context = null): TypeInterface
32-
{
33-
return $this->delegate->getTypeByValue($value, $context);
34-
}
35-
25+
#[\Override]
3626
public function getTypeByStatement(TypeStatement $statement, ?\ReflectionClass $context = null): TypeInterface
3727
{
3828
// @phpstan-ignore-next-line : PHPStan bug (array assign over readonly)
39-
return $this->types[$statement]
40-
??= $this->delegate->getTypeByStatement($statement, $context);
29+
return $this->types[$statement] ??= parent::getTypeByStatement($statement, $context);
4130
}
4231
}

src/Runtime/Repository/LoggableTypeRepository.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,15 @@ public function __construct(
1919
parent::__construct($delegate);
2020
}
2121

22+
#[\Override]
2223
public function getTypeByDefinition(#[Language('PHP')] string $definition, ?\ReflectionClass $context = null): TypeInterface
2324
{
2425
$this->logger->debug('Fetching the type by the definition "{definition}"', [
2526
'definition' => $definition,
2627
'context' => $context,
2728
]);
2829

29-
$result = $this->delegate->getTypeByDefinition($definition, $context);
30+
$result = parent::getTypeByDefinition($definition, $context);
3031

3132
$this->logger->info('The {type_name} was fetched by the definition "{definition}"', [
3233
'definition' => $definition,
@@ -38,14 +39,15 @@ public function getTypeByDefinition(#[Language('PHP')] string $definition, ?\Ref
3839
return new LoggableType($this->logger, $result);
3940
}
4041

42+
#[\Override]
4143
public function getTypeByValue(mixed $value, ?\ReflectionClass $context = null): TypeInterface
4244
{
4345
$this->logger->debug('Fetching the type by the value "{value}"', [
4446
'value' => $value,
4547
'context' => $context,
4648
]);
4749

48-
$result = $this->delegate->getTypeByValue($value, $context);
50+
$result = parent::getTypeByValue($value, $context);
4951

5052
$this->logger->info('The {type_name} was fetched by the value "{value}"', [
5153
'value' => $value,
@@ -57,6 +59,7 @@ public function getTypeByValue(mixed $value, ?\ReflectionClass $context = null):
5759
return new LoggableType($this->logger, $result);
5860
}
5961

62+
#[\Override]
6063
public function getTypeByStatement(TypeStatement $statement, ?\ReflectionClass $context = null): TypeInterface
6164
{
6265
$this->logger->debug('Fetching the type by the AST statement {statement_name}', [
@@ -65,7 +68,7 @@ public function getTypeByStatement(TypeStatement $statement, ?\ReflectionClass $
6568
'context' => $context,
6669
]);
6770

68-
$result = $this->delegate->getTypeByStatement($statement, $context);
71+
$result = parent::getTypeByStatement($statement, $context);
6972

7073
$this->logger->info('The {type_name} was fetched by the AST statement {statement_name}', [
7174
'statement' => $statement,

src/Runtime/Repository/TraceableTypeRepository.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ public function __construct(
1919
parent::__construct($delegate);
2020
}
2121

22+
#[\Override]
2223
public function getTypeByDefinition(
2324
#[Language('PHP')]
2425
string $definition,
@@ -29,7 +30,7 @@ public function getTypeByDefinition(
2930
try {
3031
$span->setAttribute('value', $definition);
3132

32-
$result = $this->delegate->getTypeByDefinition($definition, $context);
33+
$result = parent::getTypeByDefinition($definition, $context);
3334

3435
$span->setAttribute('result', $result);
3536
} finally {
@@ -39,14 +40,15 @@ public function getTypeByDefinition(
3940
return new TraceableType($this->tracer, $result);
4041
}
4142

43+
#[\Override]
4244
public function getTypeByValue(mixed $value, ?\ReflectionClass $context = null): TypeInterface
4345
{
4446
$span = $this->tracer->start(\sprintf('Fetching by value [%s]', \get_debug_type($value)));
4547

4648
try {
4749
$span->setAttribute('value', $value);
4850

49-
$result = $this->delegate->getTypeByValue($value, $context);
51+
$result = parent::getTypeByValue($value, $context);
5052

5153
$span->setAttribute('result', $result);
5254
} finally {
@@ -56,14 +58,15 @@ public function getTypeByValue(mixed $value, ?\ReflectionClass $context = null):
5658
return new TraceableType($this->tracer, $result);
5759
}
5860

61+
#[\Override]
5962
public function getTypeByStatement(TypeStatement $statement, ?\ReflectionClass $context = null): TypeInterface
6063
{
6164
$span = $this->tracer->start(\sprintf('Fetching by statement "%s"', \get_debug_type($statement)));
6265

6366
try {
6467
$span->setAttribute('value', $statement);
6568

66-
$result = $this->delegate->getTypeByStatement($statement, $context);
69+
$result = parent::getTypeByStatement($statement, $context);
6770

6871
$span->setAttribute('result', $result);
6972
} finally {

src/Runtime/Repository/TypeRepository.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,14 +63,14 @@ public function getTypeByDefinition(#[Language('PHP')] string $definition, ?\Ref
6363
{
6464
$statement = $this->parser->getStatementByDefinition($definition);
6565

66-
return $this->getTypeByStatement($statement, $context);
66+
return $this->inner->getTypeByStatement($statement, $context);
6767
}
6868

6969
public function getTypeByValue(mixed $value, ?\ReflectionClass $context = null): TypeInterface
7070
{
7171
$statement = $this->parser->getStatementByValue($value);
7272

73-
return $this->getTypeByStatement($statement, $context);
73+
return $this->inner->getTypeByStatement($statement, $context);
7474
}
7575

7676
public function getTypeByStatement(TypeStatement $statement, ?\ReflectionClass $context = null): TypeInterface

src/Runtime/Repository/TypeRepositoryDecorator.php

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,16 @@
44

55
namespace TypeLang\Mapper\Runtime\Repository;
66

7+
use JetBrains\PhpStorm\Language;
8+
use TypeLang\Mapper\Type\TypeInterface;
9+
use TypeLang\Parser\Node\Stmt\TypeStatement;
10+
711
abstract class TypeRepositoryDecorator implements
812
TypeRepositoryInterface,
913
InnerTypeRepositoryContainerInterface
1014
{
1115
public function __construct(
12-
protected readonly TypeRepositoryInterface $delegate,
16+
private readonly TypeRepositoryInterface $delegate,
1317
) {
1418
$this->setInnerContext($this);
1519
}
@@ -22,4 +26,19 @@ public function setInnerContext(TypeRepositoryInterface $inner): void
2226

2327
$this->delegate->setInnerContext($this);
2428
}
29+
30+
public function getTypeByDefinition(#[Language('PHP')] string $definition, ?\ReflectionClass $context = null): TypeInterface
31+
{
32+
return $this->delegate->getTypeByDefinition($definition, $context);
33+
}
34+
35+
public function getTypeByValue(mixed $value, ?\ReflectionClass $context = null): TypeInterface
36+
{
37+
return $this->delegate->getTypeByValue($value, $context);
38+
}
39+
40+
public function getTypeByStatement(TypeStatement $statement, ?\ReflectionClass $context = null): TypeInterface
41+
{
42+
return $this->delegate->getTypeByStatement($statement, $context);
43+
}
2544
}

0 commit comments

Comments
 (0)