Skip to content

Commit

Permalink
Fix some issues reported by phpstan
Browse files Browse the repository at this point in the history
  • Loading branch information
Coderberg committed Nov 28, 2023
1 parent 041055a commit 5274194
Show file tree
Hide file tree
Showing 16 changed files with 51 additions and 26 deletions.
3 changes: 1 addition & 2 deletions phpstan.neon
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
parameters:
level: 6
level: 2
paths:
- bin/
- config/
- public/
- src/
- tests/
4 changes: 2 additions & 2 deletions src/Controller/Ajax/Auth/ResendVerificationController.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ final class ResendVerificationController extends AbstractController implements A
public function shouldLinkBeVisible(): JsonResponse
{
/**
* @var $user User
* @var User $user
*/
$user = $this->getUser();

Expand All @@ -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();

Expand Down
7 changes: 4 additions & 3 deletions src/Controller/Ajax/CityController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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(),
Expand Down
5 changes: 4 additions & 1 deletion src/Controller/Auth/VerificationController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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'));

Expand Down
2 changes: 2 additions & 0 deletions src/Entity/Category.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,6 @@ class Category
use PropertyTrait;

final public const MAPPED_BY = 'category';
final public const GETTER = 'getCategory';
final public const SETTER = 'setCategory';
}
2 changes: 2 additions & 0 deletions src/Entity/DealType.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';
}
6 changes: 3 additions & 3 deletions src/Entity/Traits/PropertyTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand All @@ -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)) {
Expand Down
2 changes: 2 additions & 0 deletions src/Repository/PropertyRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -42,6 +43,7 @@ public function findAllPublished(): array

/**
* @throws NonUniqueResultException
* @throws NoResultException
*/
public function countAll(): int
{
Expand Down
4 changes: 2 additions & 2 deletions src/Service/AbstractService.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@

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;

abstract class AbstractService
{
use ClearCache;

private readonly SessionInterface $session;
private readonly FlashBagAwareSessionInterface $session;

public function __construct(private readonly CsrfTokenManagerInterface $tokenManager, RequestStack $requestStack)
{
Expand Down
4 changes: 2 additions & 2 deletions src/Service/Auth/EmailVerifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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());

Expand Down
19 changes: 16 additions & 3 deletions src/Service/Cache/GetCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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();
}
}
8 changes: 4 additions & 4 deletions src/Service/User/GoogleAuthenticatorService.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@

namespace App\Service\User;

use App\Entity\User;
use App\Service\AbstractService;
use Doctrine\ORM\EntityManagerInterface;
use Endroid\QrCode\Builder\Builder;
use Endroid\QrCode\Encoding\Encoding;
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;
Expand All @@ -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'));
Expand All @@ -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'));
Expand All @@ -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();
Expand Down
2 changes: 1 addition & 1 deletion src/Service/User/PasswordService.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
5 changes: 4 additions & 1 deletion src/Service/User/PropertyService.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
Expand Down
2 changes: 1 addition & 1 deletion src/Validator/ConfirmPasswordValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion src/Validator/RegisteredUserValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 5274194

Please sign in to comment.