Issues (977)

src/Controller/PokerSquareController.php (2 issues)

Labels
Severity
1
<?php
2
3
namespace App\Controller;
4
5
use App\Game\PokerSquareGame;
6
use App\Service\HighscoreService;
7
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
8
use Symfony\Component\HttpFoundation\Request;
9
use Symfony\Component\HttpFoundation\Response;
10
use Symfony\Component\HttpFoundation\Session\SessionInterface;
11
use Symfony\Component\Routing\Annotation\Route;
12
13
class PokerSquareController extends AbstractController
14
{
15
    /**
16
     * Show the current state of the Poker Square game.
17
     */
18
    #[Route('/proj/play', name: 'proj_play')]
19
    public function play(SessionInterface $session): Response
20
    {
21
        $game = $session->get('poker_game');
22
23
        if (!$game instanceof PokerSquareGame) {
24
            $game = new PokerSquareGame();
25
            $session->set('poker_game', $game);
26
        }
27
28
        return $this->render('projekt/play.html.twig', [
29
            'grid' => $game->getGrid(),
30
            'nextCard' => $game->getCurrentCard(),
31
            'gameOver' => $game->isGameOver(),
32
            'scores' => $game->getGridScores(),
33
            'totalScore' => $game->getTotalScore(),
34
            'suggestedMove' => $game->getSuggestedMove(),
0 ignored issues
show
Are you sure the usage of $game->getSuggestedMove() targeting App\Game\PokerSquareGame::getSuggestedMove() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
35
        ]);
36
    }
37
38
    /**
39
     * Place a card at a specified position in the grid.
40
     */
41
    #[Route('/proj/place', name: 'proj_place_card', methods: ['POST'])]
42
    public function place(Request $request, SessionInterface $session): Response
43
    {
44
        /** @var PokerSquareGame $game */
45
        $game = $session->get('poker_game');
46
        $position = $request->request->get('position');
47
48
        if ($game && $position) {
49
            $game->placeCard($position);
50
            $session->set('poker_game', $game);
51
        }
52
53
        return $this->redirectToRoute('proj_play');
54
    }
55
56
    /**
57
     * Save the current score to the highscore list and reset the game.
58
     */
59
    #[Route('/proj/save-score', name: 'proj_save_score', methods: ['POST'])]
60
    public function saveScore(Request $request, SessionInterface $session, HighscoreService $highscoreService): Response
61
    {
62
        /** @var PokerSquareGame $game */
63
        $game = $session->get('poker_game');
64
        $name = $request->request->get('player_name');
65
        $scores = $game->getGridScores();
66
        $total = array_sum($scores['rows']) + array_sum($scores['cols']);
67
68
        $highscoreService->addScore($name, $total);
0 ignored issues
show
$total of type double is incompatible with the type integer expected by parameter $score of App\Service\HighscoreService::addScore(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

68
        $highscoreService->addScore($name, /** @scrutinizer ignore-type */ $total);
Loading history...
69
        $session->remove('poker_game');
70
71
        return $this->redirectToRoute('proj_highscores');
72
    }
73
74
    /**
75
     * Show the list of highscores.
76
     */
77
    #[Route('/proj/highscores', name: 'proj_highscores')]
78
    public function highscores(HighscoreService $highscoreService): Response
79
    {
80
        $scores = $highscoreService->getHighscores();
81
82
        return $this->render('projekt/highscores.html.twig', [
83
            'highscores' => $scores,
84
        ]);
85
    }
86
87
    /**
88
     * Reset the current game session.
89
     */
90
    #[Route('/proj/reset', name: 'proj_reset')]
91
    public function reset(SessionInterface $session): Response
92
    {
93
        $session->remove('poker_game');
94
95
        return $this->redirectToRoute('proj_play');
96
    }
97
}
98