Skip to content

Commit af84958

Browse files
committed
Add docblock reader
1 parent 0e2d8c2 commit af84958

28 files changed

+653
-47
lines changed

src/Mapping/Metadata/ClassMetadata/PropertyInfo.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use TypeLang\Mapper\Mapping\Metadata\ClassMetadata\PropertyMetadata\DefaultValueInfo;
88
use TypeLang\Mapper\Mapping\Metadata\ConditionInfo;
99
use TypeLang\Mapper\Mapping\Metadata\MetadataInfo;
10+
use TypeLang\Mapper\Mapping\Metadata\ParsedTypeInfo;
1011
use TypeLang\Mapper\Mapping\Metadata\SourceInfo;
1112
use TypeLang\Mapper\Mapping\Metadata\TypeInfo;
1213

@@ -46,7 +47,7 @@ public function __construct(
4647
?SourceInfo $source = null,
4748
) {
4849
$this->alias = $name;
49-
$this->read = $this->write = new TypeInfo('mixed');
50+
$this->read = $this->write = ParsedTypeInfo::mixed();
5051

5152
parent::__construct($source);
5253
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace TypeLang\Mapper\Mapping\Metadata;
6+
7+
use TypeLang\Parser\Node\Stmt\NamedTypeNode;
8+
use TypeLang\Parser\Node\Stmt\TypeStatement;
9+
10+
final class ParsedTypeInfo extends TypeInfo
11+
{
12+
private static self $mixed;
13+
14+
public function __construct(
15+
public readonly TypeStatement $statement,
16+
?SourceInfo $source = null,
17+
) {
18+
parent::__construct($source);
19+
}
20+
21+
public static function mixed(): self
22+
{
23+
return self::$mixed ??= new self(
24+
statement: new NamedTypeNode('mixed'),
25+
);
26+
}
27+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace TypeLang\Mapper\Mapping\Metadata;
6+
7+
final class RawTypeInfo extends TypeInfo
8+
{
9+
private static self $mixed;
10+
11+
public function __construct(
12+
/**
13+
* @var non-empty-string
14+
*/
15+
public readonly string $definition,
16+
?SourceInfo $source = null,
17+
) {
18+
parent::__construct($source);
19+
}
20+
21+
public static function mixed(): self
22+
{
23+
return self::$mixed ??= new self('mixed');
24+
}
25+
}

src/Mapping/Metadata/TypeInfo.php

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,4 @@
44

55
namespace TypeLang\Mapper\Mapping\Metadata;
66

7-
final class TypeInfo extends MetadataInfo
8-
{
9-
public function __construct(
10-
/**
11-
* @var non-empty-string
12-
*/
13-
public readonly string $definition,
14-
?SourceInfo $source = null,
15-
) {
16-
parent::__construct($source);
17-
}
18-
}
7+
abstract class TypeInfo extends MetadataInfo {}

src/Mapping/Provider/MetadataReaderProvider.php

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,16 +26,19 @@
2626
use TypeLang\Mapper\Mapping\Metadata\Condition\NullConditionMetadata;
2727
use TypeLang\Mapper\Mapping\Metadata\ConditionInfo;
2828
use TypeLang\Mapper\Mapping\Metadata\ConditionMetadata;
29+
use TypeLang\Mapper\Mapping\Metadata\ParsedTypeInfo;
30+
use TypeLang\Mapper\Mapping\Metadata\RawTypeInfo;
2931
use TypeLang\Mapper\Mapping\Metadata\TypeInfo;
3032
use TypeLang\Mapper\Mapping\Metadata\TypeMetadata;
3133
use TypeLang\Mapper\Mapping\Reader\ReaderInterface;
34+
use TypeLang\Mapper\Mapping\Reader\ReflectionReader;
3235
use TypeLang\Mapper\Runtime\Parser\TypeParserInterface;
3336
use TypeLang\Mapper\Runtime\Repository\TypeRepositoryInterface;
3437

3538
final class MetadataReaderProvider implements ProviderInterface
3639
{
3740
public function __construct(
38-
private readonly ReaderInterface $reader,
41+
private readonly ReaderInterface $reader = new ReflectionReader(),
3942
private ?ExpressionLanguage $expression = null,
4043
private readonly ?ClockInterface $clock = null,
4144
) {}
@@ -52,9 +55,7 @@ public function getClassMetadata(
5255
TypeRepositoryInterface $types,
5356
TypeParserInterface $parser,
5457
): ClassMetadata {
55-
$info = $this->reader->read($class);
56-
57-
dd($info);
58+
$info = $this->reader->read($class, $parser);
5859

5960
return $this->toClassMetadata($info, $types, $parser);
6061
}
@@ -302,7 +303,15 @@ private function toTypeMetadata(
302303
TypeRepositoryInterface $types,
303304
TypeParserInterface $parser,
304305
): TypeMetadata {
305-
$statement = $parser->getStatementByDefinition($info->definition);
306+
$statement = match (true) {
307+
$info instanceof RawTypeInfo => $parser->getStatementByDefinition($info->definition),
308+
$info instanceof ParsedTypeInfo => $info->statement,
309+
default => throw new \InvalidArgumentException(\sprintf(
310+
'Unsupported type info "%s"',
311+
$info::class,
312+
))
313+
};
314+
306315
$type = $types->getTypeByStatement($statement);
307316

308317
return new TypeMetadata(

src/Mapping/Reader/AttributeReader/DiscriminatorMapClassAttributeLoader.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
use TypeLang\Mapper\Mapping\DiscriminatorMap;
88
use TypeLang\Mapper\Mapping\Metadata\ClassInfo;
99
use TypeLang\Mapper\Mapping\Metadata\ClassMetadata\DiscriminatorInfo;
10-
use TypeLang\Mapper\Mapping\Metadata\TypeInfo;
10+
use TypeLang\Mapper\Mapping\Metadata\RawTypeInfo;
1111

1212
final class DiscriminatorMapClassAttributeLoader extends ClassAttributeLoader
1313
{
@@ -22,13 +22,13 @@ public function load(ClassInfo $info, \ReflectionClass $class): void
2222
$default = null;
2323

2424
if ($attribute->otherwise !== null) {
25-
$default = new TypeInfo($attribute->otherwise);
25+
$default = new RawTypeInfo($attribute->otherwise);
2626
}
2727

2828
$map = [];
2929

3030
foreach ($attribute->map as $value => $type) {
31-
$map[$value] = new TypeInfo($type);
31+
$map[$value] = new RawTypeInfo($type);
3232
}
3333

3434
$info->discriminator = new DiscriminatorInfo(

src/Mapping/Reader/AttributeReader/TypePropertyAttributeLoader.php

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

77
use TypeLang\Mapper\Mapping\MapType;
88
use TypeLang\Mapper\Mapping\Metadata\ClassMetadata\PropertyInfo;
9+
use TypeLang\Mapper\Mapping\Metadata\RawTypeInfo;
910
use TypeLang\Mapper\Mapping\Metadata\SourceInfo;
10-
use TypeLang\Mapper\Mapping\Metadata\TypeInfo;
1111

1212
final class TypePropertyAttributeLoader extends PropertyAttributeLoader
1313
{
@@ -49,7 +49,7 @@ private function loadPropertyType(\ReflectionProperty $property, PropertyInfo $p
4949
return;
5050
}
5151

52-
$prototype->read = $prototype->write = new TypeInfo(
52+
$prototype->read = $prototype->write = new RawTypeInfo(
5353
definition: $attribute->type,
5454
source: $this->findSourceMap($property),
5555
);
@@ -69,7 +69,7 @@ private function loadReadHookType(\ReflectionProperty $property, PropertyInfo $p
6969
return;
7070
}
7171

72-
$prototype->read = new TypeInfo(
72+
$prototype->read = new RawTypeInfo(
7373
definition: $attribute->type,
7474
);
7575
}
@@ -88,7 +88,7 @@ private function loadWriteHookType(\ReflectionProperty $property, PropertyInfo $
8888
return;
8989
}
9090

91-
$prototype->write = new TypeInfo(
91+
$prototype->write = new RawTypeInfo(
9292
definition: $attribute->type,
9393
);
9494
}

src/Mapping/Reader/ConfigReader.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use TypeLang\Mapper\Mapping\Reader\ConfigReader\SchemaValidator;
1515
use TypeLang\Mapper\Mapping\Reader\ConfigReader\SkipConditionsPropertyConfigLoader;
1616
use TypeLang\Mapper\Mapping\Reader\ConfigReader\TypePropertyConfigLoader;
17+
use TypeLang\Mapper\Runtime\Parser\TypeParserInterface;
1718

1819
/**
1920
* @phpstan-import-type ClassConfigType from SchemaValidator
@@ -87,9 +88,9 @@ private function loadAndValidate(\ReflectionClass $class): ?array
8788
return $config;
8889
}
8990

90-
public function read(\ReflectionClass $class): ClassInfo
91+
public function read(\ReflectionClass $class, TypeParserInterface $parser): ClassInfo
9192
{
92-
$info = parent::read($class);
93+
$info = parent::read($class, $parser);
9394

9495
$config = $this->loadAndValidate($class);
9596

src/Mapping/Reader/ConfigReader/DiscriminatorMapClassConfigLoader.php

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

77
use TypeLang\Mapper\Mapping\Metadata\ClassInfo;
88
use TypeLang\Mapper\Mapping\Metadata\ClassMetadata\DiscriminatorInfo;
9-
use TypeLang\Mapper\Mapping\Metadata\TypeInfo;
9+
use TypeLang\Mapper\Mapping\Metadata\RawTypeInfo;
1010

1111
final class DiscriminatorMapClassConfigLoader extends ClassConfigLoader
1212
{
@@ -21,13 +21,13 @@ public function load(ClassInfo $info, array $config): void
2121
$map = [];
2222

2323
foreach ($discriminatorConfig['map'] as $value => $type) {
24-
$map[$value] = new TypeInfo($type);
24+
$map[$value] = new RawTypeInfo($type);
2525
}
2626

2727
$default = null;
2828

2929
if (isset($discriminatorConfig['otherwise'])) {
30-
$default = new TypeInfo($discriminatorConfig['otherwise']);
30+
$default = new RawTypeInfo($discriminatorConfig['otherwise']);
3131
}
3232

3333
$info->discriminator = new DiscriminatorInfo(

src/Mapping/Reader/ConfigReader/TypePropertyConfigLoader.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
namespace TypeLang\Mapper\Mapping\Reader\ConfigReader;
66

77
use TypeLang\Mapper\Mapping\Metadata\ClassMetadata\PropertyInfo;
8-
use TypeLang\Mapper\Mapping\Metadata\TypeInfo;
8+
use TypeLang\Mapper\Mapping\Metadata\RawTypeInfo;
99

1010
/**
1111
* @phpstan-import-type PropertyConfigType from SchemaValidator
@@ -27,7 +27,7 @@ private function loadPropertyType(PropertyInfo $info, array $config): void
2727
return;
2828
}
2929

30-
$info->read = $info->write = new TypeInfo(
30+
$info->read = $info->write = new RawTypeInfo(
3131
definition: $config['type'],
3232
);
3333
}
@@ -41,7 +41,7 @@ private function loadWritePropertyType(PropertyInfo $info, array $config): void
4141
return;
4242
}
4343

44-
$info->write = new TypeInfo(
44+
$info->write = new RawTypeInfo(
4545
definition: $config['write'],
4646
);
4747
}

0 commit comments

Comments
 (0)