php: 7.1 or above
composer require prodigeris/php-gilded-rose-refactoring-kata
If you are not using containers:
<?php
$sulfuras = new \GildedRose\Item('Sulfuras, Hand of Ragnaros', 80, 2);
$regularItem = new \GildedRose\Item('AK-47, Hand of Ragnaros', 10, 2);
$backstagePass = new \GildedRose\Item('Backstage passes to a TAFKAL80ETC concert', 10, 2);
$items = compact('sulfuras', 'regularItem', 'backstagePass');
$productFactoryRegistry = new \GildedRose\ProductFactoryRegistry();
$productFactoryRegistry->register(\GildedRose\Products\RegularProduct::class);
$productFactoryRegistry->register(\GildedRose\Products\Sulfuras::class);
$productFactoryRegistry->register(\GildedRose\Products\BackstagePass::class);
$productFactory = new \GildedRose\ProductFactory($productFactoryRegistry);
$gildedRose = new \GildedRose\GildedRose($items, $productFactory);
$gildedRose->updateQuality();
Just run phpunit
to execute all the tests.
This is my PHP solution to Gilded Rose Refactoring Kata by Terry Hughes.
It was quite a fun; I have managed to use a couple of design patterns.
- Abstract Factory Pattern to build
Products
(implementations ofItems
) - Decorator Pattern for
Products
to modifyItem
values. - Command Pattern to modify
Item
values according to rules.
Everything about the Product
can be set dynamically.
- Traits
HasDayRangeMultiplier
. If the quality of the product changes differently over time. Used by default.Expires
. If the quality of the product goes to zero after the sale date. (e.g.BackstagePass
)
- Properties
- const
name
. The name by whichProductFactory
identifies theProduct
. Default:empty
max_quality
. Determines max value of the quality. Default:50
min_quality
. Determines min value of the quality. Default:0
quality_step
. Determines the direction and speed of quality change over time. Default:-1
day_range_multiplier
. Determines how quality acts depending on the days. Default:[0 => 2]
which means that after the sale date the quality step is twice as large.
- const
- Public methods
update
. UpdatesItem
for the new day.getItem
. ReturnsItem
assigned to the product.getMaxQuality
getMinQuality
getQualityStep
. Returns the quality step taking multipliers in mind.getNewQuality
. Returns the quality for the next day. Warning: It does not take rules in mind. Rules are checked on property change.isAfterSale
. Returns if the product has passed the sale date
If you want to introduce a new product, first create a class in Products
directory.
E.g.
<?php
namespace GildedRose\Products;
use GildedRose\Product;
/**
* Class Conjured
*
* @package \GildedRose\Products
*/
class Conjured extends Product
{
/**
* The name of the product
*/
const NAME = 'Conjured Mana Cake';
/**
* Quality increases by 2 over time
*
* @var int
*/
protected static $quality_step = -2;
}
Then register in the container of ProductFactoryRegistry
.
$productFactoryRegistry->register(Conjured::class);
VoilĂ !