This is the the refactored version of the original Gilded Rose Refactoring Kata in PHP. Package comes with created PHP unit tests, refactoring done and new feature added.
The Gilded Rose Requirements can be found here.
php-xml
. Just follow on screen instructions.To install the project you need:
git clone [email protected]:mantis4444/gilded-rose-refactoring-kata-php.git /desired/directory
.cd /desired/directory
.composer install
.Note that if you are doing it on Windows OS you need to use instead of
/
for directory separation.
texttest_fixture.php
file with the command php src/texttest_fixture.php
.src/texttest_fixture.php
file../vendor/bin/phpunit
.Note that these instructions assumes that you are in the project directory.
I kept the original coding style to adapt to current situation. Though I would have liked to use PSR standard and namespaces. As a result application supports original input, thus the texttest_fixture.php
file was not touched.
First of all I made tests to make sure that original code is working properly and suprisngly it was, despite its messy algorythm structure. Then refactoring process started. In the beginning I shrinked the update_quality()
function as much as possible. Code became much more readable, but there were still too many conditional if
and I did not see a way how I could minimize it more.
After doing some analysis I realised that Polymorphism is the solution. First of all I created enhanced item class called ItemEnhanced
which was extending original and untouchable Item
class to improve its functionality. The first challange was to enhance Items and support original instruction code. While original code is setting up items with Item
class I had to find a way to enhance items without changing it. I made it on the GildedRose app initialization constructor, enhancing Items while setting them. To make the original item object enhance too, I used reference links.
Now class ItemEnhanced
has the funcionality that all type of items share. On the next step I created class for every type of item that they would have their own special funcionality. They are extending the ItemEnhanced
class thus the original Item
also. Functions of the ItemEnhanced
that are being used only in those item type classes are protected for the accesibility in item type class only.
After refactoring is finished, tests are passing successfully. Now it was a time to add new funcionality. Having new structure of the application it was easy to do that. First I created new item type class for "Conjured" item with its specific rules. Then I created new tests for it.
Voila everything works like a charm, code is much more readable and flexible. I would say that the tests could be improved a lot. They could be much more specific than they are now.