Skip to content

Commit f13eddb

Browse files
committed
use readonly properties for metadata
1 parent bd6e9db commit f13eddb

7 files changed

Lines changed: 131 additions & 190 deletions

File tree

baseline.xml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,9 @@
2222
<code><![CDATA[$rawData]]></code>
2323
</MixedArgument>
2424
<MixedAssignment>
25-
<code><![CDATA[$data[$propertyMetadata->fieldName()]]]></code>
26-
<code><![CDATA[$data[$propertyMetadata->fieldName()]]]></code>
27-
<code><![CDATA[$data[$propertyMetadata->fieldName()]]]></code>
25+
<code><![CDATA[$data[$propertyMetadata->fieldName]]]></code>
26+
<code><![CDATA[$data[$propertyMetadata->fieldName]]]></code>
27+
<code><![CDATA[$data[$propertyMetadata->fieldName]]]></code>
2828
<code><![CDATA[$rawData]]></code>
2929
<code><![CDATA[$rawData]]></code>
3030
</MixedAssignment>

src/Cryptography/SensitiveDataPayloadCryptographer.php

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public function __construct(
3636
*/
3737
public function encrypt(ClassMetadata $metadata, array $data): array
3838
{
39-
foreach ($metadata->properties() as $propertyMetadata) {
39+
foreach ($metadata->properties as $propertyMetadata) {
4040
if (!$propertyMetadata->isSensitiveData()) {
4141
continue;
4242
}
@@ -52,18 +52,18 @@ public function encrypt(ClassMetadata $metadata, array $data): array
5252

5353
$targetFieldName = $this->useEncryptedFieldName
5454
? $propertyMetadata->encryptedFieldName()
55-
: $propertyMetadata->fieldName();
55+
: $propertyMetadata->fieldName;
5656

5757
$data[$targetFieldName] = $this->cipher->encrypt(
5858
$cipherKey,
59-
$data[$propertyMetadata->fieldName()],
59+
$data[$propertyMetadata->fieldName],
6060
);
6161

6262
if (!$this->useEncryptedFieldName) {
6363
continue;
6464
}
6565

66-
unset($data[$propertyMetadata->fieldName()]);
66+
unset($data[$propertyMetadata->fieldName]);
6767
}
6868

6969
return $data;
@@ -76,7 +76,7 @@ public function encrypt(ClassMetadata $metadata, array $data): array
7676
*/
7777
public function decrypt(ClassMetadata $metadata, array $data): array
7878
{
79-
foreach ($metadata->properties() as $propertyMetadata) {
79+
foreach ($metadata->properties as $propertyMetadata) {
8080
if (!$propertyMetadata->isSensitiveData()) {
8181
continue;
8282
}
@@ -93,23 +93,23 @@ public function decrypt(ClassMetadata $metadata, array $data): array
9393
$rawData = $data[$propertyMetadata->encryptedFieldName()];
9494
unset($data[$propertyMetadata->encryptedFieldName()]);
9595
} elseif (!$this->useEncryptedFieldName || $this->fallbackToFieldName) {
96-
$rawData = $data[$propertyMetadata->fieldName()];
96+
$rawData = $data[$propertyMetadata->fieldName];
9797
} else {
9898
continue;
9999
}
100100

101101
if (!$cipherKey) {
102-
$data[$propertyMetadata->fieldName()] = $this->fallback($propertyMetadata, $subjectId, $rawData);
102+
$data[$propertyMetadata->fieldName] = $this->fallback($propertyMetadata, $subjectId, $rawData);
103103
continue;
104104
}
105105

106106
try {
107-
$data[$propertyMetadata->fieldName()] = $this->cipher->decrypt(
107+
$data[$propertyMetadata->fieldName] = $this->cipher->decrypt(
108108
$cipherKey,
109109
$rawData,
110110
);
111111
} catch (DecryptionFailed) {
112-
$data[$propertyMetadata->fieldName()] = $this->fallback($propertyMetadata, $subjectId, $rawData);
112+
$data[$propertyMetadata->fieldName] = $this->fallback($propertyMetadata, $subjectId, $rawData);
113113
}
114114
}
115115

@@ -123,7 +123,7 @@ private function subjectId(PropertyMetadata $propertyMetadata, ClassMetadata $me
123123
throw new NotSensitiveData($metadata->className(), $propertyMetadata->propertyName());
124124
}
125125

126-
$sensitiveDataSubjectIdName = $propertyMetadata->sensitiveDataSubjectIdName();
126+
$sensitiveDataSubjectIdName = $propertyMetadata->sensitiveDataSubjectIdName;
127127

128128
if (!$metadata->hasSubjectIdIdentifier($sensitiveDataSubjectIdName)) {
129129
throw new MissingSubjectId($metadata->className(), $propertyMetadata->propertyName());
@@ -153,7 +153,7 @@ private function fallback(PropertyMetadata $propertyMetadata, string $subjectId,
153153
$callback = $propertyMetadata->sensitiveDataFallbackCallable();
154154

155155
if (!$callback) {
156-
return $propertyMetadata->sensitiveDataFallback();
156+
return $propertyMetadata->sensitiveDataFallback;
157157
}
158158

159159
return $callback($subjectId, $rawData);

src/Metadata/AttributeMetadataFactory.php

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -256,28 +256,28 @@ private function mergeMetadata(ClassMetadata $parent, ClassMetadata $child): Cla
256256
{
257257
$properties = [];
258258

259-
foreach ($parent->properties() as $property) {
260-
$properties[$property->fieldName()] = $property;
259+
foreach ($parent->properties as $property) {
260+
$properties[$property->fieldName] = $property;
261261
}
262262

263-
foreach ($child->properties() as $property) {
264-
if (array_key_exists($property->fieldName(), $properties)) {
263+
foreach ($child->properties as $property) {
264+
if (array_key_exists($property->fieldName, $properties)) {
265265
throw DuplicatedFieldNameInMetadata::byInheritance(
266-
$property->fieldName(),
266+
$property->fieldName,
267267
$parent->className(),
268268
$child->className(),
269269
);
270270
}
271271

272-
$properties[$property->fieldName()] = $property;
272+
$properties[$property->fieldName] = $property;
273273
}
274274

275275
$mergedClassMetadata = new ClassMetadata(
276-
$parent->reflection(),
276+
$parent->reflection,
277277
array_values($properties),
278-
array_merge($parent->postHydrateCallbacks(), $child->postHydrateCallbacks()),
279-
array_merge($parent->preExtractCallbacks(), $child->preExtractCallbacks()),
280-
$child->lazy() ?? $parent->lazy(),
278+
array_merge($parent->postHydrateCallbacks, $child->postHydrateCallbacks),
279+
array_merge($parent->preExtractCallbacks, $child->preExtractCallbacks),
280+
$child->lazy ?? $parent->lazy,
281281
);
282282

283283
$this->validate($mergedClassMetadata);
@@ -314,20 +314,20 @@ private function validate(ClassMetadata $metadata): void
314314
{
315315
$subjectIds = [];
316316

317-
foreach ($metadata->properties() as $property) {
317+
foreach ($metadata->properties as $property) {
318318
if ($property->isSensitiveData() && $property->isSubjectId()) {
319319
throw new SubjectIdAndSensitiveDataConflict($metadata->className(), $property->propertyName());
320320
}
321321

322-
if ($property->isSensitiveData() && !$metadata->hasSubjectIdIdentifier($property->sensitiveDataSubjectIdName())) {
322+
if ($property->isSensitiveData() && !$metadata->hasSubjectIdIdentifier($property->sensitiveDataSubjectIdName)) {
323323
throw new MissingDataSubjectId($metadata->className());
324324
}
325325

326326
if (!$property->isSubjectId()) {
327327
continue;
328328
}
329329

330-
$subjectIdIdentifier = $property->subjectIdName();
330+
$subjectIdIdentifier = $property->subjectIdName;
331331

332332
if (array_key_exists($subjectIdIdentifier, $subjectIds)) {
333333
throw new DuplicateSubjectIdIdentifier(

src/Metadata/ClassMetadata.php

Lines changed: 9 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -26,53 +26,24 @@ final class ClassMetadata
2626
* @param list<CallbackMetadata> $preExtractCallbacks
2727
*/
2828
public function __construct(
29-
private readonly ReflectionClass $reflection,
30-
private readonly array $properties = [],
31-
private readonly array $postHydrateCallbacks = [],
32-
private readonly array $preExtractCallbacks = [],
33-
private readonly bool|null $lazy = null,
29+
public readonly ReflectionClass $reflection,
30+
public readonly array $properties = [],
31+
public readonly array $postHydrateCallbacks = [],
32+
public readonly array $preExtractCallbacks = [],
33+
public readonly bool|null $lazy = null,
3434
) {
3535
}
3636

37-
/** @return ReflectionClass<T> */
38-
public function reflection(): ReflectionClass
39-
{
40-
return $this->reflection;
41-
}
42-
4337
/** @return class-string<T> */
4438
public function className(): string
4539
{
4640
return $this->reflection->getName();
4741
}
4842

49-
/** @return list<PropertyMetadata> */
50-
public function properties(): array
51-
{
52-
return $this->properties;
53-
}
54-
55-
/** @return list<CallbackMetadata> */
56-
public function postHydrateCallbacks(): array
57-
{
58-
return $this->postHydrateCallbacks;
59-
}
60-
61-
/** @return list<CallbackMetadata> */
62-
public function preExtractCallbacks(): array
63-
{
64-
return $this->preExtractCallbacks;
65-
}
66-
67-
public function lazy(): bool|null
68-
{
69-
return $this->lazy;
70-
}
71-
7243
public function propertyForField(string $name): PropertyMetadata
7344
{
7445
foreach ($this->properties as $property) {
75-
if ($property->fieldName() === $name) {
46+
if ($property->fieldName === $name) {
7647
return $property;
7748
}
7849
}
@@ -83,7 +54,7 @@ public function propertyForField(string $name): PropertyMetadata
8354
public function hasSubjectIdIdentifier(string $subjectIdIdentifier): bool
8455
{
8556
foreach ($this->properties as $property) {
86-
if ($property->subjectIdName() === $subjectIdIdentifier) {
57+
if ($property->subjectIdName === $subjectIdIdentifier) {
8758
return true;
8859
}
8960
}
@@ -94,8 +65,8 @@ public function hasSubjectIdIdentifier(string $subjectIdIdentifier): bool
9465
public function getSubjectIdFieldName(string $subjectIdIdentifier): string
9566
{
9667
foreach ($this->properties as $property) {
97-
if ($property->subjectIdName() === $subjectIdIdentifier) {
98-
return $property->fieldName();
68+
if ($property->subjectIdName === $subjectIdIdentifier) {
69+
return $property->fieldName;
9970
}
10071
}
10172

src/Metadata/PropertyMetadata.php

Lines changed: 9 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -28,44 +28,29 @@ final class PropertyMetadata
2828

2929
/** @param (callable(string, mixed):mixed)|null $sensitiveDataFallbackCallable */
3030
public function __construct(
31-
private readonly ReflectionProperty $reflection,
32-
private readonly string $fieldName,
33-
private readonly Normalizer|null $normalizer = null,
34-
private readonly string|null $subjectIdName = null,
35-
private readonly string|null $sensitiveDataSubjectIdName = null,
36-
private readonly mixed $sensitiveDataFallback = null,
37-
private readonly mixed $sensitiveDataFallbackCallable = null,
31+
public readonly ReflectionProperty $reflection,
32+
public readonly string $fieldName,
33+
public readonly Normalizer|null $normalizer = null,
34+
public readonly string|null $subjectIdName = null,
35+
public readonly string|null $sensitiveDataSubjectIdName = null,
36+
public readonly mixed $sensitiveDataFallback = null,
37+
public readonly mixed $sensitiveDataFallbackCallable = null,
3838
) {
3939
if (str_starts_with($fieldName, self::ENCRYPTED_PREFIX)) {
4040
throw new InvalidArgumentException('fieldName must not start with !');
4141
}
4242
}
4343

44-
public function reflection(): ReflectionProperty
45-
{
46-
return $this->reflection;
47-
}
48-
4944
public function propertyName(): string
5045
{
5146
return $this->reflection->getName();
5247
}
5348

54-
public function fieldName(): string
55-
{
56-
return $this->fieldName;
57-
}
58-
5949
public function encryptedFieldName(): string
6050
{
6151
return self::ENCRYPTED_PREFIX . $this->fieldName;
6252
}
6353

64-
public function normalizer(): Normalizer|null
65-
{
66-
return $this->normalizer;
67-
}
68-
6954
public function setValue(object $object, mixed $value): void
7055
{
7156
$this->reflection->setValue($object, $value);
@@ -76,28 +61,18 @@ public function getValue(object $object): mixed
7661
return $this->reflection->getValue($object);
7762
}
7863

79-
/** @phpstan-assert-if-true !null $this->sensitiveDataSubjectIdName() */
64+
/** @phpstan-assert-if-true !null $this->sensitiveDataSubjectIdName */
8065
public function isSensitiveData(): bool
8166
{
8267
return $this->sensitiveDataSubjectIdName !== null;
8368
}
8469

85-
/** @phpstan-assert-if-true !null $this->subjectIdName() */
70+
/** @phpstan-assert-if-true !null $this->subjectIdName */
8671
public function isSubjectId(): bool
8772
{
8873
return $this->subjectIdName !== null;
8974
}
9075

91-
public function subjectIdName(): string|null
92-
{
93-
return $this->subjectIdName;
94-
}
95-
96-
public function sensitiveDataFallback(): mixed
97-
{
98-
return $this->sensitiveDataFallback;
99-
}
100-
10176
/** @return (Closure(string, mixed):mixed)|null */
10277
public function sensitiveDataFallbackCallable(): Closure|null
10378
{
@@ -108,11 +83,6 @@ public function sensitiveDataFallbackCallable(): Closure|null
10883
return null;
10984
}
11085

111-
public function sensitiveDataSubjectIdName(): string|null
112-
{
113-
return $this->sensitiveDataSubjectIdName;
114-
}
115-
11686
/** @return serialized */
11787
public function __serialize(): array
11888
{

0 commit comments

Comments
 (0)