Fix SonarQube issues: cognitive complexity, duplicate literals, PHPDoc, regex syntax
- AdminController: extract DQL_STATUS_PAID constant (3 occurrences) - AdminController: remove misplaced @return PHPDoc on export(), add it on computeEventStats() - HomeController: extract handleInvitationRegistration() to reduce complexity and returns (17→~10, 4→3 returns) - MeilisearchConsistencyCommand: extract userToDocument() to reduce checkUserIndex complexity (18→~10) - TranslateCommand: use \w instead of [a-zA-Z0-9_] in regex - TranslateCommand: add comment in empty catch block Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -250,29 +250,36 @@ class MeilisearchConsistencyCommand extends Command
|
||||
}
|
||||
|
||||
return $this->diffAndReport($index, $meiliIds, $dbIds, $fix, $io, function (array $missing) use ($usersById, $isOrganizer, $index): void {
|
||||
$docs = [];
|
||||
foreach ($missing as $id) {
|
||||
if (isset($usersById[$id])) {
|
||||
$u = $usersById[$id];
|
||||
$doc = [
|
||||
'id' => $u->getId(),
|
||||
'firstName' => $u->getFirstName(),
|
||||
'lastName' => $u->getLastName(),
|
||||
'email' => $u->getEmail(),
|
||||
];
|
||||
if ($isOrganizer) {
|
||||
$doc['companyName'] = $u->getCompanyName();
|
||||
$doc['siret'] = $u->getSiret();
|
||||
$doc['city'] = $u->getCity();
|
||||
} else {
|
||||
$doc['createdAt'] = $u->getCreatedAt()->format('d/m/Y');
|
||||
}
|
||||
$docs[] = $doc;
|
||||
}
|
||||
}
|
||||
$docs = array_filter(array_map(
|
||||
fn (int|string $id) => isset($usersById[$id]) ? $this->userToDocument($usersById[$id], $isOrganizer) : null,
|
||||
$missing,
|
||||
));
|
||||
if ([] !== $docs) {
|
||||
$this->meilisearch->addDocuments($index, $docs);
|
||||
$this->meilisearch->addDocuments($index, array_values($docs));
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<string, mixed>
|
||||
*/
|
||||
private function userToDocument(User $u, bool $isOrganizer): array
|
||||
{
|
||||
$doc = [
|
||||
'id' => $u->getId(),
|
||||
'firstName' => $u->getFirstName(),
|
||||
'lastName' => $u->getLastName(),
|
||||
'email' => $u->getEmail(),
|
||||
];
|
||||
|
||||
if ($isOrganizer) {
|
||||
$doc['companyName'] = $u->getCompanyName();
|
||||
$doc['siret'] = $u->getSiret();
|
||||
$doc['city'] = $u->getCity();
|
||||
} else {
|
||||
$doc['createdAt'] = $u->getCreatedAt()->format('d/m/Y');
|
||||
}
|
||||
|
||||
return $doc;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -250,7 +250,7 @@ class TranslateCommand extends Command
|
||||
|
||||
private function preservePlaceholders(string $original, string $translated): string
|
||||
{
|
||||
preg_match_all('/%[a-zA-Z0-9_]+%/', $original, $matches);
|
||||
preg_match_all('/%\w+%/', $original, $matches);
|
||||
if (empty($matches[0])) {
|
||||
return $translated;
|
||||
}
|
||||
@@ -276,7 +276,7 @@ class TranslateCommand extends Command
|
||||
if (200 === $response->getStatusCode()) {
|
||||
return true;
|
||||
}
|
||||
} catch (\Throwable) {
|
||||
} catch (\Throwable) { // @phpstan-ignore-line API not ready yet
|
||||
}
|
||||
sleep(2);
|
||||
}
|
||||
|
||||
@@ -1162,9 +1162,6 @@ class AccountController extends AbstractController
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array{totalHT: int, totalSold: int, billetStats: array<int, array{name: string, sold: int, revenue: int}>}
|
||||
*/
|
||||
#[Route('/mon-compte/export/{year}/{month}', name: 'app_account_export', requirements: ['year' => '\d{4}', 'month' => '\d{1,2}'], methods: ['GET'])]
|
||||
public function export(int $year, int $month, ExportService $exportService): Response
|
||||
{
|
||||
@@ -1201,6 +1198,11 @@ class AccountController extends AbstractController
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param list<BilletBuyer> $paidOrders
|
||||
*
|
||||
* @return array{totalHT: int, totalSold: int, billetStats: array<int, array{name: string, sold: int, revenue: int}>}
|
||||
*/
|
||||
private function computeEventStats(array $paidOrders): array
|
||||
{
|
||||
$totalHT = 0;
|
||||
|
||||
@@ -26,6 +26,8 @@ use Symfony\Component\Validator\Validator\ValidatorInterface;
|
||||
#[IsGranted('ROLE_ROOT')]
|
||||
class AdminController extends AbstractController
|
||||
{
|
||||
private const DQL_STATUS_PAID = 'o.status = :paid';
|
||||
|
||||
#[Route('', name: 'app_admin_dashboard')]
|
||||
public function dashboard(EntityManagerInterface $em): Response
|
||||
{
|
||||
@@ -35,7 +37,7 @@ class AdminController extends AbstractController
|
||||
$totalCA = (int) ($em->createQueryBuilder()
|
||||
->select('SUM(o.totalHT)')
|
||||
->from(BilletBuyer::class, 'o')
|
||||
->where('o.status = :paid')
|
||||
->where(self::DQL_STATUS_PAID)
|
||||
->setParameter('paid', BilletBuyer::STATUS_PAID)
|
||||
->getQuery()
|
||||
->getSingleScalarResult() ?? 0);
|
||||
@@ -50,7 +52,7 @@ class AdminController extends AbstractController
|
||||
->from(BilletBuyer::class, 'o')
|
||||
->join('o.event', 'e')
|
||||
->join('e.account', 'a')
|
||||
->where('o.status = :paid')
|
||||
->where(self::DQL_STATUS_PAID)
|
||||
->setParameter('paid', BilletBuyer::STATUS_PAID)
|
||||
->getQuery()
|
||||
->getResult();
|
||||
@@ -521,7 +523,7 @@ class AdminController extends AbstractController
|
||||
$totalCA = $em->createQueryBuilder()
|
||||
->select('SUM(o.totalHT)')
|
||||
->from(BilletBuyer::class, 'o')
|
||||
->where('o.status = :paid')
|
||||
->where(self::DQL_STATUS_PAID)
|
||||
->setParameter('paid', BilletBuyer::STATUS_PAID)
|
||||
->getQuery()
|
||||
->getSingleScalarResult() ?? 0;
|
||||
|
||||
@@ -393,50 +393,50 @@ class HomeController extends AbstractController
|
||||
}
|
||||
|
||||
if ($request->isMethod('POST')) {
|
||||
$firstName = trim($request->request->getString('first_name'));
|
||||
$lastName = trim($request->request->getString('last_name'));
|
||||
$password = $request->request->getString('password');
|
||||
$siret = trim($request->request->getString('siret'));
|
||||
$address = trim($request->request->getString('address'));
|
||||
$postalCode = trim($request->request->getString('postal_code'));
|
||||
$city = trim($request->request->getString('city'));
|
||||
$phone = trim($request->request->getString('phone'));
|
||||
|
||||
if ('' === $firstName || '' === $lastName || '' === $password) {
|
||||
$this->addFlash('error', 'Tous les champs obligatoires doivent etre remplis.');
|
||||
|
||||
return $this->redirectToRoute('app_invitation_register', ['token' => $token]);
|
||||
}
|
||||
|
||||
$user = new User();
|
||||
$user->setEmail($invitation->getEmail());
|
||||
$user->setFirstName($firstName);
|
||||
$user->setLastName($lastName);
|
||||
$user->setPassword($passwordHasher->hashPassword($user, $password));
|
||||
$user->setRoles(['ROLE_ORGANIZER']);
|
||||
$user->setCompanyName($invitation->getCompanyName());
|
||||
$user->setOffer($invitation->getOffer());
|
||||
$user->setCommissionRate($invitation->getCommissionRate());
|
||||
$user->setIsApproved(true);
|
||||
$user->setIsVerified(true);
|
||||
$user->setSiret($siret ?: null);
|
||||
$user->setAddress($address ?: null);
|
||||
$user->setPostalCode($postalCode ?: null);
|
||||
$user->setCity($city ?: null);
|
||||
$user->setPhone($phone ?: null);
|
||||
|
||||
$em->persist($user);
|
||||
$em->flush();
|
||||
|
||||
$security->login($user, 'form_login');
|
||||
|
||||
$this->addFlash('success', 'Bienvenue ! Votre compte organisateur est actif.');
|
||||
|
||||
return $this->redirectToRoute('app_account');
|
||||
return $this->handleInvitationRegistration($invitation, $request, $em, $passwordHasher, $security);
|
||||
}
|
||||
|
||||
return $this->render('home/invitation_register.html.twig', [
|
||||
'invitation' => $invitation,
|
||||
]);
|
||||
}
|
||||
|
||||
private function handleInvitationRegistration(OrganizerInvitation $invitation, Request $request, EntityManagerInterface $em, UserPasswordHasherInterface $passwordHasher, Security $security): Response
|
||||
{
|
||||
$firstName = trim($request->request->getString('first_name'));
|
||||
$lastName = trim($request->request->getString('last_name'));
|
||||
$password = $request->request->getString('password');
|
||||
|
||||
if ('' === $firstName || '' === $lastName || '' === $password) {
|
||||
$this->addFlash('error', 'Tous les champs obligatoires doivent etre remplis.');
|
||||
|
||||
return $this->redirectToRoute('app_invitation_register', ['token' => $invitation->getToken()]);
|
||||
}
|
||||
|
||||
$user = new User();
|
||||
$user->setEmail($invitation->getEmail());
|
||||
$user->setFirstName($firstName);
|
||||
$user->setLastName($lastName);
|
||||
$user->setPassword($passwordHasher->hashPassword($user, $password));
|
||||
$user->setRoles(['ROLE_ORGANIZER']);
|
||||
$user->setCompanyName($invitation->getCompanyName());
|
||||
$user->setOffer($invitation->getOffer());
|
||||
$user->setCommissionRate($invitation->getCommissionRate());
|
||||
$user->setIsApproved(true);
|
||||
$user->setIsVerified(true);
|
||||
$user->setSiret(trim($request->request->getString('siret')) ?: null);
|
||||
$user->setAddress(trim($request->request->getString('address')) ?: null);
|
||||
$user->setPostalCode(trim($request->request->getString('postal_code')) ?: null);
|
||||
$user->setCity(trim($request->request->getString('city')) ?: null);
|
||||
$user->setPhone(trim($request->request->getString('phone')) ?: null);
|
||||
|
||||
$em->persist($user);
|
||||
$em->flush();
|
||||
|
||||
$security->login($user, 'form_login');
|
||||
|
||||
$this->addFlash('success', 'Bienvenue ! Votre compte organisateur est actif.');
|
||||
|
||||
return $this->redirectToRoute('app_account');
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user