mirror of
https://github.com/emilybache/GildedRose-Refactoring-Kata.git
synced 2026-02-16 06:51:27 +00:00
Refactor
- Add different item classes - Separate logic of updateQuality - Modify tests
This commit is contained in:
parent
af972b4009
commit
9cc121e80c
@ -24,39 +24,7 @@ final class GildedRose
|
||||
public function updateQuality(): void
|
||||
{
|
||||
foreach ($this->items as $item) {
|
||||
if ($item->name === 'Sulfuras, Hand of Ragnaros') {
|
||||
continue;
|
||||
}
|
||||
|
||||
if ($item->name === 'Aged Brie' || $item->name === 'Backstage passes to a TAFKAL80ETC concert') {
|
||||
if ($item->quality < 50) {
|
||||
$item->quality = $item->quality + 1;
|
||||
if ($item->name === 'Backstage passes to a TAFKAL80ETC concert') {
|
||||
if ($item->sellIn < 11) {
|
||||
$item->increaseQuality();
|
||||
}
|
||||
if ($item->sellIn < 6) {
|
||||
$item->increaseQuality();
|
||||
}
|
||||
}
|
||||
}
|
||||
} else {
|
||||
$item->decreaseQuality();
|
||||
}
|
||||
|
||||
$item->sellIn = $item->sellIn - 1;
|
||||
|
||||
if ($item->sellIn < 0) {
|
||||
if ($item->name === 'Aged Brie') {
|
||||
$item->increaseQuality();
|
||||
} else {
|
||||
if ($item->name === 'Backstage passes to a TAFKAL80ETC concert') {
|
||||
$item->quality = $item->quality - $item->quality;
|
||||
} else {
|
||||
$item->decreaseQuality();
|
||||
}
|
||||
}
|
||||
}
|
||||
$item->update();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -3,8 +3,7 @@
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace GildedRose;
|
||||
|
||||
final class Item
|
||||
abstract class Item
|
||||
{
|
||||
public string $name;
|
||||
|
||||
@ -24,7 +23,9 @@ final class Item
|
||||
return "{$this->name}, {$this->sellIn}, {$this->quality}";
|
||||
}
|
||||
|
||||
public function increaseQuality(): void
|
||||
abstract public function update();
|
||||
|
||||
protected function increaseQuality(): void
|
||||
{
|
||||
if ($this->quality >= 50) {
|
||||
return;
|
||||
@ -32,7 +33,7 @@ final class Item
|
||||
$this->quality += 1;
|
||||
}
|
||||
|
||||
public function decreaseQuality(): void
|
||||
protected function decreaseQuality(): void
|
||||
{
|
||||
if ($this->quality <= 0) {
|
||||
return;
|
||||
|
||||
19
php/src/Item/AgedBrieItem.php
Normal file
19
php/src/Item/AgedBrieItem.php
Normal file
@ -0,0 +1,19 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace GildedRose\Item;
|
||||
|
||||
use GildedRose\Item;
|
||||
|
||||
final class AgedBrieItem extends Item
|
||||
{
|
||||
public function update()
|
||||
{
|
||||
$this->increaseQuality();
|
||||
$this->sellIn -= 1;
|
||||
if ($this->sellIn < 0) {
|
||||
$this->increaseQuality();
|
||||
}
|
||||
}
|
||||
}
|
||||
26
php/src/Item/BackstagePassItem.php
Normal file
26
php/src/Item/BackstagePassItem.php
Normal file
@ -0,0 +1,26 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace GildedRose\Item;
|
||||
|
||||
use GildedRose\Item;
|
||||
|
||||
final class BackstagePassItem extends Item
|
||||
{
|
||||
public function update()
|
||||
{
|
||||
$this->sellIn -= 1;
|
||||
$this->increaseQuality();
|
||||
if ($this->sellIn < 10) {
|
||||
$this->increaseQuality();
|
||||
}
|
||||
if ($this->sellIn < 5) {
|
||||
$this->increaseQuality();
|
||||
}
|
||||
if ($this->sellIn < 0) {
|
||||
$this->quality = 0;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
19
php/src/Item/NormalItem.php
Normal file
19
php/src/Item/NormalItem.php
Normal file
@ -0,0 +1,19 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace GildedRose\Item;
|
||||
|
||||
use GildedRose\Item;
|
||||
|
||||
final class NormalItem extends Item
|
||||
{
|
||||
public function update()
|
||||
{
|
||||
$this->decreaseQuality();
|
||||
$this->sellIn = $this->sellIn - 1;
|
||||
if ($this->sellIn < 0) {
|
||||
$this->decreaseQuality();
|
||||
}
|
||||
}
|
||||
}
|
||||
12
php/src/Item/SulfurasItem.php
Normal file
12
php/src/Item/SulfurasItem.php
Normal file
@ -0,0 +1,12 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace GildedRose\Item;
|
||||
|
||||
use GildedRose\Item;
|
||||
|
||||
final class SulfurasItem extends Item
|
||||
{
|
||||
public function update() {}
|
||||
}
|
||||
@ -5,14 +5,17 @@ declare(strict_types=1);
|
||||
namespace Tests;
|
||||
|
||||
use GildedRose\GildedRose;
|
||||
use GildedRose\Item;
|
||||
use GildedRose\Item\AgedBrieItem;
|
||||
use GildedRose\Item\BackstagePassItem;
|
||||
use GildedRose\Item\NormalItem;
|
||||
use GildedRose\Item\SulfurasItem;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
class GildedRoseTest extends TestCase
|
||||
{
|
||||
public function testQualityNeverIsNegative(): void
|
||||
{
|
||||
$items = [new Item("foo", 0, 0)];
|
||||
$items = [new NormalItem("foo", 0, 0)];
|
||||
$app = new GildedRose($items);
|
||||
|
||||
$app->updateQuality();
|
||||
@ -22,7 +25,7 @@ class GildedRoseTest extends TestCase
|
||||
|
||||
public function testSulfurasCouldNotBeSold(): void
|
||||
{
|
||||
$items = [new Item("Sulfuras, Hand of Ragnaros", 10, 0)];
|
||||
$items = [new SulfurasItem("Sulfuras, Hand of Ragnaros", 10, 0)];
|
||||
$app = new GildedRose($items);
|
||||
|
||||
$app->updateQuality();
|
||||
@ -32,7 +35,7 @@ class GildedRoseTest extends TestCase
|
||||
|
||||
public function testSulfurasCouldNotDecreaseQuality(): void
|
||||
{
|
||||
$items = [new Item("Sulfuras, Hand of Ragnaros", 10, 10)];
|
||||
$items = [new SulfurasItem("Sulfuras, Hand of Ragnaros", 10, 10)];
|
||||
$app = new GildedRose($items);
|
||||
|
||||
$app->updateQuality();
|
||||
@ -42,7 +45,7 @@ class GildedRoseTest extends TestCase
|
||||
|
||||
public function testQualityCouldNotBeMoreThanFifty(): void
|
||||
{
|
||||
$items = [new Item("Aged Brie", 10, 50)];
|
||||
$items = [new AgedBrieItem("Aged Brie", 10, 50)];
|
||||
$app = new GildedRose($items);
|
||||
|
||||
$app->updateQuality();
|
||||
@ -52,7 +55,7 @@ class GildedRoseTest extends TestCase
|
||||
|
||||
public function testItemWithDatePassedQualityDecreaseByTwice(): void
|
||||
{
|
||||
$items = [new Item("foo", -1, 40)];
|
||||
$items = [new NormalItem("foo", -1, 40)];
|
||||
$app = new GildedRose($items);
|
||||
|
||||
$app->updateQuality();
|
||||
@ -62,7 +65,7 @@ class GildedRoseTest extends TestCase
|
||||
|
||||
public function testAgedBrieIncreaseQualityWhenItGetsOlder(): void
|
||||
{
|
||||
$items = [new Item("Aged Brie", 1, 40)];
|
||||
$items = [new AgedBrieItem("Aged Brie", 1, 40)];
|
||||
$app = new GildedRose($items);
|
||||
|
||||
$app->updateQuality();
|
||||
@ -72,7 +75,7 @@ class GildedRoseTest extends TestCase
|
||||
|
||||
public function testAgedBrieIncreaseByTwoQualityWhenDatePassed(): void
|
||||
{
|
||||
$items = [new Item("Aged Brie", -1, 40)];
|
||||
$items = [new AgedBrieItem("Aged Brie", -1, 40)];
|
||||
$app = new GildedRose($items);
|
||||
|
||||
$app->updateQuality();
|
||||
@ -81,7 +84,7 @@ class GildedRoseTest extends TestCase
|
||||
}
|
||||
|
||||
public function testAgedBrieIncreaseByTwoQualityWhenDatePassedAndNotMoreThanFifty() {
|
||||
$items = [new Item("Aged Brie", -1, 50)];
|
||||
$items = [new AgedBrieItem("Aged Brie", -1, 50)];
|
||||
$app = new GildedRose($items);
|
||||
|
||||
$app->updateQuality();
|
||||
@ -90,7 +93,7 @@ class GildedRoseTest extends TestCase
|
||||
}
|
||||
|
||||
public function testBackstagePassesIncreaseQualityByTwoWhenSelinLessThanTen() {
|
||||
$items = [new Item("Backstage passes to a TAFKAL80ETC concert", 10, 40)];
|
||||
$items = [new BackstagePassItem("Backstage passes to a TAFKAL80ETC concert", 10, 40)];
|
||||
$app = new GildedRose($items);
|
||||
|
||||
$app->updateQuality();
|
||||
@ -99,7 +102,7 @@ class GildedRoseTest extends TestCase
|
||||
}
|
||||
|
||||
public function testBackstagePassesIncreaseQualityByTwoWhenSellinLessThanSix() {
|
||||
$items = [new Item("Backstage passes to a TAFKAL80ETC concert", 6, 40)];
|
||||
$items = [new BackstagePassItem("Backstage passes to a TAFKAL80ETC concert", 6, 40)];
|
||||
$app = new GildedRose($items);
|
||||
|
||||
$app->updateQuality();
|
||||
@ -108,7 +111,7 @@ class GildedRoseTest extends TestCase
|
||||
}
|
||||
|
||||
public function testBackstagePassesIncreaseQualityByThreeWhenSellinLessThanFive() {
|
||||
$items = [new Item("Backstage passes to a TAFKAL80ETC concert", 5, 40)];
|
||||
$items = [new BackstagePassItem("Backstage passes to a TAFKAL80ETC concert", 5, 40)];
|
||||
$app = new GildedRose($items);
|
||||
|
||||
$app->updateQuality();
|
||||
@ -117,7 +120,7 @@ class GildedRoseTest extends TestCase
|
||||
}
|
||||
|
||||
public function testBackstagePassesIncreaseQualityByTwoWhenSellinLessThanSixAndNotMoreThanFifty() {
|
||||
$items = [new Item("Backstage passes to a TAFKAL80ETC concert", 6, 49)];
|
||||
$items = [new BackstagePassItem("Backstage passes to a TAFKAL80ETC concert", 6, 49)];
|
||||
$app = new GildedRose($items);
|
||||
|
||||
$app->updateQuality();
|
||||
@ -126,7 +129,7 @@ class GildedRoseTest extends TestCase
|
||||
}
|
||||
|
||||
public function testBackstagePassesIncreaseQualityByThreeWhenSellinLessThanFiveAndNotMoreThanFifty() {
|
||||
$items = [new Item("Backstage passes to a TAFKAL80ETC concert", 5, 48)];
|
||||
$items = [new BackstagePassItem("Backstage passes to a TAFKAL80ETC concert", 5, 48)];
|
||||
$app = new GildedRose($items);
|
||||
|
||||
$app->updateQuality();
|
||||
@ -135,7 +138,7 @@ class GildedRoseTest extends TestCase
|
||||
}
|
||||
|
||||
public function testBackstagePassesQualityDropsToZeroAfterConcert() {
|
||||
$items = [new Item("Backstage passes to a TAFKAL80ETC concert", 0, 40)];
|
||||
$items = [new BackstagePassItem("Backstage passes to a TAFKAL80ETC concert", 0, 40)];
|
||||
$app = new GildedRose($items);
|
||||
|
||||
$app->updateQuality();
|
||||
@ -144,7 +147,7 @@ class GildedRoseTest extends TestCase
|
||||
}
|
||||
|
||||
public function testBackstagePassesQualityIncreaseQualityByOneWhenDateIsMoreThanTen() {
|
||||
$items = [new Item("Backstage passes to a TAFKAL80ETC concert", 11, 40)];
|
||||
$items = [new BackstagePassItem("Backstage passes to a TAFKAL80ETC concert", 11, 40)];
|
||||
$app = new GildedRose($items);
|
||||
|
||||
$app->updateQuality();
|
||||
|
||||
Loading…
Reference in New Issue
Block a user