fix: SA: Fix "Call to sprintf" rule.

SA stands for Static Analysis.
This commit is contained in:
Pol Dellaiera
2021-11-16 11:52:52 +01:00
parent f8aeb08594
commit a3eb23478a
4 changed files with 62 additions and 116 deletions

View File

@@ -1,22 +1,10 @@
<?php
/*
* Copyright (C) 2018 Champs Libres Cooperative <info@champs-libres.coop>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
declare(strict_types=1);
namespace Chill\MainBundle\Security\UserProvider;
use Doctrine\ORM\NoResultException;
use Symfony\Component\Security\Core\User\UserProviderInterface;
use Symfony\Component\Security\Core\User\UserInterface;
use Doctrine\ORM\EntityManagerInterface;
@@ -24,25 +12,15 @@ use Chill\MainBundle\Entity\User;
use Symfony\Component\Security\Core\Exception\UsernameNotFoundException;
use Symfony\Component\Security\Core\Exception\UnsupportedUserException;
/**
*
*
* @author Julien Fastré <julien.fastre@champs-libres.coop>
*/
class UserProvider implements UserProviderInterface
{
/**
*
* @var EntityManagerInterface
*/
protected $em;
protected EntityManagerInterface $em;
public function __construct(EntityManagerInterface $em)
{
$this->em = $em;
}
public function loadUserByUsername($username): UserInterface
{
try {
@@ -50,14 +28,18 @@ class UserProvider implements UserProviderInterface
"SELECT u FROM %s u "
. "WHERE u.usernameCanonical = UNACCENT(LOWER(:pattern)) "
. "OR "
. "u.emailCanonical = UNACCENT(LOWER(:pattern))",
. "u.emailCanonical = UNACCENT(LOWER(:pattern))",
User::class))
->setParameter('pattern', $username)
->getSingleResult();
} catch (\Doctrine\ORM\NoResultException $e) {
throw new UsernameNotFoundException(sprintf('Bad credentials.', $username));
} catch (NoResultException $e) {
throw new UsernameNotFoundException(
sprintf('Bad credentials.'),
0,
$e
);
}
return $user;
}
@@ -66,13 +48,13 @@ class UserProvider implements UserProviderInterface
if (!$user instanceof User) {
throw new UnsupportedUserException("Unsupported user class: cannot reload this user");
}
$reloadedUser = $this->em->getRepository(User::class)->find($user->getId());
if (NULL === $reloadedUser) {
throw new UsernameNotFoundException(sprintf('User with ID "%s" could not be reloaded.', $user->getId()));
}
return $reloadedUser;
}