mirror of
https://github.com/emilybache/GildedRose-Refactoring-Kata.git
synced 2026-02-15 22:41:30 +00:00
Improve code readability and update test
This commit is contained in:
parent
878f2da3aa
commit
abfd7796d7
@ -6,11 +6,21 @@ namespace GildedRose;
|
||||
|
||||
final class GildedRose
|
||||
{
|
||||
private const AGED_BRIE = 'Aged Brie';
|
||||
private const BACKSTAGE = 'Backstage passes to a TAFKAL80ETC concert';
|
||||
private const SHR = 'Sulfuras, Hand of Ragnaros';
|
||||
|
||||
private const MAX_THRESHOLD = 50;
|
||||
private const MIN_THRESHOLD = 0;
|
||||
|
||||
/**
|
||||
* @var Item[]
|
||||
*/
|
||||
private $items;
|
||||
private array $items;
|
||||
|
||||
/**
|
||||
* @param array $items
|
||||
*/
|
||||
public function __construct(array $items)
|
||||
{
|
||||
$this->items = $items;
|
||||
@ -19,51 +29,72 @@ final class GildedRose
|
||||
public function updateQuality(): void
|
||||
{
|
||||
foreach ($this->items as $item) {
|
||||
if ($item->name != 'Aged Brie' and $item->name != 'Backstage passes to a TAFKAL80ETC concert') {
|
||||
if ($item->quality > 0) {
|
||||
if ($item->name != 'Sulfuras, Hand of Ragnaros') {
|
||||
$item->quality = $item->quality - 1;
|
||||
if (!$this->isLegendaryItem($item)) {
|
||||
$item->quality = $this->subtractQuality($item);
|
||||
}
|
||||
|
||||
if ($this->qualityIncreases($item)) {
|
||||
$item->quality = $this->addQuality($item);
|
||||
|
||||
if ($item->name === self::BACKSTAGE) {
|
||||
if ($item->sell_in <= 10) {
|
||||
$item->quality = $this->addQuality($item);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
if ($item->quality < 50) {
|
||||
$item->quality = $item->quality + 1;
|
||||
if ($item->name == 'Backstage passes to a TAFKAL80ETC concert') {
|
||||
if ($item->sell_in < 11) {
|
||||
if ($item->quality < 50) {
|
||||
$item->quality = $item->quality + 1;
|
||||
}
|
||||
}
|
||||
if ($item->sell_in < 6) {
|
||||
if ($item->quality < 50) {
|
||||
$item->quality = $item->quality + 1;
|
||||
}
|
||||
}
|
||||
if ($item->sell_in <= 5) {
|
||||
$item->quality = $this->addQuality($item);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if ($item->name != 'Sulfuras, Hand of Ragnaros') {
|
||||
$item->sell_in = $item->sell_in - 1;
|
||||
if (!$this->isLegendaryItem($item)) {
|
||||
$item->sell_in = --$item->sell_in;
|
||||
}
|
||||
|
||||
if ($item->sell_in < 0) {
|
||||
if ($item->name != 'Aged Brie') {
|
||||
if ($item->name != 'Backstage passes to a TAFKAL80ETC concert') {
|
||||
if ($item->quality > 0) {
|
||||
if ($item->name != 'Sulfuras, Hand of Ragnaros') {
|
||||
$item->quality = $item->quality - 1;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
$item->quality = $item->quality - $item->quality;
|
||||
}
|
||||
} else {
|
||||
if ($item->quality < 50) {
|
||||
$item->quality = $item->quality + 1;
|
||||
}
|
||||
if ($this->sellDateReached($item)) {
|
||||
if ($item->name === self::AGED_BRIE) {
|
||||
$item->quality = $this->addQuality($item);
|
||||
continue;
|
||||
}
|
||||
|
||||
if ($item->name === self::BACKSTAGE) {
|
||||
// Quality of backstage passes drops to 0 when sell date has been reached
|
||||
$item->quality = 0;
|
||||
continue;
|
||||
}
|
||||
|
||||
if (!$this->isLegendaryItem($item)) {
|
||||
$item->quality = $this->subtractQuality($item);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private function qualityIncreases(Item $item): bool
|
||||
{
|
||||
return in_array($item->name, [self::AGED_BRIE, self::BACKSTAGE]);
|
||||
}
|
||||
|
||||
private function isLegendaryItem(Item $item): bool
|
||||
{
|
||||
return $item->name === self::SHR;
|
||||
}
|
||||
|
||||
private function sellDateReached(Item $item): bool
|
||||
{
|
||||
return $item->sell_in < 0;
|
||||
}
|
||||
|
||||
private function subtractQuality(Item $item): int
|
||||
{
|
||||
$quality = $item->quality;
|
||||
|
||||
return $quality > self::MIN_THRESHOLD ? --$quality : $quality;
|
||||
}
|
||||
|
||||
private function addQuality(Item $item): int
|
||||
{
|
||||
$quality = $item->quality;
|
||||
|
||||
return $quality < self::MAX_THRESHOLD ? ++$quality : $quality;
|
||||
}
|
||||
}
|
||||
|
||||
@ -12,9 +12,23 @@ class GildedRoseTest extends TestCase
|
||||
{
|
||||
public function testFoo(): void
|
||||
{
|
||||
$items = [new Item('foo', 0, 0)];
|
||||
$items = [
|
||||
new Item('+5 Dexterity Vest', 10, 20),
|
||||
new Item('Aged Brie', 2, 0),
|
||||
new Item('Sulfuras, Hand of Ragnaros', 0, 80),
|
||||
];
|
||||
|
||||
$gildedRose = new GildedRose($items);
|
||||
|
||||
$gildedRose->updateQuality();
|
||||
$this->assertSame('fixme', $items[0]->name);
|
||||
|
||||
$this->assertSame(9, $items[0]->sell_in);
|
||||
$this->assertSame(19, $items[0]->quality);
|
||||
|
||||
$this->assertSame(1, $items[1]->sell_in);
|
||||
$this->assertSame(1, $items[1]->quality);
|
||||
|
||||
$this->assertSame(0, $items[2]->sell_in);
|
||||
$this->assertSame(80, $items[2]->quality);
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user