From 7eb95bbccc6e2db7dab7c0bfc627b9ca403df693 Mon Sep 17 00:00:00 2001 From: Sam Watkinson Date: Mon, 21 Nov 2022 17:36:52 +0000 Subject: [PATCH 1/3] refactor: extract SUITS const and type suit values as instance of Suit class --- exercises/php/cards/PlayingCard.php | 6 +++--- exercises/php/cards/PlayingCardDeck.php | 5 ++--- exercises/php/cards/Suit.php | 17 +++++++++++------ exercises/php/cards/Suits.php | 14 ++++++++++++++ 4 files changed, 30 insertions(+), 12 deletions(-) create mode 100644 exercises/php/cards/Suits.php diff --git a/exercises/php/cards/PlayingCard.php b/exercises/php/cards/PlayingCard.php index 05e53bd..854f2b9 100644 --- a/exercises/php/cards/PlayingCard.php +++ b/exercises/php/cards/PlayingCard.php @@ -6,10 +6,10 @@ class PlayingCard implements Card { - private string $suit; + private Suit $suit; private int $faceValue; - public function __construct(string $suit, int $faceValue) + public function __construct(Suit $suit, int $faceValue) { $this->suit = $suit; $this->faceValue = $faceValue; @@ -57,7 +57,7 @@ public function getFaceValue(): int public function getSuit(): string { - return $this->suit; + return strval($this->suit); } public function snap(?Card $card): bool diff --git a/exercises/php/cards/PlayingCardDeck.php b/exercises/php/cards/PlayingCardDeck.php index b12945e..2f090f9 100644 --- a/exercises/php/cards/PlayingCardDeck.php +++ b/exercises/php/cards/PlayingCardDeck.php @@ -8,12 +8,11 @@ class PlayingCardDeck implements Deck public function __construct() { - $cSuits = new Suit(); $this->cards = []; - foreach ($cSuits->suit as $suit) { + foreach (Suits::SUITS as $suit) { for ($faceValue = 0; $faceValue < 13; $faceValue++) { - $this->cards[] = new PlayingCard($suit, $faceValue); + $this->cards[] = new PlayingCard(new Suit($suit), $faceValue); } } } diff --git a/exercises/php/cards/Suit.php b/exercises/php/cards/Suit.php index dfae53e..b2d937b 100644 --- a/exercises/php/cards/Suit.php +++ b/exercises/php/cards/Suit.php @@ -4,11 +4,16 @@ class Suit { - public array $suit = [ - "clubs", - "diamonds", - "hearts", - "spades" - ]; + private string $suitName; + + public function __construct(string $suitName) + { + $this->suitName = $suitName; + } + + public function __toString(): string + { + return $this->suitName; + } } \ No newline at end of file diff --git a/exercises/php/cards/Suits.php b/exercises/php/cards/Suits.php new file mode 100644 index 0000000..9322f97 --- /dev/null +++ b/exercises/php/cards/Suits.php @@ -0,0 +1,14 @@ + Date: Mon, 21 Nov 2022 17:37:04 +0000 Subject: [PATCH 2/3] refactor: extract ANIMALS const and type animal values as instance of Animal class --- exercises/php/cards/Animal.php | 40 +++++++++--------------------- exercises/php/cards/AnimalCard.php | 6 ++--- exercises/php/cards/AnimalDeck.php | 7 +++--- exercises/php/cards/Animals.php | 36 +++++++++++++++++++++++++++ 4 files changed, 54 insertions(+), 35 deletions(-) create mode 100644 exercises/php/cards/Animals.php diff --git a/exercises/php/cards/Animal.php b/exercises/php/cards/Animal.php index adde2aa..bb44404 100644 --- a/exercises/php/cards/Animal.php +++ b/exercises/php/cards/Animal.php @@ -4,33 +4,17 @@ class Animal { - public array $animal = [ - "AARDVARK", - "BABOON", - "CAMEL", - "DEER", - "ELEPHANT", - "FROG", - "GORILLA", - "HARE", - "IMPALA", - "JAGUAR", - "KANGAROO", - "LION", - "MOOSE", - "NEWT", - "OCTOPUS", - "PENGUIN", - "QUETZAL", - "RABBIT", - "SALMON", - "TORTOISE", - "UAKARIS", - "VAQUITA", - "WHALE", - "X_RAY_TETRA", - "YAK", - "ZEBRA" - ]; + private string $animalName; + + public function __construct(string $animalName) + { + $this->animalName = $animalName; + } + + public function __toString(): string + { + return $this->animalName; + } + } \ No newline at end of file diff --git a/exercises/php/cards/AnimalCard.php b/exercises/php/cards/AnimalCard.php index a0651ab..a0d7dbf 100644 --- a/exercises/php/cards/AnimalCard.php +++ b/exercises/php/cards/AnimalCard.php @@ -6,16 +6,16 @@ class AnimalCard implements Card { - private string $animal; + private Animal $animal; - public function __construct(string $animal) + public function __construct(Animal $animal) { $this->animal = $animal; } public function __toString(): string { - return $this->animal; + return strval($this->animal); } public function snap(?Card $card): bool diff --git a/exercises/php/cards/AnimalDeck.php b/exercises/php/cards/AnimalDeck.php index 67c888d..552736b 100644 --- a/exercises/php/cards/AnimalDeck.php +++ b/exercises/php/cards/AnimalDeck.php @@ -10,10 +10,9 @@ public function __construct() { $this->cards = []; - $cAnimal = new Animal(); - foreach ($cAnimal->animal as $animal) { - $this->cards[] = new AnimalCard($animal); - $this->cards[] = new AnimalCard($animal); + foreach (Animals::ANIMALS as $animal) { + $this->cards[] = new AnimalCard(new Animal($animal)); + $this->cards[] = new AnimalCard(new Animal($animal)); } } diff --git a/exercises/php/cards/Animals.php b/exercises/php/cards/Animals.php new file mode 100644 index 0000000..a7b54e0 --- /dev/null +++ b/exercises/php/cards/Animals.php @@ -0,0 +1,36 @@ + Date: Mon, 21 Nov 2022 17:37:23 +0000 Subject: [PATCH 3/3] fix: failing tests --- exercises/php/tests/cards/AnimalDeckTest.php | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/exercises/php/tests/cards/AnimalDeckTest.php b/exercises/php/tests/cards/AnimalDeckTest.php index 0b3b0ea..878b37b 100644 --- a/exercises/php/tests/cards/AnimalDeckTest.php +++ b/exercises/php/tests/cards/AnimalDeckTest.php @@ -32,10 +32,9 @@ public function testShouldShuffleCardsInAnyOrder(): void protected function setUp(): void { - $cAnimal = new Animal(); - foreach ($cAnimal->animal as $animal) { - $this->testCards[] = new AnimalCard($animal); - $this->testCards[] = new AnimalCard($animal); + foreach (Animals::ANIMALS as $animal) { + $this->testCards[] = new AnimalCard(new Animal($animal)); + $this->testCards[] = new AnimalCard(new Animal($animal)); } }