mirror of
https://github.com/emilybache/GildedRose-Refactoring-Kata.git
synced 2026-02-10 12:11:20 +00:00
feat: conjured implementation
This commit is contained in:
parent
2f93d81eed
commit
d32604ed1a
@ -11,57 +11,102 @@ final class GildedRose
|
|||||||
*/
|
*/
|
||||||
public function __construct(
|
public function __construct(
|
||||||
private array $items
|
private array $items
|
||||||
) {
|
)
|
||||||
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
public function updateQuality(): void
|
public function updateQuality(): void
|
||||||
{
|
{
|
||||||
foreach ($this->items as $item) {
|
foreach ($this->items as $item) {
|
||||||
if ($item->name != 'Aged Brie' and $item->name != 'Backstage passes to a TAFKAL80ETC concert') {
|
$this->updateItemQuality($item);
|
||||||
if ($item->quality > 0) {
|
|
||||||
if ($item->name != 'Sulfuras, Hand of Ragnaros') {
|
|
||||||
$item->quality = $item->quality - 1;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
if ($item->quality < 50) {
|
|
||||||
$item->quality = $item->quality + 1;
|
|
||||||
if ($item->name == 'Backstage passes to a TAFKAL80ETC concert') {
|
|
||||||
if ($item->sellIn < 11) {
|
|
||||||
if ($item->quality < 50) {
|
|
||||||
$item->quality = $item->quality + 1;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if ($item->sellIn < 6) {
|
|
||||||
if ($item->quality < 50) {
|
|
||||||
$item->quality = $item->quality + 1;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if ($item->name != 'Sulfuras, Hand of Ragnaros') {
|
|
||||||
$item->sellIn = $item->sellIn - 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
if ($item->sellIn < 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;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private function updateItemQuality($item): void
|
||||||
|
{
|
||||||
|
if ($item->name == 'Sulfuras, Hand of Ragnaros') {
|
||||||
|
return; // Legendary item, no changes needed
|
||||||
|
}
|
||||||
|
|
||||||
|
$this->decreaseSellIn($item);
|
||||||
|
|
||||||
|
switch ($item->name) {
|
||||||
|
case 'Aged Brie':
|
||||||
|
$this->updateAgedBrie($item);
|
||||||
|
break;
|
||||||
|
case 'Backstage passes to a TAFKAL80ETC concert':
|
||||||
|
$this->updateBackstagePasses($item);
|
||||||
|
break;
|
||||||
|
case 'Conjured Mana Cake':
|
||||||
|
$this->updateConjured($item);
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
$this->updateNormalItem($item);
|
||||||
|
}
|
||||||
|
|
||||||
|
$this->ensureQualityInRange($item);
|
||||||
|
}
|
||||||
|
private function decreaseSellIn($item): void
|
||||||
|
{
|
||||||
|
$item->sellIn--;
|
||||||
|
}
|
||||||
|
|
||||||
|
private function updateAgedBrie($item): void
|
||||||
|
{
|
||||||
|
$this->increaseQuality($item);
|
||||||
|
if ($item->sellIn < 0) {
|
||||||
|
$this->increaseQuality($item);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private function updateBackstagePasses($item): void
|
||||||
|
{
|
||||||
|
$this->increaseQuality($item);
|
||||||
|
if ($item->sellIn < 10) {
|
||||||
|
$this->increaseQuality($item);
|
||||||
|
}
|
||||||
|
if ($item->sellIn < 5) {
|
||||||
|
$this->increaseQuality($item);
|
||||||
|
}
|
||||||
|
if ($item->sellIn < 0) {
|
||||||
|
$item->quality = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private function updateConjured($item): void
|
||||||
|
{
|
||||||
|
$this->decreaseQuality($item);
|
||||||
|
$this->decreaseQuality($item);
|
||||||
|
if ($item->sellIn < 0) {
|
||||||
|
$this->decreaseQuality($item);
|
||||||
|
$this->decreaseQuality($item);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private function updateNormalItem($item): void
|
||||||
|
{
|
||||||
|
$this->decreaseQuality($item);
|
||||||
|
if ($item->sellIn < 0) {
|
||||||
|
$this->decreaseQuality($item);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private function increaseQuality($item): void
|
||||||
|
{
|
||||||
|
if ($item->quality < 50) {
|
||||||
|
$item->quality++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private function decreaseQuality($item): void
|
||||||
|
{
|
||||||
|
if ($item->quality > 0) {
|
||||||
|
$item->quality--;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private function ensureQualityInRange($item): void
|
||||||
|
{
|
||||||
|
$item->quality = max(0, min(50, $item->quality));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user