Skip to content

Commit

Permalink
improved lastname anonymization
Browse files Browse the repository at this point in the history
fixes
  • Loading branch information
kpodemski committed Dec 9, 2022
1 parent 0718b6a commit 919e51a
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 15 deletions.
34 changes: 26 additions & 8 deletions controllers/front/ListComments.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,17 +64,17 @@ public function display()
$productComment['content'] = htmlentities($productComment['content']);
$productComment['date_add'] = $dateFormatter->format($dateAdd);

if ($isLastNameAnonymous && isset($productComment['lastname'])) {
$productComment['lastname'] = substr($productComment['lastname'], 0, 1) . '.';
}
// The customer has firstname and lastname, for guest we only have customer_name field
$productComment['customer_name'] = !empty($productComment['customer_name'])
? $productComment['customer_name']
: $productComment['firstname'] . ' ' . $productComment['lastname'];

// if registered customer : return customer first and last name instead of using customer_name
if (!empty($productComment['lastname'])) {
$productComment['customer_name'] = htmlentities($productComment['firstname'] . ' ' . $productComment['lastname']);
} else {
$productComment['customer_name'] = htmlentities($productComment['customer_name']);
if ($isLastNameAnonymous) {
$productComment['customer_name'] = $this->anonymizeName($productComment['customer_name']);
}

$product['customer_name'] = htmlentities($productComment['customer_name']);

$usefulness = $productCommentRepository->getProductCommentUsefulness($productComment['id_product_comment']);
$productComment = array_merge($productComment, $usefulness);
if (empty($productComment['customer_name']) && !isset($productComment['firstname']) && !isset($productComment['lastname'])) {
Expand All @@ -91,4 +91,22 @@ public function display()
)
);
}

/**
* Anonymize the user's last name. Display only initials, e.g. John D.
*
* @param string $name
*/
private function anonymizeName(string $name): string
{
$parts = explode(' ', $name);
$firstName = $parts[0];
$lastName = count($parts) > 1 ? array_pop($parts) : '';
$name = $firstName;
if (!empty($lastName)) {
$name .= ' ' . mb_substr($lastName, 0, 1, 'UTF-8') . '.';
}

return $name;
}
}
7 changes: 0 additions & 7 deletions tests/phpstan/phpstan-latest.neon
Original file line number Diff line number Diff line change
@@ -1,9 +1,2 @@
includes:
- %currentWorkingDirectory%/tests/phpstan/phpstan.neon

parameters:
ignoreErrors:
- '#Access to an undefined property Cookie::\$id_customer.#'
- '#Access to an undefined property Cookie::\$id_guest.#'
- '#Access to an undefined property HelperList::\$list_id.#'
- '#Access to an undefined property HelperList::\$shopLinkType.#'

0 comments on commit 919e51a

Please sign in to comment.