Skip to content

Commit d682fe3

Browse files
committed
Simplify metadata accessors
1 parent abe4dcd commit d682fe3

21 files changed

+112
-190
lines changed

example/01.normalization/08.object-output-normalization.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ public function __construct(
2323

2424
$mapper = new Mapper(
2525
config: new Configuration(
26-
objectsAsArrays: true,
26+
isObjectsAsArrays: true,
2727
),
2828
);
2929

src/Mapping/Driver/ArrayConfigDriver.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -197,13 +197,13 @@ class: $reflection,
197197
// @phpstan-ignore-next-line : Additional DbC invariant
198198
assert(\is_string($propertyConfig['type']));
199199

200-
$metadata->setTypeInfo($this->createPropertyType(
200+
$metadata->type = $this->createPropertyType(
201201
class: $reflection,
202202
propertyName: $propertyName,
203203
propertyType: $propertyConfig['type'],
204204
types: $types,
205205
parser: $parser,
206-
));
206+
);
207207
}
208208

209209
// -----------------------------------------------------------------
@@ -215,7 +215,7 @@ class: $reflection,
215215
// @phpstan-ignore-next-line : Additional DbC invariant
216216
assert(\is_string($propertyConfig['name']));
217217

218-
$metadata->setExportName($propertyConfig['name']);
218+
$metadata->alias = $propertyConfig['name'];
219219
}
220220

221221
// -----------------------------------------------------------------
@@ -242,7 +242,7 @@ class: $reflection,
242242
expression: $this->createExpression($propertyConfigSkip, [
243243
ExpressionConditionMetadata::DEFAULT_CONTEXT_VARIABLE_NAME,
244244
]),
245-
context: ExpressionConditionMetadata::DEFAULT_CONTEXT_VARIABLE_NAME,
245+
variable: ExpressionConditionMetadata::DEFAULT_CONTEXT_VARIABLE_NAME,
246246
)
247247
});
248248
}

src/Mapping/Driver/AttributeDriver.php

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -95,44 +95,49 @@ protected function load(
9595
// -----------------------------------------------------------------
9696

9797
$attribute = $this->findPropertyAttribute($property, MapType::class);
98+
9899
if ($attribute !== null) {
99-
$metadata->setTypeInfo($this->createPropertyType(
100+
$metadata->type = $this->createPropertyType(
100101
type: $attribute->type,
101102
property: $property,
102103
types: $types,
103104
parser: $parser,
104-
));
105+
);
105106
}
106107

107108
// -----------------------------------------------------------------
108109
// Apply property name
109110
// -----------------------------------------------------------------
110111

111112
$attribute = $this->findPropertyAttribute($property, MapName::class);
113+
112114
if ($attribute !== null) {
113-
$metadata->setExportName($attribute->name);
115+
$metadata->alias = $attribute->name;
114116
}
115117

116118
// -----------------------------------------------------------------
117119
// Apply skip conditions
118120
// -----------------------------------------------------------------
119121

120122
$conditions = $this->getAllPropertyAttributes($property, SkipWhen::class);
123+
121124
foreach ($conditions as $condition) {
122125
$metadata->addSkipCondition(new ExpressionConditionMetadata(
123126
expression: $this->createExpression($condition->expr, [
124127
$condition->context,
125128
]),
126-
context: $condition->context,
129+
variable: $condition->context,
127130
));
128131
}
129132

130133
$condition = $this->findPropertyAttribute($property, SkipWhenEmpty::class);
134+
131135
if ($condition !== null) {
132136
$metadata->addSkipCondition(new EmptyConditionMetadata());
133137
}
134138

135139
$condition = $this->findPropertyAttribute($property, SkipWhenNull::class);
140+
136141
if ($condition !== null) {
137142
$metadata->addSkipCondition(new NullConditionMetadata());
138143
}
@@ -143,6 +148,7 @@ protected function load(
143148
// -----------------------------------------------------------------
144149

145150
$attribute = $this->findClassAttribute($reflection, DiscriminatorMap::class);
151+
146152
if ($attribute !== null) {
147153
$mapping = [];
148154
$default = null;

src/Mapping/Driver/DocBlockDriver.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ private static function assertKernelPackageIsInstalled(): void
106106
*/
107107
private function findType(\ReflectionClass $class, PropertyMetadata $meta): ?TypeStatement
108108
{
109-
$property = $class->getProperty($meta->getName());
109+
$property = $class->getProperty($meta->name);
110110

111111
if ($property->isPromoted()) {
112112
return $this->promotedProperties->findType($property, $meta);
@@ -133,16 +133,16 @@ protected function load(
133133
} catch (TypeNotFoundException $e) {
134134
throw PropertyTypeNotFoundException::becauseTypeOfPropertyNotDefined(
135135
class: $class->name,
136-
property: $metadata->getName(),
136+
property: $metadata->name,
137137
type: $e->getType(),
138138
previous: $e,
139139
);
140140
}
141141

142-
$metadata->setTypeInfo(new TypeMetadata(
142+
$metadata->type = new TypeMetadata(
143143
type: $type,
144144
statement: $statement,
145-
));
145+
);
146146
}
147147

148148
$class->addProperty($metadata);

src/Mapping/Driver/DocBlockDriver/PromotedPropertyTypeDriver.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ private function isExpectedParamTag(TagInterface $tag, PropertyMetadata $meta):
6565
{
6666
return $tag instanceof ParamTag
6767
&& $tag->getName() === $this->paramTagName
68-
&& $tag->getVariableName() === $meta->getName();
68+
&& $tag->getVariableName() === $meta->name;
6969
}
7070

7171
/**

src/Mapping/Driver/ReflectionDriver.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,10 +110,10 @@ class: $class->getName(),
110110
);
111111
}
112112

113-
$meta->setTypeInfo(new TypeMetadata(
113+
$meta->type = new TypeMetadata(
114114
type: $type,
115115
statement: $statement,
116-
));
116+
);
117117
}
118118

119119
/**

src/Mapping/Metadata/ClassMetadata.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ public function getTypeStatement(Context $context): TypeStatement
108108
*/
109109
public function addProperty(PropertyMetadata $property): void
110110
{
111-
$this->properties[$property->getName()] = $property;
111+
$this->properties[$property->name] = $property;
112112
}
113113

114114
/**

src/Mapping/Metadata/ExpressionConditionMetadata.php

Lines changed: 9 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,16 @@ final class ExpressionConditionMetadata extends MatchConditionMetadata
1414
public const DEFAULT_CONTEXT_VARIABLE_NAME = 'this';
1515

1616
/**
17-
* @param non-empty-string $context
17+
* @param non-empty-string $variable
1818
*/
1919
public function __construct(
20-
private readonly ParsedExpression $expression,
21-
private readonly string $context = self::DEFAULT_CONTEXT_VARIABLE_NAME,
20+
public readonly ParsedExpression $expression,
21+
/**
22+
* Gets expression variable name.
23+
*
24+
* @var non-empty-string
25+
*/
26+
public readonly string $variable = self::DEFAULT_CONTEXT_VARIABLE_NAME,
2227
?int $createdAt = null,
2328
) {
2429
parent::__construct($createdAt);
@@ -29,25 +34,7 @@ public function match(object $object, mixed $value): bool
2934
$nodes = $this->expression->getNodes();
3035

3136
return (bool) $nodes->evaluate([], [
32-
$this->getContextVariableName() => $object,
37+
$this->variable => $object,
3338
]);
3439
}
35-
36-
/**
37-
* @api
38-
*
39-
* @return non-empty-string
40-
*/
41-
public function getContextVariableName(): string
42-
{
43-
return $this->context;
44-
}
45-
46-
/**
47-
* @api
48-
*/
49-
public function getExpression(): ParsedExpression
50-
{
51-
return $this->expression;
52-
}
5340
}

src/Mapping/Metadata/Metadata.php

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

77
abstract class Metadata
88
{
9-
private readonly int $timestamp;
9+
/**
10+
* Gets the metadata creation timestamp in seconds.
11+
*/
12+
public readonly int $timestamp;
1013

1114
public function __construct(?int $createdAt = null)
1215
{
@@ -24,14 +27,4 @@ private function getCurrentTimestamp(): int
2427

2528
return $date->getTimestamp();
2629
}
27-
28-
/**
29-
* Returns the metadata creation timestamp in seconds.
30-
*
31-
* @api
32-
*/
33-
public function getTimestamp(): int
34-
{
35-
return $this->timestamp;
36-
}
3730
}

src/Mapping/Metadata/PropertyMetadata.php

Lines changed: 17 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,11 @@
1212
final class PropertyMetadata extends Metadata
1313
{
1414
/**
15+
* Gets property public name.
16+
*
1517
* @var non-empty-string
1618
*/
17-
private readonly string $name;
19+
public string $alias;
1820

1921
private mixed $defaultValue = null;
2022

@@ -25,15 +27,20 @@ final class PropertyMetadata extends Metadata
2527
*/
2628
private array $skipWhen = [];
2729

28-
/**
29-
* @param non-empty-string $export
30-
*/
3130
public function __construct(
32-
private string $export,
33-
private ?TypeMetadata $type = null,
31+
/**
32+
* Gets property real name.
33+
*
34+
* @var non-empty-string
35+
*/
36+
public readonly string $name,
37+
/**
38+
* Gets property type info.
39+
*/
40+
public ?TypeMetadata $type = null,
3441
?int $createdAt = null,
3542
) {
36-
$this->name = $this->export;
43+
$this->alias = $this->name;
3744

3845
parent::__construct($createdAt);
3946
}
@@ -45,7 +52,7 @@ public function __construct(
4552
*/
4653
public function getTypeStatement(Context $context): ?TypeStatement
4754
{
48-
$info = $this->findTypeInfo();
55+
$info = $this->type;
4956

5057
if ($info === null) {
5158
return null;
@@ -73,10 +80,10 @@ public function getFieldNode(Context $context): ?NamedFieldNode
7380
return null;
7481
}
7582

76-
$name = $this->getName();
83+
$name = $this->name;
7784

7885
if ($context->isDenormalization()) {
79-
$name = $this->getExportName();
86+
$name = $this->alias;
8087
}
8188

8289
return new NamedFieldNode(
@@ -86,77 +93,6 @@ public function getFieldNode(Context $context): ?NamedFieldNode
8693
);
8794
}
8895

89-
/**
90-
* Returns property real name.
91-
*
92-
* @api
93-
*
94-
* @return non-empty-string
95-
*/
96-
public function getName(): string
97-
{
98-
return $this->name;
99-
}
100-
101-
/**
102-
* @api
103-
*
104-
* @param non-empty-string $name
105-
*/
106-
public function setExportName(string $name): void
107-
{
108-
$this->export = $name;
109-
}
110-
111-
/**
112-
* Returns property public name.
113-
*
114-
* @api
115-
*
116-
* @return non-empty-string
117-
*/
118-
public function getExportName(): string
119-
{
120-
return $this->export;
121-
}
122-
123-
/**
124-
* @api
125-
*/
126-
public function setTypeInfo(TypeMetadata $type): void
127-
{
128-
$this->type = $type;
129-
}
130-
131-
/**
132-
* @api
133-
*/
134-
public function removeTypeInfo(): void
135-
{
136-
$this->type = null;
137-
}
138-
139-
/**
140-
* @api
141-
*/
142-
public function hasTypeInfo(): bool
143-
{
144-
return $this->type !== null;
145-
}
146-
147-
/**
148-
* Note: The prefix "find" is used to indicate that the {@see TypeMetadata}
149-
* definition may be optional and method may return {@see null}.
150-
* The prefix "get" is used when the value is forced to be obtained
151-
* and should throw an exception if the type definition is missing.
152-
*
153-
* @api
154-
*/
155-
public function findTypeInfo(): ?TypeMetadata
156-
{
157-
return $this->type;
158-
}
159-
16096
/**
16197
* Adds new skip condition.
16298
*

0 commit comments

Comments
 (0)