From 5274194c5140d5c9dc96afeebaf363b76bdb120d Mon Sep 17 00:00:00 2001 From: Valery Maslov Date: Tue, 28 Nov 2023 10:12:18 +0300 Subject: [PATCH] Fix some issues reported by phpstan --- phpstan.neon | 3 +-- .../Auth/ResendVerificationController.php | 4 ++-- src/Controller/Ajax/CityController.php | 7 ++++--- .../Auth/VerificationController.php | 5 ++++- src/Entity/Category.php | 2 ++ src/Entity/DealType.php | 2 ++ src/Entity/Traits/PropertyTrait.php | 6 +++--- src/Repository/PropertyRepository.php | 2 ++ src/Service/AbstractService.php | 4 ++-- src/Service/Auth/EmailVerifier.php | 4 ++-- src/Service/Cache/GetCache.php | 19 ++++++++++++++++--- .../User/GoogleAuthenticatorService.php | 8 ++++---- src/Service/User/PasswordService.php | 2 +- src/Service/User/PropertyService.php | 5 ++++- src/Validator/ConfirmPasswordValidator.php | 2 +- src/Validator/RegisteredUserValidator.php | 2 +- 16 files changed, 51 insertions(+), 26 deletions(-) diff --git a/phpstan.neon b/phpstan.neon index e0de575f..7a2b6df3 100644 --- a/phpstan.neon +++ b/phpstan.neon @@ -1,8 +1,7 @@ parameters: - level: 6 + level: 2 paths: - bin/ - config/ - public/ - src/ - - tests/ diff --git a/src/Controller/Ajax/Auth/ResendVerificationController.php b/src/Controller/Ajax/Auth/ResendVerificationController.php index 7e534864..05a34195 100644 --- a/src/Controller/Ajax/Auth/ResendVerificationController.php +++ b/src/Controller/Ajax/Auth/ResendVerificationController.php @@ -22,7 +22,7 @@ final class ResendVerificationController extends AbstractController implements A public function shouldLinkBeVisible(): JsonResponse { /** - * @var $user User + * @var User $user */ $user = $this->getUser(); @@ -33,7 +33,7 @@ public function shouldLinkBeVisible(): JsonResponse public function resendEmail(MessageBusInterface $messageBus, TranslatorInterface $translator): JsonResponse { /** - * @var $user User + * @var User $user */ $user = $this->getUser(); diff --git a/src/Controller/Ajax/CityController.php b/src/Controller/Ajax/CityController.php index 8013ae58..1f4f7f1b 100644 --- a/src/Controller/Ajax/CityController.php +++ b/src/Controller/Ajax/CityController.php @@ -8,7 +8,6 @@ use App\Repository\DistrictRepository; use App\Repository\MetroRepository; use App\Repository\NeighborhoodRepository; -use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepositoryInterface; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; use Symfony\Component\HttpFoundation\JsonResponse; use Symfony\Component\Routing\Annotation\Route; @@ -30,8 +29,10 @@ public function show( ]); } - private function find(City $city, ServiceEntityRepositoryInterface $repository): array - { + private function find( + City $city, + DistrictRepository|MetroRepository|NeighborhoodRepository $repository + ): array { return array_map(fn ($entity) => [ 'id' => $entity->getId(), 'name' => $entity->getName(), diff --git a/src/Controller/Auth/VerificationController.php b/src/Controller/Auth/VerificationController.php index 09cae52d..5a786a23 100644 --- a/src/Controller/Auth/VerificationController.php +++ b/src/Controller/Auth/VerificationController.php @@ -4,6 +4,7 @@ namespace App\Controller\Auth; +use App\Entity\User; use App\Service\Auth\EmailVerifier; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; use Symfony\Component\HttpFoundation\Request; @@ -25,7 +26,9 @@ public function verifyUserEmail(Request $request, TranslatorInterface $translato // validate email confirmation link, sets User::isVerified=true and persists try { - $this->emailVerifier->handleEmailConfirmation($request, $this->getUser()); + /** @var User $user */ + $user = $this->getUser(); + $this->emailVerifier->handleEmailConfirmation($request, $user); } catch (VerifyEmailExceptionInterface $exception) { $this->addFlash('danger', $translator->trans($exception->getReason(), [], 'VerifyEmailBundle')); diff --git a/src/Entity/Category.php b/src/Entity/Category.php index 8df9219b..0fa62678 100644 --- a/src/Entity/Category.php +++ b/src/Entity/Category.php @@ -20,4 +20,6 @@ class Category use PropertyTrait; final public const MAPPED_BY = 'category'; + final public const GETTER = 'getCategory'; + final public const SETTER = 'setCategory'; } diff --git a/src/Entity/DealType.php b/src/Entity/DealType.php index 4eb56822..593fb9ec 100644 --- a/src/Entity/DealType.php +++ b/src/Entity/DealType.php @@ -20,4 +20,6 @@ class DealType use PropertyTrait; final public const MAPPED_BY = 'deal_type'; + final public const GETTER = 'getDealType'; + final public const SETTER = 'setDealType'; } diff --git a/src/Entity/Traits/PropertyTrait.php b/src/Entity/Traits/PropertyTrait.php index a54a63de..e77a5a8a 100644 --- a/src/Entity/Traits/PropertyTrait.php +++ b/src/Entity/Traits/PropertyTrait.php @@ -26,7 +26,7 @@ protected function getProperties(): Collection protected function addProperty(Property $property): self { - /* callable $setter */ + /* @var callable $setter */ $setter = self::SETTER; if (!$this->properties->contains($property)) { @@ -39,10 +39,10 @@ protected function addProperty(Property $property): self protected function removeProperty(Property $property): self { - /* callable $setter */ + /* @var callable $setter */ $setter = self::SETTER; - /* callable $getter */ + /* @var callable $getter */ $getter = self::GETTER; if ($this->properties->contains($property)) { diff --git a/src/Repository/PropertyRepository.php b/src/Repository/PropertyRepository.php index 0b4821ba..fb95a6b4 100644 --- a/src/Repository/PropertyRepository.php +++ b/src/Repository/PropertyRepository.php @@ -8,6 +8,7 @@ use App\Entity\Settings; use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository; use Doctrine\ORM\NonUniqueResultException; +use Doctrine\ORM\NoResultException; use Doctrine\ORM\Query; use Doctrine\Persistence\ManagerRegistry; use Knp\Component\Pager\Pagination\PaginationInterface; @@ -42,6 +43,7 @@ public function findAllPublished(): array /** * @throws NonUniqueResultException + * @throws NoResultException */ public function countAll(): int { diff --git a/src/Service/AbstractService.php b/src/Service/AbstractService.php index 7935035b..36813ce1 100644 --- a/src/Service/AbstractService.php +++ b/src/Service/AbstractService.php @@ -6,7 +6,7 @@ use App\Service\Cache\ClearCache; use Symfony\Component\HttpFoundation\RequestStack; -use Symfony\Component\HttpFoundation\Session\SessionInterface; +use Symfony\Component\HttpFoundation\Session\FlashBagAwareSessionInterface; use Symfony\Component\Security\Csrf\CsrfToken; use Symfony\Component\Security\Csrf\CsrfTokenManagerInterface; @@ -14,7 +14,7 @@ abstract class AbstractService { use ClearCache; - private readonly SessionInterface $session; + private readonly FlashBagAwareSessionInterface $session; public function __construct(private readonly CsrfTokenManagerInterface $tokenManager, RequestStack $requestStack) { diff --git a/src/Service/Auth/EmailVerifier.php b/src/Service/Auth/EmailVerifier.php index cbe583df..e4691832 100644 --- a/src/Service/Auth/EmailVerifier.php +++ b/src/Service/Auth/EmailVerifier.php @@ -4,9 +4,9 @@ namespace App\Service\Auth; +use App\Entity\User; use Doctrine\ORM\EntityManagerInterface; use Symfony\Component\HttpFoundation\Request; -use Symfony\Component\Security\Core\User\UserInterface; use SymfonyCasts\Bundle\VerifyEmail\Exception\VerifyEmailExceptionInterface; use SymfonyCasts\Bundle\VerifyEmail\VerifyEmailHelperInterface; @@ -21,7 +21,7 @@ public function __construct( /** * @throws VerifyEmailExceptionInterface */ - public function handleEmailConfirmation(Request $request, UserInterface $user): void + public function handleEmailConfirmation(Request $request, User $user): void { $this->verifyEmailHelper->validateEmailConfirmation($request->getUri(), (string) $user->getId(), $user->getEmail()); diff --git a/src/Service/Cache/GetCache.php b/src/Service/Cache/GetCache.php index f0297b66..c94d13aa 100644 --- a/src/Service/Cache/GetCache.php +++ b/src/Service/Cache/GetCache.php @@ -4,6 +4,14 @@ namespace App\Service\Cache; +use App\Repository\CategoryRepository; +use App\Repository\CityRepository; +use App\Repository\DealTypeRepository; +use App\Repository\PageRepository; +use App\Repository\PropertyRepository; +use App\Repository\UserRepository; +use Doctrine\ORM\NonUniqueResultException; +use Doctrine\ORM\NoResultException; use Doctrine\Persistence\ManagerRegistry; use Symfony\Component\Cache\Adapter\FilesystemAdapter; @@ -28,10 +36,15 @@ public function getCount(string $key, string $class): int return (int) $count; } + /** + * @throws NonUniqueResultException + * @throws NoResultException + */ private function countItems(): int { - return (int) $this->doctrine - ->getRepository($this->persistentObjectName) - ->countAll(); + /** @var PropertyRepository|CityRepository|DealTypeRepository|CategoryRepository|PageRepository|UserRepository $repository */ + $repository = $this->doctrine->getRepository($this->persistentObjectName); + + return $repository->countAll(); } } diff --git a/src/Service/User/GoogleAuthenticatorService.php b/src/Service/User/GoogleAuthenticatorService.php index d9100034..aead0fff 100644 --- a/src/Service/User/GoogleAuthenticatorService.php +++ b/src/Service/User/GoogleAuthenticatorService.php @@ -4,6 +4,7 @@ namespace App\Service\User; +use App\Entity\User; use App\Service\AbstractService; use Doctrine\ORM\EntityManagerInterface; use Endroid\QrCode\Builder\Builder; @@ -11,7 +12,6 @@ use Endroid\QrCode\ErrorCorrectionLevel\ErrorCorrectionLevelHigh; use Endroid\QrCode\RoundBlockSizeMode\RoundBlockSizeModeMargin; use Endroid\QrCode\Writer\PngWriter; -use Scheb\TwoFactorBundle\Model\Google\TwoFactorInterface; use Symfony\Component\HttpFoundation\RequestStack; use Symfony\Component\Security\Csrf\CsrfTokenManagerInterface; use Symfony\Contracts\Translation\TranslatorInterface; @@ -33,7 +33,7 @@ public function __construct( * * @return string[] */ - public function generateSecret(TwoFactorInterface $user): array + public function generateSecret(User $user): array { if ($user->isGoogleAuthenticatorEnabled()) { throw new \LogicException($this->translator->trans('2fa.errors.secret_is_already_set')); @@ -54,7 +54,7 @@ public function generateSecret(TwoFactorInterface $user): array /** * @throws \Exception */ - public function setSecret(TwoFactorInterface $user, ?string $secret, ?string $authenticationCode): void + public function setSecret(User $user, ?string $secret, ?string $authenticationCode): void { if (!$authenticationCode || !$secret) { throw new \Exception($this->translator->trans('2fa.errors.cannot_enable_ga')); @@ -70,7 +70,7 @@ public function setSecret(TwoFactorInterface $user, ?string $secret, ?string $au $this->addFlash('success', '2fa.messages.enabled'); } - public function deleteSecret(TwoFactorInterface $user): void + public function deleteSecret(User $user): void { $user->setGoogleAuthenticatorSecret(null); $this->entityManager->flush(); diff --git a/src/Service/User/PasswordService.php b/src/Service/User/PasswordService.php index 9e95adeb..0b5562be 100644 --- a/src/Service/User/PasswordService.php +++ b/src/Service/User/PasswordService.php @@ -32,7 +32,7 @@ public function update(Request $request): void throw new \Exception($violations[0]->getMessage()); } - /*** @var $user User */ + /** @var User $user * */ $user = $this->tokenStorage->getToken()->getUser(); $user->setPassword($request->get('password1')); $this->service->update($user); diff --git a/src/Service/User/PropertyService.php b/src/Service/User/PropertyService.php index 5b4339a3..fa256e55 100644 --- a/src/Service/User/PropertyService.php +++ b/src/Service/User/PropertyService.php @@ -5,6 +5,7 @@ namespace App\Service\User; use App\Entity\Property; +use App\Entity\User; use App\Repository\UserPropertyRepository; use App\Service\Admin\PropertyService as Service; use App\Transformer\PropertyTransformer; @@ -37,7 +38,9 @@ public function __construct( public function getUserProperties(Request $request): PaginationInterface { $searchParams = $this->transformer->transform($request); - $searchParams['user'] = $this->tokenStorage->getToken()->getUser()->getId(); + /** @var User $user */ + $user = $this->tokenStorage->getToken()->getUser(); + $searchParams['user'] = $user->getId(); return $this->repository->findByUser($searchParams); } diff --git a/src/Validator/ConfirmPasswordValidator.php b/src/Validator/ConfirmPasswordValidator.php index 51f3e6df..9df5a0f4 100644 --- a/src/Validator/ConfirmPasswordValidator.php +++ b/src/Validator/ConfirmPasswordValidator.php @@ -11,7 +11,7 @@ final class ConfirmPasswordValidator extends ConstraintValidator { public function validate($value, Constraint $constraint): void { - /* @var $constraint ConfirmPassword */ + /** @var ConfirmPassword $constraint */ if (null === $value || '' === $value) { return; diff --git a/src/Validator/RegisteredUserValidator.php b/src/Validator/RegisteredUserValidator.php index 20ec0ba0..40194bcc 100644 --- a/src/Validator/RegisteredUserValidator.php +++ b/src/Validator/RegisteredUserValidator.php @@ -17,7 +17,7 @@ public function __construct(private readonly UserRepository $userRepository) public function validate($value, Constraint $constraint): void { - /* @var $constraint RegisteredUser */ + /** @var RegisteredUser $constraint */ if (null === $value || '' === $value) { return;