Address PHPStan issues

This commit is contained in:
myrmidex 2026-06-05 22:51:24 +00:00
parent 5821209e4d
commit dc6c4a082d
4 changed files with 83 additions and 12 deletions

View file

@ -4,6 +4,7 @@ namespace App\Controller\Api;
use App\Entity\Rating;
use App\Entity\User;
use App\Presenter\RatingPresenter;
use App\Repository\RatingRepository;
use App\Repository\SubjectRepository;
use App\Services\OutputService;
@ -20,19 +21,10 @@ class RatingController extends AbstractController
public function index(
RatingRepository $repo,
OutputService $output,
RatingPresenter $ratingPresenter,
): JsonResponse {
return $this->json($output->success([
'ratings' => array_map(
fn(Rating $rating) => [
'id' => $rating->getId(),
'score' => $rating->getScore(),
'subject' => [
'id' => $rating->getSubject()->getId(),
'name' => $rating->getSubject()->getName(),
],
],
$repo->findAll()
),
'ratings' => $ratingPresenter->presentMany($repo->findAll()),
]), 200);
}

View file

@ -4,6 +4,7 @@ namespace App\Controller\Api;
use App\Entity\Restaurant;
use App\Entity\User;
use App\Presenter\SubjectPresenter;
use App\Repository\SubjectRepository;
use App\Services\OutputService;
use Doctrine\ORM\EntityManagerInterface;
@ -19,8 +20,11 @@ class SubjectController extends AbstractController
public function index(
SubjectRepository $repo,
OutputService $output,
SubjectPresenter $subjectPresenter,
): JsonResponse {
return $this->json($output->success(['subjects' => $repo->findAll()]));
return $this->json($output->success([
'subjects' => $subjectPresenter->presentMany($repo->findAll()),
]));
}
#[Route('/api/subjects', methods: ['POST'])]

View file

@ -0,0 +1,33 @@
<?php
namespace App\Presenter;
use App\Entity\Rating;
class RatingPresenter
{
/**
* @return array<string, mixed>
*/
public function present(Rating $rating): array
{
return [
'id' => $rating->getId(),
'score' => $rating->getScore(),
'note' => $rating->getNote(),
'createdAt' => $rating->getCreatedAt()?->format(\DateTimeInterface::ATOM),
'user' => [
'id' => $rating->getUser()->getId(),
],
];
}
/**
* @param iterable<Rating> $ratings
* @return array<int, array<string, mixed>>
*/
public function presentMany(iterable $ratings): array
{
return array_map(fn(Rating $r) => $this->present($r), [...$ratings]);
}
}

View file

@ -0,0 +1,42 @@
<?php
namespace App\Presenter;
use App\Entity\Subject;
class SubjectPresenter
{
public function __construct(
private RatingPresenter $ratingPresenter,
) {}
/**
* @return array<string, mixed>
*/
public function present(Subject $subject): array
{
return [
'id' => $subject->getId(),
'name' => $subject->getName(),
'latitude' => $subject->getLatitude(),
'longitude' => $subject->getLongitude(),
'createdBy' => [
'id' => $subject->getCreatedBy()->getId(),
'email' => $subject->getCreatedBy()->getEmail(),
],
'ratings' => array_map(
fn($r) => $this->ratingPresenter->present($r),
$subject->getRatings()->toArray()
),
];
}
/**
* @param iterable<Subject> $subjects
* @return array<int, array<string, mixed>>
*/
public function presentMany(iterable $subjects): array
{
return array_map(fn($s) => $this->present($s), [...$subjects]);
}
}