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
No known key found for this signature in database
GPG Key ID: D476DFE9C67467CA
4 changed files with 62 additions and 116 deletions

View File

@ -235,11 +235,6 @@ parameters:
count: 1 count: 1
path: src/Bundle/ChillCustomFieldsBundle/Form/CustomFieldType.php path: src/Bundle/ChillCustomFieldsBundle/Form/CustomFieldType.php
-
message: "#^Call to sprintf contains 0 placeholders, 1 value given\\.$#"
count: 1
path: src/Bundle/ChillCustomFieldsBundle/Form/DataTransformer/CustomFieldsGroupToIdTransformer.php
- -
message: "#^Access to an undefined property Chill\\\\CustomFieldsBundle\\\\Form\\\\DataTransformer\\\\JsonCustomFieldToArrayTransformer\\:\\:\\$customField\\.$#" message: "#^Access to an undefined property Chill\\\\CustomFieldsBundle\\\\Form\\\\DataTransformer\\\\JsonCustomFieldToArrayTransformer\\:\\:\\$customField\\.$#"
count: 3 count: 3
@ -385,11 +380,6 @@ parameters:
count: 2 count: 2
path: src/Bundle/ChillMainBundle/CRUD/Controller/ApiController.php path: src/Bundle/ChillMainBundle/CRUD/Controller/ApiController.php
-
message: "#^Call to sprintf contains 2 placeholders, 0 values given\\.$#"
count: 1
path: src/Bundle/ChillMainBundle/CRUD/Controller/CRUDController.php
- -
message: "#^Construct empty\\(\\) is not allowed\\. Use more strict comparison\\.$#" message: "#^Construct empty\\(\\) is not allowed\\. Use more strict comparison\\.$#"
count: 1 count: 1
@ -405,11 +395,6 @@ parameters:
count: 1 count: 1
path: src/Bundle/ChillMainBundle/CRUD/Controller/CRUDController.php path: src/Bundle/ChillMainBundle/CRUD/Controller/CRUDController.php
-
message: "#^Method Symfony\\\\Bundle\\\\FrameworkBundle\\\\Controller\\\\AbstractController\\:\\:createNotFoundException\\(\\) invoked with 3 parameters, 0\\-2 required\\.$#"
count: 1
path: src/Bundle/ChillMainBundle/CRUD/Controller/CRUDController.php
- -
message: "#^Parameter \\$scope of method Chill\\\\MainBundle\\\\CRUD\\\\Controller\\\\CRUDController\\:\\:getReachableCenters\\(\\) has invalid type Chill\\\\MainBundle\\\\CRUD\\\\Controller\\\\Scope\\.$#" message: "#^Parameter \\$scope of method Chill\\\\MainBundle\\\\CRUD\\\\Controller\\\\CRUDController\\:\\:getReachableCenters\\(\\) has invalid type Chill\\\\MainBundle\\\\CRUD\\\\Controller\\\\Scope\\.$#"
count: 1 count: 1
@ -795,11 +780,6 @@ parameters:
count: 1 count: 1
path: src/Bundle/ChillMainBundle/Security/PasswordRecover/TokenManager.php path: src/Bundle/ChillMainBundle/Security/PasswordRecover/TokenManager.php
-
message: "#^Call to sprintf contains 0 placeholders, 1 value given\\.$#"
count: 1
path: src/Bundle/ChillMainBundle/Security/UserProvider/UserProvider.php
- -
message: "#^Variable \\$message on left side of \\?\\? always exists and is not nullable\\.$#" message: "#^Variable \\$message on left side of \\?\\? always exists and is not nullable\\.$#"
count: 1 count: 1

View File

@ -33,12 +33,12 @@ class CustomFieldsGroupToIdTransformer implements DataTransformerInterface
if (null === $customFieldsGroup) { if (null === $customFieldsGroup) {
return ""; return "";
} }
if (!$customFieldsGroup instanceof CustomFieldsGroup) { if (!$customFieldsGroup instanceof CustomFieldsGroup) {
throw new TransformationFailedException(sprintf('Transformation failed: ' throw new TransformationFailedException(sprintf('Transformation failed: '
. 'the expected type of the transforme function is an ' . 'the expected type of the transforme function is an '
. 'object of type Chill\CustomFieldsBundle\Entity\CustomFieldsGroup, ' . 'object of type Chill\CustomFieldsBundle\Entity\CustomFieldsGroup, '
. '%s given (value : %s)', gettype($customFieldsGroup), . '%s given (value : %s)', gettype($customFieldsGroup),
$customFieldsGroup)); $customFieldsGroup));
} }
@ -57,26 +57,31 @@ class CustomFieldsGroupToIdTransformer implements DataTransformerInterface
if (!$id) { if (!$id) {
return null; return null;
} }
if ($id instanceof CustomFieldsGroup) { if ($id instanceof CustomFieldsGroup) {
throw new TransformationFailedException(sprintf( throw new TransformationFailedException(
sprintf(
'The transformation failed: the expected argument on ' 'The transformation failed: the expected argument on '
. 'reverseTransform is an object of type int,' . 'reverseTransform is an object of type int,'
. 'Chill\CustomFieldsBundle\Entity\CustomFieldsGroup, ' . 'Chill\CustomFieldsBundle\Entity\CustomFieldsGroup, '
. 'given', gettype($id))); . 'given'
)
);
} }
$customFieldsGroup = $this->om $customFieldsGroup = $this->om
->getRepository('ChillCustomFieldsBundle:customFieldsGroup')->find($id) ->getRepository(CustomFieldsGroup::class)->find($id)
; ;
if (null === $customFieldsGroup) { if (null === $customFieldsGroup) {
throw new TransformationFailedException(sprintf( throw new TransformationFailedException(
'Le group avec le numéro "%s" ne peut pas être trouvé!', sprintf(
$id 'Le group avec le numéro "%s" ne peut pas être trouvé!',
)); $id
)
);
} }
return $customFieldsGroup; return $customFieldsGroup;
} }
} }

View File

@ -1,22 +1,4 @@
<?php <?php
/*
* Chill is a software for social workers
*
* Copyright (C) 2019, Champs Libres Cooperative SCRLFS, <http://www.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/>.
*/
namespace Chill\MainBundle\CRUD\Controller; namespace Chill\MainBundle\CRUD\Controller;
@ -37,58 +19,38 @@ use Chill\MainBundle\Pagination\PaginatorInterface;
use Chill\MainBundle\Security\Authorization\AuthorizationHelper; use Chill\MainBundle\Security\Authorization\AuthorizationHelper;
use Symfony\Component\Serializer\SerializerInterface; use Symfony\Component\Serializer\SerializerInterface;
/**
* Class CRUDController
*
* @package Chill\MainBundle\CRUD\Controller
*/
class CRUDController extends AbstractController class CRUDController extends AbstractController
{ {
/** /**
* The crud configuration * The crud configuration
* *
* This configuration si defined by `chill_main['crud']`. * This configuration si defined by `chill_main['crud']`.
*
* @var array
*/ */
protected $crudConfig; protected array $crudConfig;
/** public function setCrudConfig(array $config): void
* @param array $config
*/
public function setCrudConfig(array $config)
{ {
$this->crudConfig = $config; $this->crudConfig = $config;
} }
/** public function CRUD(?string $parameter): Response
* @param $parameter
* @return Response
*/
public function CRUD($parameter)
{ {
return new Response($parameter); return new Response($parameter);
} }
/** /**
* @param Request $request
* @param $id * @param $id
* @return \Symfony\Component\HttpFoundation\RedirectResponse|Response
*/ */
public function delete(Request $request, $id) public function delete(Request $request, $id): Response
{ {
return $this->deleteAction('delete', $request, $id); return $this->deleteAction('delete', $request, $id);
} }
/** /**
* @param string $action
* @param Request $request
* @param $id * @param $id
* @param null $formClass * @param null $formClass
* @return null|\Symfony\Component\HttpFoundation\RedirectResponse|Response|void
*/ */
protected function deleteAction(string $action, Request $request, $id, $formClass = null) protected function deleteAction(string $action, Request $request, $id, $formClass = null): Response
{ {
$this->onPreDelete($action, $request, $id); $this->onPreDelete($action, $request, $id);
@ -101,8 +63,13 @@ class CRUDController extends AbstractController
} }
if (NULL === $entity) { if (NULL === $entity) {
throw $this->createNotFoundException(sprintf("The %s with id %s " throw $this->createNotFoundException(
. "is not found"), $this->getCrudName(), $id); sprintf(
'The %s with id %s is not found',
$this->getCrudName(),
$id
)
);
} }
$response = $this->checkACL($action, $entity); $response = $this->checkACL($action, $entity);
@ -141,7 +108,9 @@ class CRUDController extends AbstractController
return $this->redirectToRoute('chill_crud_'.$this->getCrudName().'_view', ['id' => $entity->getId()]); return $this->redirectToRoute('chill_crud_'.$this->getCrudName().'_view', ['id' => $entity->getId()]);
} elseif ($form->isSubmitted()) { }
if ($form->isSubmitted()) {
$this->addFlash('error', $this->generateFormErrorMessage($action, $form)); $this->addFlash('error', $this->generateFormErrorMessage($action, $form));
} }
@ -505,8 +474,13 @@ class CRUDController extends AbstractController
} }
if (NULL === $entity) { if (NULL === $entity) {
throw $this->createNotFoundException(sprintf("The %s with id %s " throw $this->createNotFoundException(
. "is not found", $this->getCrudName(), $id)); sprintf(
'The %s with id %s is not found',
$this->getCrudName(),
$id
)
);
} }
$response = $this->checkACL($action, $entity); $response = $this->checkACL($action, $entity);
@ -598,8 +572,13 @@ class CRUDController extends AbstractController
$entity = $this->getEntity($action, $id, $request); $entity = $this->getEntity($action, $id, $request);
if (NULL === $entity) { if (NULL === $entity) {
throw $this->createNotFoundException(sprintf("The %s with id %s " throw $this->createNotFoundException(
. "is not found", $this->getCrudName(), $id)); sprintf(
'The %s with id %s is not found',
$this->getCrudName(),
$id
)
);
} }
$response = $this->checkACL($action, $entity); $response = $this->checkACL($action, $entity);

View File

@ -1,22 +1,10 @@
<?php <?php
/*
* Copyright (C) 2018 Champs Libres Cooperative <info@champs-libres.coop> declare(strict_types=1);
*
* 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/>.
*/
namespace Chill\MainBundle\Security\UserProvider; namespace Chill\MainBundle\Security\UserProvider;
use Doctrine\ORM\NoResultException;
use Symfony\Component\Security\Core\User\UserProviderInterface; use Symfony\Component\Security\Core\User\UserProviderInterface;
use Symfony\Component\Security\Core\User\UserInterface; use Symfony\Component\Security\Core\User\UserInterface;
use Doctrine\ORM\EntityManagerInterface; 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\UsernameNotFoundException;
use Symfony\Component\Security\Core\Exception\UnsupportedUserException; use Symfony\Component\Security\Core\Exception\UnsupportedUserException;
/**
*
*
* @author Julien Fastré <julien.fastre@champs-libres.coop>
*/
class UserProvider implements UserProviderInterface class UserProvider implements UserProviderInterface
{ {
/** protected EntityManagerInterface $em;
*
* @var EntityManagerInterface
*/
protected $em;
public function __construct(EntityManagerInterface $em) public function __construct(EntityManagerInterface $em)
{ {
$this->em = $em; $this->em = $em;
} }
public function loadUserByUsername($username): UserInterface public function loadUserByUsername($username): UserInterface
{ {
try { try {
@ -50,14 +28,18 @@ class UserProvider implements UserProviderInterface
"SELECT u FROM %s u " "SELECT u FROM %s u "
. "WHERE u.usernameCanonical = UNACCENT(LOWER(:pattern)) " . "WHERE u.usernameCanonical = UNACCENT(LOWER(:pattern)) "
. "OR " . "OR "
. "u.emailCanonical = UNACCENT(LOWER(:pattern))", . "u.emailCanonical = UNACCENT(LOWER(:pattern))",
User::class)) User::class))
->setParameter('pattern', $username) ->setParameter('pattern', $username)
->getSingleResult(); ->getSingleResult();
} catch (\Doctrine\ORM\NoResultException $e) { } catch (NoResultException $e) {
throw new UsernameNotFoundException(sprintf('Bad credentials.', $username)); throw new UsernameNotFoundException(
sprintf('Bad credentials.'),
0,
$e
);
} }
return $user; return $user;
} }
@ -66,13 +48,13 @@ class UserProvider implements UserProviderInterface
if (!$user instanceof User) { if (!$user instanceof User) {
throw new UnsupportedUserException("Unsupported user class: cannot reload this user"); throw new UnsupportedUserException("Unsupported user class: cannot reload this user");
} }
$reloadedUser = $this->em->getRepository(User::class)->find($user->getId()); $reloadedUser = $this->em->getRepository(User::class)->find($user->getId());
if (NULL === $reloadedUser) { if (NULL === $reloadedUser) {
throw new UsernameNotFoundException(sprintf('User with ID "%s" could not be reloaded.', $user->getId())); throw new UsernameNotFoundException(sprintf('User with ID "%s" could not be reloaded.', $user->getId()));
} }
return $reloadedUser; return $reloadedUser;
} }