Passed
Push — main ( 8f061d...12f820 )
by Karl
05:34
created

FrenchSuitedDeck::sortByValue()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 19
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 1

Importance

Changes 4
Bugs 0 Features 0
Metric Value
cc 1
eloc 4
nc 1
nop 2
dl 0
loc 19
ccs 10
cts 10
cp 1
crap 1
rs 10
c 4
b 0
f 0
1
<?php
2
3
namespace App\Model;
4
5
use App\Model\DeckOfCards;
6
use Random\Randomizer;
0 ignored issues
show
Bug introduced by
The type Random\Randomizer was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
7
use App\Model\Card;
8
9
class FrenchSuitedDeck extends DeckOfCards
10
{
11
    /**
12
     * Creates a new instance of FrenchSuitedDeck.
13
     *
14
     * @param Randomizer $randomizer The randomizer object to be used for shuffling the deck.
15
     * @param array<SplObserver> $observers [Optional] An array of SplObserver objects to be attached to the deck for notification on deck creation. Defaults to an empty array.
16
     * @return FrenchSuitedDeck The newly created FrenchSuitedDeck object.
17
     */
18 16
    public static function create(Randomizer $randomizer, array $observers = []): FrenchSuitedDeck
19
    {
20 16
        $deck = new FrenchSuitedDeck($randomizer, $observers);
21 16
        foreach (self::$suitsOfCards as $suit) {
22 16
            foreach (self::$namesOfCards as $name => $value) {
23 16
                $deck->addCard(new Card($suit, $name, $value[0], $value[1] ?? null));
24
            }
25
        }
26 16
        $deck->notify();
27 16
        return $deck;
28
    }
29
30
31 3
    public static function createFromSession(mixed $deck, Randomizer $randomizer, array $observers = []): FrenchSuitedDeck
32
    {
33 3
        $frenchDeck = new FrenchSuitedDeck($randomizer, $observers);
34
        // interesting enough, the card which are coming from session are restored as Card-objects, while the Deck is an Array.
35 3
        foreach ($deck as $card) {
36 3
            $frenchDeck->addCard($card);
37
        }
38 3
        $frenchDeck->notify();
39 3
        return $frenchDeck;
40
    }
41
42 4
    public function sort(): void
43
    {
44 4
        usort($this->cards, [$this, 'sortBySuitAndValue']);
45 4
        $this->notify();
46
    }
47
48 4
    public function sortBySuit(Card $cardA, Card $cardB): int
49
    {
50 4
        $sortingOrder = ['Spade', 'Heart', 'Diamond', 'Club'];
51 4
        return array_search($cardA->getSuit(), $sortingOrder) <=> array_search($cardB->getSuit(), $sortingOrder);
52
    }
53
54 4
    public function sortByValue(Card $cardA, Card $cardB): int
55
    {
56 4
        // if ($cardA->getAlternativeValue() !== null && $cardB->getAlternativeValue() !== null) {
57 1
        //     return $cardA->getAlternativeValue() <=> $cardB->getAlternativeValue();
58
        // }
59 4
        // if ($cardA->getAlternativeValue() !== null && !($cardB->getAlternativeValue() !== null)) {
60 4
        //     return $cardA->getAlternativeValue() <=> $cardB->getValue();
61
        // }
62
63 4
        // if (!($cardA->getAlternativeValue() !== null) && $cardB->getAlternativeValue() !== null) {
64 4
        //     return $cardA->getValue() <=> $cardB->getAlternativeValue();
65
        // }
66
67 4
        // return $cardA->getValue() <=> $cardB->getValue();
68
        return (
69
            $cardA->getAlternativeValue() ?? $cardA->getValue()
70 4
            )
71
            <=>
72 4
            ($cardB->getAlternativeValue() ?? $cardB->getValue());
73 4
    }
74 4
75
    public function sortBySuitAndValue(Card $cardA, Card $cardB): int
76 4
    {
77
        $suitComparison = $this->sortBySuit($cardA, $cardB);
78
        if ($suitComparison === 0) {
79
            return $this->sortByValue($cardA, $cardB);
80
        }
81
        return $suitComparison;
82
    }
83
}
84