Eliminate if statements using Guard Clause

This commit is contained in:
Kacper Majczak 2022-09-12 13:47:06 +02:00
parent c10371164c
commit 795c150ab5
2 changed files with 46 additions and 41 deletions

View File

@ -22,57 +22,38 @@ final class GildedRose
public function updateQuality(): void
{
foreach ($this->items as $item) {
if ($item->name != self::SULFURAS_HAND_OF_RAGNAROS) {
$item->sell_in -= 1;
if ($item->name !== self::SULFURAS_HAND_OF_RAGNAROS) {
$item->decreaseSellDate();
}
if ($item->name != self::AGED_BRIE && $item->name != self::BACKSTAGE_PASSES_TO_A_TAFKAL_80_ETC_CONCERT) {
if ($item->name != self::SULFURAS_HAND_OF_RAGNAROS) {
$this->decreaseItemQuality($item);
if ($this->isSellDatePassed($item)) {
$this->decreaseItemQuality($item);
}
}
} else {
$this->increaseItemQuality($item);
if ($item->name == self::BACKSTAGE_PASSES_TO_A_TAFKAL_80_ETC_CONCERT) {
if ($item->sell_in <= 10) {
$this->increaseItemQuality($item);
}
if ($item->sell_in <= 5) {
$this->increaseItemQuality($item);
}
if ($item->name === self::AGED_BRIE) {
$item->increaseQuality();
if ($item->isSellDatePassed()) {
$item->increaseQuality();
}
}
if ($this->isSellDatePassed($item)) {
if ($item->name != self::AGED_BRIE) {
if ($item->name === self::BACKSTAGE_PASSES_TO_A_TAFKAL_80_ETC_CONCERT) {
$item->quality = 0;
}
} else {
$this->increaseItemQuality($item);
if ($item->name === self::BACKSTAGE_PASSES_TO_A_TAFKAL_80_ETC_CONCERT) {
$item->increaseQuality();
if ($item->sell_in <= 10) {
$item->increaseQuality();
}
if ($item->sell_in <= 5) {
$item->increaseQuality();
}
}
}
}
private function isSellDatePassed(Item $item): bool
{
return $item->sell_in < 0;
}
private function increaseItemQuality(Item $item): void
{
if ($item->quality < 50) {
$item->quality += 1;
}
}
if (in_array($item->name, [self::AGED_BRIE, self::BACKSTAGE_PASSES_TO_A_TAFKAL_80_ETC_CONCERT, self::SULFURAS_HAND_OF_RAGNAROS]) === false) {
$item->decreaseQuality();
if ($item->isSellDatePassed()) {
$item->decreaseQuality();
}
}
private function decreaseItemQuality(Item $item): void
{
if ($item->quality > 0) {
$item->quality -= 1;
if ($item->isSellDatePassed() && $item->name === self::BACKSTAGE_PASSES_TO_A_TAFKAL_80_ETC_CONCERT) {
$item->quality = 0;
}
}
}
}

View File

@ -32,4 +32,28 @@ final class Item
{
return "{$this->name}, {$this->sell_in}, {$this->quality}";
}
public function increaseQuality(): void
{
if ($this->quality < 50) {
$this->quality += 1;
}
}
public function decreaseQuality(): void
{
if ($this->quality > 0) {
$this->quality -= 1;
}
}
public function isSellDatePassed(): bool
{
return $this->sell_in < 0;
}
public function decreaseSellDate(): void
{
$this->sell_in -= 1;
}
}