mirror of
https://github.com/emilybache/GildedRose-Refactoring-Kata.git
synced 2026-02-18 07:51:29 +00:00
refactor of handleItemRules
This commit is contained in:
parent
34885ec017
commit
e818672101
@ -19,8 +19,7 @@
|
||||
"phpstan/phpstan": "^0.12.85",
|
||||
"phpstan/phpstan-phpunit": "^0.12.18",
|
||||
"symplify/easy-coding-standard": "^9.3",
|
||||
"symplify/phpstan-extensions": "^9.3",
|
||||
"approvals/approval-tests": "^1.4"
|
||||
"symplify/phpstan-extensions": "^9.3"
|
||||
},
|
||||
"scripts": {
|
||||
"checkcode": "phpcs src tests --standard=PSR12",
|
||||
|
||||
3274
php/composer.lock
generated
3274
php/composer.lock
generated
File diff suppressed because it is too large
Load Diff
@ -18,13 +18,12 @@ $items = [
|
||||
new Item('Backstage passes to a TAFKAL80ETC concert', 15, 20),
|
||||
new Item('Backstage passes to a TAFKAL80ETC concert', 10, 49),
|
||||
new Item('Backstage passes to a TAFKAL80ETC concert', 5, 49),
|
||||
// this conjured item does not work properly yet
|
||||
new Item('Conjured Mana Cake', 3, 6),
|
||||
];
|
||||
|
||||
$app = new GildedRose($items);
|
||||
|
||||
$days = 2;
|
||||
$days = 10;
|
||||
if (count($argv) > 1) {
|
||||
$days = (int) $argv[1];
|
||||
}
|
||||
|
||||
@ -4,6 +4,8 @@ declare(strict_types=1);
|
||||
|
||||
namespace GildedRose;
|
||||
|
||||
use Item;
|
||||
|
||||
final class GildedRose
|
||||
{
|
||||
/**
|
||||
@ -11,22 +13,80 @@ final class GildedRose
|
||||
*/
|
||||
private $items;
|
||||
|
||||
/**
|
||||
* @var int
|
||||
*/
|
||||
private $qualityModifier = -1;
|
||||
|
||||
/**
|
||||
* @var int
|
||||
*/
|
||||
private $minQuality = 0;
|
||||
|
||||
/**
|
||||
* @var int
|
||||
*/
|
||||
private $maxQuality = 50;
|
||||
|
||||
/**
|
||||
* @var Array
|
||||
*/
|
||||
private $itemTypes;
|
||||
|
||||
public function __construct(array $items)
|
||||
{
|
||||
$this->items = $items;
|
||||
|
||||
// This could be collected from a database or stored on the item
|
||||
$this->itemTypes = ['aged', 'conjured', 'backstagePasses', 'legendary'];
|
||||
}
|
||||
|
||||
public function updateQuality(): void
|
||||
{
|
||||
// all this config could be stored on the item or collected from a database
|
||||
$ruleItems = [
|
||||
'Aged Brie' => 'aged',
|
||||
'Conjured Mana Cake' => 'conjured',
|
||||
'Backstage passes to a TAFKAL80ETC concert' => 'backstagePasses',
|
||||
'Sulfuras, Hand of Ragnaros' => 'legendary',
|
||||
];
|
||||
|
||||
// here i would set up a class for each type of item, it store validation in a database
|
||||
// potentially a global rules class might suit this
|
||||
$agedRules = [
|
||||
'quality' => 1,
|
||||
'minQuality' => -1,
|
||||
];
|
||||
$conjuredRules = [
|
||||
'quality' => -2
|
||||
];
|
||||
$backstagePassesRules = [
|
||||
'quality' => [
|
||||
'sell_in' => [
|
||||
1 => 0,
|
||||
6 => 3,
|
||||
11 => 2,
|
||||
'default' => 1,
|
||||
],
|
||||
],
|
||||
];
|
||||
$legendaryRules = [];
|
||||
|
||||
$specialItems = [];
|
||||
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 (isset($ruleItems[$item->name])) {
|
||||
$ruleArrayName = $ruleItems[$item->name] . "Rules";
|
||||
$item = $this->handleItemRules($item, $$ruleArrayName);
|
||||
} else {
|
||||
if ($item->quality < 50) {
|
||||
$qualityModifier = ($item->sell_in < 0) ? $this->qualityModifier * 2: $this->qualityModifier;
|
||||
if ($item->quality > $this->minQuality) {
|
||||
$item->quality = $item->quality + $this->qualityModifier;
|
||||
}
|
||||
|
||||
$item->sell_in = $item->sell_in - 1;
|
||||
}
|
||||
|
||||
/*if ($item->quality < 50) {
|
||||
$item->quality = $item->quality + 1;
|
||||
if ($item->name == 'Backstage passes to a TAFKAL80ETC concert') {
|
||||
if ($item->sell_in < 11) {
|
||||
@ -40,30 +100,47 @@ final class GildedRose
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if ($item->name != 'Sulfuras, Hand of Ragnaros') {
|
||||
$item->sell_in = $item->sell_in - 1;
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
}*/
|
||||
}
|
||||
}
|
||||
|
||||
private function handleItemRules(Object $item, Array $rules)
|
||||
{
|
||||
if (!empty($rules)) {
|
||||
if (isset($rules['quality'])) {
|
||||
if (is_array($rules['quality'])) {
|
||||
foreach ($rules['quality']['sell_in'] as $expiry => $ruleQuantityModifier) {
|
||||
if ($item->sell_in < $expiry) {
|
||||
if ($ruleQuantityModifier == 0) {
|
||||
$qualityModifier = -$item->quality;
|
||||
} else {
|
||||
$qualityModifier = $ruleQuantityModifier;
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
$qualityModifier = $rules['quality'];
|
||||
}
|
||||
}
|
||||
|
||||
// make sure default is set
|
||||
$qualityModifier = $qualityModifier ?? $this->qualityModifier;
|
||||
|
||||
$qualityModifier = ($item->sell_in < 1) ? $qualityModifier * 2: $qualityModifier;
|
||||
|
||||
$minQuality = $rules['minQuality'] ?? $this->minQuality;
|
||||
$maxQuality = $rules['maxQuality'] ?? $this->maxQuality;
|
||||
|
||||
$item->quality += $qualityModifier;
|
||||
if ($item->quality > $maxQuality) {
|
||||
$item->quality = $maxQuality;
|
||||
} else if ($item->quality < $minQuality) {
|
||||
$item->quality = $minQuality;
|
||||
}
|
||||
|
||||
$item->sell_in = $item->sell_in - 1;
|
||||
}
|
||||
return $item;
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user