Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions baseline.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@
<code><![CDATA[$rawData]]></code>
</MixedArgument>
<MixedAssignment>
<code><![CDATA[$data[$propertyMetadata->fieldName()]]]></code>
<code><![CDATA[$data[$propertyMetadata->fieldName()]]]></code>
<code><![CDATA[$data[$propertyMetadata->fieldName()]]]></code>
<code><![CDATA[$data[$propertyMetadata->fieldName]]]></code>
<code><![CDATA[$data[$propertyMetadata->fieldName]]]></code>
<code><![CDATA[$data[$propertyMetadata->fieldName]]]></code>
<code><![CDATA[$rawData]]></code>
<code><![CDATA[$rawData]]></code>
</MixedAssignment>
Expand Down
22 changes: 11 additions & 11 deletions src/Cryptography/SensitiveDataPayloadCryptographer.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public function __construct(
*/
public function encrypt(ClassMetadata $metadata, array $data): array
{
foreach ($metadata->properties() as $propertyMetadata) {
foreach ($metadata->properties as $propertyMetadata) {
if (!$propertyMetadata->isSensitiveData()) {
continue;
}
Expand All @@ -52,18 +52,18 @@ public function encrypt(ClassMetadata $metadata, array $data): array

$targetFieldName = $this->useEncryptedFieldName
? $propertyMetadata->encryptedFieldName()
: $propertyMetadata->fieldName();
: $propertyMetadata->fieldName;

$data[$targetFieldName] = $this->cipher->encrypt(
$cipherKey,
$data[$propertyMetadata->fieldName()],
$data[$propertyMetadata->fieldName],
);

if (!$this->useEncryptedFieldName) {
continue;
}

unset($data[$propertyMetadata->fieldName()]);
unset($data[$propertyMetadata->fieldName]);
}

return $data;
Expand All @@ -76,7 +76,7 @@ public function encrypt(ClassMetadata $metadata, array $data): array
*/
public function decrypt(ClassMetadata $metadata, array $data): array
{
foreach ($metadata->properties() as $propertyMetadata) {
foreach ($metadata->properties as $propertyMetadata) {
if (!$propertyMetadata->isSensitiveData()) {
continue;
}
Expand All @@ -93,23 +93,23 @@ public function decrypt(ClassMetadata $metadata, array $data): array
$rawData = $data[$propertyMetadata->encryptedFieldName()];
unset($data[$propertyMetadata->encryptedFieldName()]);
} elseif (!$this->useEncryptedFieldName || $this->fallbackToFieldName) {
$rawData = $data[$propertyMetadata->fieldName()];
$rawData = $data[$propertyMetadata->fieldName];
} else {
continue;
}

if (!$cipherKey) {
$data[$propertyMetadata->fieldName()] = $this->fallback($propertyMetadata, $subjectId, $rawData);
$data[$propertyMetadata->fieldName] = $this->fallback($propertyMetadata, $subjectId, $rawData);
continue;
}

try {
$data[$propertyMetadata->fieldName()] = $this->cipher->decrypt(
$data[$propertyMetadata->fieldName] = $this->cipher->decrypt(
$cipherKey,
$rawData,
);
} catch (DecryptionFailed) {
$data[$propertyMetadata->fieldName()] = $this->fallback($propertyMetadata, $subjectId, $rawData);
$data[$propertyMetadata->fieldName] = $this->fallback($propertyMetadata, $subjectId, $rawData);
}
}

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

$sensitiveDataSubjectIdName = $propertyMetadata->sensitiveDataSubjectIdName();
$sensitiveDataSubjectIdName = $propertyMetadata->sensitiveDataSubjectIdName;

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

if (!$callback) {
return $propertyMetadata->sensitiveDataFallback();
return $propertyMetadata->sensitiveDataFallback;
}

return $callback($subjectId, $rawData);
Expand Down
26 changes: 13 additions & 13 deletions src/Metadata/AttributeMetadataFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -256,28 +256,28 @@ private function mergeMetadata(ClassMetadata $parent, ClassMetadata $child): Cla
{
$properties = [];

foreach ($parent->properties() as $property) {
$properties[$property->fieldName()] = $property;
foreach ($parent->properties as $property) {
$properties[$property->fieldName] = $property;
}

foreach ($child->properties() as $property) {
if (array_key_exists($property->fieldName(), $properties)) {
foreach ($child->properties as $property) {
if (array_key_exists($property->fieldName, $properties)) {
throw DuplicatedFieldNameInMetadata::byInheritance(
$property->fieldName(),
$property->fieldName,
$parent->className(),
$child->className(),
);
}

$properties[$property->fieldName()] = $property;
$properties[$property->fieldName] = $property;
}

$mergedClassMetadata = new ClassMetadata(
$parent->reflection(),
$parent->reflection,
array_values($properties),
array_merge($parent->postHydrateCallbacks(), $child->postHydrateCallbacks()),
array_merge($parent->preExtractCallbacks(), $child->preExtractCallbacks()),
$child->lazy() ?? $parent->lazy(),
array_merge($parent->postHydrateCallbacks, $child->postHydrateCallbacks),
array_merge($parent->preExtractCallbacks, $child->preExtractCallbacks),
$child->lazy ?? $parent->lazy,
);

$this->validate($mergedClassMetadata);
Expand Down Expand Up @@ -314,20 +314,20 @@ private function validate(ClassMetadata $metadata): void
{
$subjectIds = [];

foreach ($metadata->properties() as $property) {
foreach ($metadata->properties as $property) {
if ($property->isSensitiveData() && $property->isSubjectId()) {
throw new SubjectIdAndSensitiveDataConflict($metadata->className(), $property->propertyName());
}

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

if (!$property->isSubjectId()) {
continue;
}

$subjectIdIdentifier = $property->subjectIdName();
$subjectIdIdentifier = $property->subjectIdName;

if (array_key_exists($subjectIdIdentifier, $subjectIds)) {
throw new DuplicateSubjectIdIdentifier(
Expand Down
47 changes: 9 additions & 38 deletions src/Metadata/ClassMetadata.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,53 +26,24 @@ final class ClassMetadata
* @param list<CallbackMetadata> $preExtractCallbacks
*/
public function __construct(
private readonly ReflectionClass $reflection,
private readonly array $properties = [],
private readonly array $postHydrateCallbacks = [],
private readonly array $preExtractCallbacks = [],
private readonly bool|null $lazy = null,
public readonly ReflectionClass $reflection,
public readonly array $properties = [],
public readonly array $postHydrateCallbacks = [],
public readonly array $preExtractCallbacks = [],
public readonly bool|null $lazy = null,
) {
}

/** @return ReflectionClass<T> */
public function reflection(): ReflectionClass
{
return $this->reflection;
}

/** @return class-string<T> */
public function className(): string
{
return $this->reflection->getName();
}

/** @return list<PropertyMetadata> */
public function properties(): array
{
return $this->properties;
}

/** @return list<CallbackMetadata> */
public function postHydrateCallbacks(): array
{
return $this->postHydrateCallbacks;
}

/** @return list<CallbackMetadata> */
public function preExtractCallbacks(): array
{
return $this->preExtractCallbacks;
}

public function lazy(): bool|null
{
return $this->lazy;
}

public function propertyForField(string $name): PropertyMetadata
{
foreach ($this->properties as $property) {
if ($property->fieldName() === $name) {
if ($property->fieldName === $name) {
return $property;
}
}
Expand All @@ -83,7 +54,7 @@ public function propertyForField(string $name): PropertyMetadata
public function hasSubjectIdIdentifier(string $subjectIdIdentifier): bool
{
foreach ($this->properties as $property) {
if ($property->subjectIdName() === $subjectIdIdentifier) {
if ($property->subjectIdName === $subjectIdIdentifier) {
return true;
}
}
Expand All @@ -94,8 +65,8 @@ public function hasSubjectIdIdentifier(string $subjectIdIdentifier): bool
public function getSubjectIdFieldName(string $subjectIdIdentifier): string
{
foreach ($this->properties as $property) {
if ($property->subjectIdName() === $subjectIdIdentifier) {
return $property->fieldName();
if ($property->subjectIdName === $subjectIdIdentifier) {
return $property->fieldName;
}
}

Expand Down
48 changes: 9 additions & 39 deletions src/Metadata/PropertyMetadata.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,44 +28,29 @@ final class PropertyMetadata

/** @param (callable(string, mixed):mixed)|null $sensitiveDataFallbackCallable */
public function __construct(
private readonly ReflectionProperty $reflection,
private readonly string $fieldName,
private readonly Normalizer|null $normalizer = null,
private readonly string|null $subjectIdName = null,
private readonly string|null $sensitiveDataSubjectIdName = null,
private readonly mixed $sensitiveDataFallback = null,
private readonly mixed $sensitiveDataFallbackCallable = null,
public readonly ReflectionProperty $reflection,
public readonly string $fieldName,
public readonly Normalizer|null $normalizer = null,
public readonly string|null $subjectIdName = null,
public readonly string|null $sensitiveDataSubjectIdName = null,
public readonly mixed $sensitiveDataFallback = null,
public readonly mixed $sensitiveDataFallbackCallable = null,
) {
if (str_starts_with($fieldName, self::ENCRYPTED_PREFIX)) {
throw new InvalidArgumentException('fieldName must not start with !');
}
}

public function reflection(): ReflectionProperty
{
return $this->reflection;
}

public function propertyName(): string
{
return $this->reflection->getName();
}

public function fieldName(): string
{
return $this->fieldName;
}

public function encryptedFieldName(): string
{
return self::ENCRYPTED_PREFIX . $this->fieldName;
}

public function normalizer(): Normalizer|null
{
return $this->normalizer;
}

public function setValue(object $object, mixed $value): void
{
$this->reflection->setValue($object, $value);
Expand All @@ -76,28 +61,18 @@ public function getValue(object $object): mixed
return $this->reflection->getValue($object);
}

/** @phpstan-assert-if-true !null $this->sensitiveDataSubjectIdName() */
/** @phpstan-assert-if-true !null $this->sensitiveDataSubjectIdName */
public function isSensitiveData(): bool
{
return $this->sensitiveDataSubjectIdName !== null;
}

/** @phpstan-assert-if-true !null $this->subjectIdName() */
/** @phpstan-assert-if-true !null $this->subjectIdName */
public function isSubjectId(): bool
{
return $this->subjectIdName !== null;
}

public function subjectIdName(): string|null
{
return $this->subjectIdName;
}

public function sensitiveDataFallback(): mixed
{
return $this->sensitiveDataFallback;
}

/** @return (Closure(string, mixed):mixed)|null */
public function sensitiveDataFallbackCallable(): Closure|null
{
Expand All @@ -108,11 +83,6 @@ public function sensitiveDataFallbackCallable(): Closure|null
return null;
}

public function sensitiveDataSubjectIdName(): string|null
{
return $this->sensitiveDataSubjectIdName;
}

/** @return serialized */
public function __serialize(): array
{
Expand Down
Loading
Loading