CardDealer   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 28
Duplicated Lines 0 %

Importance

Changes 2
Bugs 1 Features 0
Metric Value
eloc 10
dl 0
loc 28
rs 10
c 2
b 1
f 0
wmc 5

1 Method

Rating   Name   Duplication   Size   Complexity  
A deal() 0 17 5
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