prodigeris / php-gilded-rose-refactoring-kata
My PHP solution to GildedRose Refactoring Kata.
Installs: 25
Dependents: 0
Suggesters: 0
Security: 0
Stars: 2
Watchers: 1
Forks: 0
Open Issues: 0
Type:project
Requires
- php: ^7.1
Requires (Dev)
- phpunit/phpunit: ^7
This package is auto-updated.
Last update: 2024-06-05 18:55:06 UTC
README
Requirements
php: 7.1 or above
Installation
composer require prodigeris/php-gilded-rose-refactoring-kata
Usage
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();
Tests
Just run phpunit
to execute all the tests.
Description
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.
Product
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
New Product
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à!