From e0ab06959b66cc16808846f8dd9eecc642288c0c Mon Sep 17 00:00:00 2001 From: Kacper Majczak Date: Mon, 12 Sep 2022 13:05:01 +0200 Subject: [PATCH] Use consts and change way of assigning properties --- php/src/GildedRose.php | 40 ++++++++++++++++++++++++---------------- 1 file changed, 24 insertions(+), 16 deletions(-) diff --git a/php/src/GildedRose.php b/php/src/GildedRose.php index cfb7f78a..d025968b 100644 --- a/php/src/GildedRose.php +++ b/php/src/GildedRose.php @@ -6,6 +6,9 @@ namespace GildedRose; final class GildedRose { + private const SULFURAS_HAND_OF_RAGNAROS = 'Sulfuras, Hand of Ragnaros'; + private const BACKSTAGE_PASSES_TO_A_TAFKAL_80_ETC_CONCERT = 'Backstage passes to a TAFKAL80ETC concert'; + private const AGED_BRIE = 'Aged Brie'; /** * @var Item[] */ @@ -19,51 +22,56 @@ 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->name != self::AGED_BRIE && $item->name != self::BACKSTAGE_PASSES_TO_A_TAFKAL_80_ETC_CONCERT) { if ($item->quality > 0) { - if ($item->name != 'Sulfuras, Hand of Ragnaros') { - $item->quality = $item->quality - 1; + if ($item->name != self::SULFURAS_HAND_OF_RAGNAROS) { + $item->quality -= 1; } } } else { if ($item->quality < 50) { - $item->quality = $item->quality + 1; - if ($item->name == 'Backstage passes to a TAFKAL80ETC concert') { + $item->quality += 1; + if ($item->name == self::BACKSTAGE_PASSES_TO_A_TAFKAL_80_ETC_CONCERT) { if ($item->sell_in < 11) { if ($item->quality < 50) { - $item->quality = $item->quality + 1; + $item->quality += 1; } } if ($item->sell_in < 6) { if ($item->quality < 50) { - $item->quality = $item->quality + 1; + $item->quality += 1; } } } } } - if ($item->name != 'Sulfuras, Hand of Ragnaros') { - $item->sell_in = $item->sell_in - 1; + if ($item->name != self::SULFURAS_HAND_OF_RAGNAROS) { + $item->sell_in -= 1; } - if ($item->sell_in < 0) { - if ($item->name != 'Aged Brie') { - if ($item->name != 'Backstage passes to a TAFKAL80ETC concert') { + if ($this->isSellDatePassed($item)) { + if ($item->name != self::AGED_BRIE) { + if ($item->name != self::BACKSTAGE_PASSES_TO_A_TAFKAL_80_ETC_CONCERT) { if ($item->quality > 0) { - if ($item->name != 'Sulfuras, Hand of Ragnaros') { - $item->quality = $item->quality - 1; + if ($item->name != self::SULFURAS_HAND_OF_RAGNAROS) { + $item->quality -= 1; } } } else { - $item->quality = $item->quality - $item->quality; + $item->quality = 0; } } else { if ($item->quality < 50) { - $item->quality = $item->quality + 1; + $item->quality += 1; } } } } } + + private function isSellDatePassed($item): bool + { + return $item->sell_in < 0; + } }