CardDealer::deal()   A
last analyzed

Complexity

Conditions 5
Paths 8

Size

Total Lines 17
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
eloc 9
c 1
b 0
f 0
nc 8
nop 3
dl 0
loc 17
rs 9.6111
1
<?php
2
3
namespace App\Model;
4
5
use Exception;
6
7
class CardDealer
8
{
9
    /**
10
     * Deal cards to players.
11
     *
12
     * @param DeckOfCards $deck The deck of cards to deal from.
13
     * @param int $numberOfPlayers The number of players to deal to.
14
     * @param int $cardsPerPlayer The number of cards to deal to each player.
15
     *
16
     * @return array<CardHand> The hands of the players.
17
     */
18
    public function deal(DeckOfCards $deck, int $numberOfPlayers, int $cardsPerPlayer): array
19
    {
20
        $hands = [];
21
        for ($i = 0; $i < $numberOfPlayers; $i++) {
22
            $hands[] = new CardHand();
23
        }
24
25
        for ($i = 0; $i < $cardsPerPlayer; $i++) {
26
            foreach ($hands as $hand) {
27
                if (!$deck->cardCollection->getNumberOfCards() > 0) {
28
                    throw new Exception('Not enough cards to deal');
29
                }
30
                $hand->addCard($deck->cardCollection->drawCards(1)[0]);
31
            }
32
        }
33
34
        return $hands;
35
    }
36
}
37