Issues (24)

src/Controller/CardAPIController.php (6 issues)

1
<?php
2
3
namespace App\Controller;
4
5
use Symfony\Component\HttpFoundation\JsonResponse;
6
use Symfony\Component\HttpFoundation\Response;
7
use Symfony\Component\Routing\Annotation\Route;
8
use App\Card\Card;
9
use App\Card\CardGraphic;
10
use App\Card\DeckOfCards;
11
use App\Card\CardHand;
12
use Symfony\Component\HttpFoundation\Session\SessionInterface;
13
14
/**
15
 * CardAPIController - defines the JSON API endpoints for the Card game
16
 */
17
class CardAPIController
18
{
19
    /**
20
     * deck - Creates a new deck of cards and returns it
21
     *
22
     * @return void
23
     */
24
    #[Route("/api/deck", name: "api_deck", methods: ['GET'])]
25
    public function deck(
26
        SessionInterface $session
27
    ): Response {
28
        $deck = new DeckOfCards();
29
        $session->set("card_deck", $deck);
30
31
        $data = [
32
            "deck" => $deck->getDeck()
33
        ];
34
35
        $response = new JsonResponse($data);
36
        $response->setEncodingOptions(
37
            $response->getEncodingOptions() | JSON_PRETTY_PRINT
38
        );
39
        return $response;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $response returns the type Symfony\Component\HttpFoundation\JsonResponse which is incompatible with the documented return type void.
Loading history...
40
    }
41
42
    /**
43
     * shuffle - Shuffles an existing deck of cards and returns it
44
     *
45
     * @return void
46
     */
47
    #[Route("/api/deck/shuffle", name: "api_deck_shuffle", methods: ['POST'])]
48
    public function shuffle(
49
        SessionInterface $session
50
    ): Response {
51
        $deck = $session->get("card_deck");
52
        if ($deck === null || $deck->getNumberOfCards() < 52) {
53
            $session->clear("card_deck");
0 ignored issues
show
The call to Symfony\Component\HttpFo...ssionInterface::clear() has too many arguments starting with 'card_deck'. ( Ignorable by Annotation )

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

53
            $session->/** @scrutinizer ignore-call */ 
54
                      clear("card_deck");

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
54
            $deck = new DeckOfCards();
55
        }
56
        $deck->shuffle();
57
        $session->set("card_deck", $deck);
58
        $data = [
59
            "deck" => $deck->getDeck()
60
        ];
61
        $response = new JsonResponse($data);
62
        $response->setEncodingOptions(
63
            $response->getEncodingOptions() | JSON_PRETTY_PRINT
64
        );
65
        return $response;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $response returns the type Symfony\Component\HttpFoundation\JsonResponse which is incompatible with the documented return type void.
Loading history...
66
    }
67
    /**
68
     * draw - Draws a card from the deck and returns it
69
     *
70
     * @return void
71
     */
72
    #[Route("/api/deck/draw", name: "api_deck_draw", methods: ['POST'])]
73
    public function draw(
74
        SessionInterface $session
75
    ): Response {
76
        $cardsStr = [];
77
        $deck = $session->get("card_deck");
78
        if ($deck == null) {
79
            $data = [
80
                "error" => "Ingen kortlek finns. Skapa en ny kortlek innan du drar ett kort."
81
            ];
82
        } else {
83
            $card = $deck->drawCard();
84
            $cardsStr[] = $card->getAsString();
85
            $session->set("card_deck", $deck);
86
            $data = [
87
                "cardsStr" => $cardsStr,
88
                "cards_left" => $deck->getNumberOfCards()
89
            ];
90
        }
91
        $response = new JsonResponse($data);
92
        $response->setEncodingOptions(
93
            $response->getEncodingOptions() | JSON_PRETTY_PRINT
94
        );
95
        return $response;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $response returns the type Symfony\Component\HttpFoundation\JsonResponse which is incompatible with the documented return type void.
Loading history...
96
    }
97
    /**
98
     * draw_multiple - draws multiple cards from the deck and returns them
99
     *
100
     * @return void
101
     */
102
    #[Route("/api/deck/draw/{num<\d+>}", name: "api_deck_draw_multiple", methods: ['POST'])]
103
    public function draw_multiple(
104
        int $num,
105
        SessionInterface $session
106
    ): Response {
107
        $deck = $session->get("card_deck");
108
109
        if ($deck == null) {
110
            $data = [
111
                "error" => "Ingen kortlek finns. Skapa en ny kortlek innan du drar ett kort."
112
            ];
113
        } else {
114
            $cardsleft = $deck->getNumberOfCards();
115
        }
116
        if ($num > $cardsleft) {
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $cardsleft does not seem to be defined for all execution paths leading up to this point.
Loading history...
117
            $data = [
118
                   "error" => "Du kan inte dra fler kort än det finns kvar i kortleken",
119
                   "cards_left" => $cardsleft
120
            ];
121
        } else {
122
            $cards = [];
123
            $cardsStr = [];
124
            for ($i = 0; $i < $num; $i++) {
125
                $cards[] = $deck->drawCard();
126
                $cardsStr[] = $cards[$i]->getAsString();
127
            }
128
            $session->set("card_deck", $deck);
129
            $data = [
130
                "cardsStr" => $cardsStr,
131
                "cards_left" => $deck->getNumberOfCards()
132
            ];
133
        }
134
135
        $response = new JsonResponse($data);
136
        $response->setEncodingOptions(
137
            $response->getEncodingOptions() | JSON_PRETTY_PRINT
138
        );
139
        return $response;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $response returns the type Symfony\Component\HttpFoundation\JsonResponse which is incompatible with the documented return type void.
Loading history...
140
    }
141
}
142