Skip to content

Commit

Permalink
Refactor code, update PHP version and optimize database queries
Browse files Browse the repository at this point in the history
This commit features modifications to streamline the code by revising the annotation format and removing unnecessary imports. It also updates the PHP version from 8.2 to 8.3 in the rector configuration file. The sorting mechanism in the CityRepository was also switched to Order from Criteria for efficiency improvement in database queries.
  • Loading branch information
Spomky committed Mar 24, 2024
1 parent cacbdd2 commit 3e64921
Show file tree
Hide file tree
Showing 9 changed files with 18 additions and 25 deletions.
3 changes: 1 addition & 2 deletions rector.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
declare(strict_types=1);

use Rector\Config\RectorConfig;
use Rector\Core\ValueObject\PhpVersion;

use Rector\Doctrine\Set\DoctrineSetList;
use Rector\Php71\Rector\FuncCall\RemoveExtraParametersRector;
use Rector\PHPUnit\Set\PHPUnitLevelSetList;
Expand All @@ -13,6 +11,7 @@
use Rector\Set\ValueObject\SetList;
use Rector\Symfony\Set\SymfonyLevelSetList;
use Rector\Symfony\Set\SymfonySetList;
use Rector\ValueObject\PhpVersion;

return static function (RectorConfig $rectorConfig): void {
$rectorConfig->sets([
Expand Down
4 changes: 1 addition & 3 deletions src/ApiResource/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,7 @@
use ApiPlatform\Metadata\Get;
use App\State\UserDtoProvider;

#[ApiResource(
operations: [new Get('/me', provider: UserDtoProvider::class)],
)]
#[ApiResource(operations: [new Get('/me', provider: UserDtoProvider::class)],)]
final readonly class User
{
public function __construct(
Expand Down
1 change: 0 additions & 1 deletion src/Controller/SecurityController.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

namespace App\Controller;

use LogicException;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
Expand Down
16 changes: 7 additions & 9 deletions src/Entity/City.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,27 +12,25 @@

#[ORM\Entity(repositoryClass: CityRepository::class)]
#[ORM\Table(name: '`cities`')]
#[ApiResource(
operations: [new GetCollection(), new Get()]
)]
#[ApiResource(operations: [new GetCollection(), new Get()])]
class City
{
public function __construct(
#[ORM\Id]
#[ORM\Column]
public int $id,
public int $id,
#[ORM\ManyToOne(inversedBy: 'cities')]
public Department $department,
#[ORM\Column(length: 10, nullable: true)]
public ?string $inseeCode,
public ?string $inseeCode,
#[ORM\Column(length: 10, nullable: true)]
public ?string $zipCode,
public ?string $zipCode,
#[ORM\Column(length: 200)]
public string $name,
public string $name,
#[ORM\Column(length: 200)]
public string $slug,
public string $slug,
#[ORM\Column]
public float $gpsLat,
public float $gpsLat,
#[ORM\Column]
public float $gpsLng
) {
Expand Down
4 changes: 1 addition & 3 deletions src/Entity/Department.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,7 @@

#[ORM\Entity(repositoryClass: DepartmentRepository::class)]
#[ORM\Table(name: '`departments`')]
#[ApiResource(
operations: [new GetCollection(), new Get()]
)]
#[ApiResource(operations: [new GetCollection(), new Get()])]
class Department
{
/**
Expand Down
4 changes: 1 addition & 3 deletions src/Entity/Region.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,7 @@

#[ORM\Entity(repositoryClass: RegionRepository::class)]
#[ORM\Table(name: '`regions`')]
#[ApiResource(
operations: [new GetCollection(), new Get()]
)]
#[ApiResource(operations: [new GetCollection(), new Get()])]
class Region
{
/**
Expand Down
4 changes: 2 additions & 2 deletions src/Repository/CityRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

use App\Entity\City;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Doctrine\Common\Collections\Criteria;
use Doctrine\Common\Collections\Order;
use Doctrine\Persistence\ManagerRegistry;

/**
Expand Down Expand Up @@ -63,7 +63,7 @@ public function findByNameLike(string $query): array
'(LOWER(c.name) LIKE :query) OR (LOWER(c.zipCode) LIKE :query) OR (LOWER(c.inseeCode) LIKE :query)'
)
->setParameter('query', sprintf('%%%s%%', mb_strtolower($query)))
->orderBy('c.name', Criteria::ASC)
->orderBy('c.name', Order::Ascending->value)
->setMaxResults(50)
->getQuery()
->getResult()
Expand Down
5 changes: 4 additions & 1 deletion src/Security/UserProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@
use Symfony\Component\Security\Core\User\UserInterface;
use Symfony\Component\Security\Core\User\UserProviderInterface;

/**
* @implements UserProviderInterface<User>
*/
final readonly class UserProvider implements UserProviderInterface
{
public function __construct(
Expand All @@ -21,7 +24,7 @@ public function __construct(
public function loadUserByIdentifier(string $identifier): UserInterface
{
$user = $this->userRepository->findOneByUsername($identifier);
$user !== null || throw new UserNotFoundException('User not found');
$user instanceof UserInterface || throw new UserNotFoundException('User not found');

return $user;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Security/UsernamePasswordAuthenticator.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public function authenticate(Request $request): Passport
);
}

public function onAuthenticationSuccess(Request $request, TokenInterface $token, string $firewallName): ?Response
public function onAuthenticationSuccess(Request $request, TokenInterface $token, string $firewallName): Response
{
$targetPath = $this->getTargetPath($request->getSession(), $firewallName);
if ($targetPath !== null) {
Expand Down

0 comments on commit 3e64921

Please sign in to comment.