chore: fix cgl issues flagged by phpstan

This commit is contained in:
Björn Fromme
2026-07-10 14:33:09 +02:00
parent f47142c465
commit 2f8701c2e0
6 changed files with 50 additions and 111 deletions
+34 -110
View File
@@ -10,41 +10,11 @@ use Faker\Generator;
class BpnXmlAnonymizer
{
private const SAFE_EMAIL_DOMAIN = 'example.com';
private const SALUTATIONS = ['Herr', 'Frau', 'Divers'];
private const GENDERS = ['M', 'W', 'D'];
private const COUNTRY_CODES = ['AT', 'BE', 'CH', 'DE', 'DK', 'ES', 'FR', 'IT', 'NL', 'NO', 'PL', 'SE'];
private Generator $faker;
/**
* @var array<string, array{
* firstName: string,
* lastName: string,
* salutation: string,
* title: string,
* gender: string,
* nationality: string,
* birthDate: string,
* street: string,
* postalCode: string,
* city: string,
* district: string,
* country: string,
* email: string,
* mobilePhone: string,
* phone: string,
* newsletter: string,
* iban: string,
* userName: string,
* requestKey: string,
* requestPassword: string,
* personId: string,
* addressId: string,
* customerId: string,
* height: string,
* weight: string,
* shoeSize: string
* }>
* @var array<string, array<string, string>>
*/
private array $identities = [];
@@ -141,24 +111,9 @@ class BpnXmlAnonymizer
}
}
$currentNode = $personNode;
while ($currentNode instanceof \DOMElement) {
foreach (['personid', 'idperson', 'idadresseperson'] as $tagName) {
$child = $this->getDirectChild($currentNode, $tagName);
if (null !== $child) {
$value = trim($child->textContent);
if ('' !== $value) {
return $value;
}
}
}
$parentNode = $currentNode->parentNode;
if (false === $parentNode instanceof \DOMElement) {
break;
}
$currentNode = $parentNode;
$personIdValue = $this->findPersonIdInAncestors($personNode);
if (null !== $personIdValue) {
return $personIdValue;
}
$fingerprintKey = $this->buildFingerprintKey($personNode);
@@ -232,34 +187,7 @@ class BpnXmlAnonymizer
}
/**
* @return array{
* firstName: string,
* lastName: string,
* salutation: string,
* title: string,
* gender: string,
* nationality: string,
* birthDate: string,
* street: string,
* postalCode: string,
* city: string,
* district: string,
* country: string,
* email: string,
* mobilePhone: string,
* phone: string,
* newsletter: string,
* iban: string,
* userName: string,
* requestKey: string,
* requestPassword: string,
* personId: string,
* addressId: string,
* customerId: string,
* height: string,
* weight: string,
* shoeSize: string
* }
* @return array<string, string>
*/
private function getOrCreateIdentity(string $personId): array
{
@@ -330,7 +258,7 @@ class BpnXmlAnonymizer
}
/**
* @param array{firstName: string, lastName: string, street: string, postalCode: string, city: string, email: string, mobilePhone: string} $identity
* @param array<string, string> $identity
*/
private function replaceNameFields(\DOMElement $personNode, array $identity): void
{
@@ -358,7 +286,7 @@ class BpnXmlAnonymizer
}
/**
* @param array{firstName: string, lastName: string, street: string, postalCode: string, city: string, email: string, mobilePhone: string} $identity
* @param array<string, string> $identity
*/
private function replaceEmailFields(\DOMElement $personNode, array $identity): void
{
@@ -396,7 +324,7 @@ class BpnXmlAnonymizer
}
/**
* @param array{firstName: string, lastName: string, street: string, postalCode: string, city: string, email: string, mobilePhone: string} $identity
* @param array<string, string> $identity
*/
private function replaceAddressFields(\DOMElement $personNode, array $identity): void
{
@@ -436,6 +364,9 @@ class BpnXmlAnonymizer
}
}
/**
* @param array<string, string> $identity
*/
private function replaceDemographicFields(\DOMElement $personNode, array $identity): void
{
$salutationNode = $this->getDirectChild($personNode, 'anrede');
@@ -479,6 +410,9 @@ class BpnXmlAnonymizer
}
}
/**
* @param array<string, string> $identity
*/
private function replaceFinancialFields(\DOMElement $personNode, array $identity): void
{
$bankNode = $this->getDirectChild($personNode, 'bankverbindung');
@@ -492,6 +426,9 @@ class BpnXmlAnonymizer
}
}
/**
* @param array<string, string> $identity
*/
private function replaceMetadataFields(\DOMElement $personNode, array $identity): void
{
if (true === $personNode->hasAttribute('id')) {
@@ -503,6 +440,9 @@ class BpnXmlAnonymizer
}
}
/**
* @param array<string, string> $identity
*/
private function replaceRequestFields(\DOMElement $personNode, array $identity): void
{
$userNode = $this->getDirectChild($personNode, 'user');
@@ -544,23 +484,24 @@ class BpnXmlAnonymizer
$node->setAttribute($attributeName, $replacement);
}
private function findAncestorDirectChild(\DOMElement $personNode, string $ancestorTagName, string $childTagName): ?\DOMElement
private function findPersonIdInAncestors(\DOMElement $node): ?string
{
$currentNode = $personNode;
while ($currentNode instanceof \DOMElement) {
if ($ancestorTagName === $currentNode->tagName) {
return $this->getDirectChild($currentNode, $childTagName);
foreach (['personid', 'idperson', 'idadresseperson'] as $tagName) {
$child = $this->getDirectChild($node, $tagName);
if (null !== $child) {
$value = trim($child->textContent);
if ('' !== $value) {
return $value;
}
}
$parentNode = $currentNode->parentNode;
if (false === $parentNode instanceof \DOMElement) {
break;
}
$currentNode = $parentNode;
}
return null;
$parent = $node->parentNode;
if (false === $parent instanceof \DOMElement) {
return null;
}
return $this->findPersonIdInAncestors($parent);
}
private function getDirectChild(\DOMElement $parent, string $name): ?\DOMElement
@@ -578,21 +519,4 @@ class BpnXmlAnonymizer
return null;
}
private function slug(string $value): string
{
$normalizedValue = iconv('UTF-8', 'ASCII//TRANSLIT//IGNORE', $value);
if (false === $normalizedValue) {
$normalizedValue = $value;
}
$normalizedValue = strtolower($normalizedValue);
$normalizedValue = preg_replace('/[^a-z0-9]+/', '.', $normalizedValue);
$normalizedValue = trim((string) $normalizedValue, '.');
if ('' === $normalizedValue) {
return 'user';
}
return $normalizedValue;
}
}
+6
View File
@@ -48,6 +48,9 @@ class DatabaseAnonymizer
$this->identitySequence = 0;
}
/**
* @return array{users: int, newsletterConsents: int, newsletterOptInRequests: int, bookingEditDrafts: int}
*/
public function anonymizeAll(int $batchSize = self::DEFAULT_BATCH_SIZE): array
{
if ($batchSize < 1) {
@@ -405,6 +408,9 @@ class DatabaseAnonymizer
return null !== $this->stringOrNull($value);
}
/**
* @param array<string, string> $identity
*/
private function placeholderText(array $identity, string $suffix): string
{
return sprintf('%s-%s', $identity['lastName'], $suffix);