Completed
Push — GildedRose/edu-dani ( e1f431 )
by Albert
04:13
created

GildedRose::factory_method()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 19
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 4

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 19
ccs 9
cts 9
cp 1
rs 9.2
cc 4
eloc 10
nc 4
nop 1
crap 4
1
<?php
2
3
namespace Kata\GildedRoseKata;
4
5
class GildedRose
6
{
7
8
    private $items;
9
10 14
    function __construct($items)
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
11
    {
12 14
        $this->items = $items;
13 14
    }
14
15 14
    function update_quality()
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
16
    {
17 14
        foreach ($this->items as $item)
18
        {
19 14
            $new_item = $this->factory_method($item);
0 ignored issues
show
Unused Code introduced by
$new_item is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
20
21 14
            if ($item->name != 'Aged Brie' and $item->name != 'Backstage passes to a TAFKAL80ETC concert')
0 ignored issues
show
Comprehensibility Best Practice introduced by
Using logical operators such as and instead of && is generally not recommended.

PHP has two types of connecting operators (logical operators, and boolean operators):

  Logical Operators Boolean Operator
AND - meaning and &&
OR - meaning or ||

The difference between these is the order in which they are executed. In most cases, you would want to use a boolean operator like &&, or ||.

Let’s take a look at a few examples:

// Logical operators have lower precedence:
$f = false or true;

// is executed like this:
($f = false) or true;


// Boolean operators have higher precedence:
$f = false || true;

// is executed like this:
$f = (false || true);

Logical Operators are used for Control-Flow

One case where you explicitly want to use logical operators is for control-flow such as this:

$x === 5
    or die('$x must be 5.');

// Instead of
if ($x !== 5) {
    die('$x must be 5.');
}

Since die introduces problems of its own, f.e. it makes our code hardly testable, and prevents any kind of more sophisticated error handling; you probably do not want to use this in real-world code. Unfortunately, logical operators cannot be combined with throw at this point:

// The following is currently a parse error.
$x === 5
    or throw new RuntimeException('$x must be 5.');

These limitations lead to logical operators rarely being of use in current PHP code.

Loading history...
22
            {
23 7
                $this->decrementQualityOneUnitIfItemNameIsNotSulfuras($item);
24
            }
25
            else
26
            {
27 7
                $this->incrementQuality($item);
28
            }
29
30
            if ($item->name != 'Sulfuras, Hand of Ragnaros')
31
            {
32
                $item->sell_in = $item->sell_in - 1;
33
            }
34
35
            if ($item->sell_in > 0)
36
            {
37
                continue;
38
            }
39
40
            if ($item->name == 'Aged Brie')
41
            {
42
                $this->increaseQualityByOneUnitWhenQualitySmallerThanFifty($item);
43
            }
44
45
            if ($item->name != 'Backstage passes to a TAFKAL80ETC concert')
46
            {
47
                $this->decrementQualityOneUnitIfItemNameIsNotSulfuras($item);
48
            }
49
            else
50
            {
51
                $item->quality = $item->quality - $item->quality;
52
            }
53
        }
54
    }
55
56 3
    public function increaseQualityByOneUnitWhenQualitySmallerThanFifty($item): void
57
    {
58 3
        if ($item->quality < 50)
59
        {
60 3
            $item->quality = $item->quality + 1;
61
        }
62 3
    }
63
64 7
    public function decrementQualityOneUnitIfItemNameIsNotSulfuras($item): void
65
    {
66 7
        if ($item->quality > 0)
67
        {
68 5
            if ($item->name != 'Sulfuras, Hand of Ragnaros')
69
            {
70 3
                $item->quality = $item->quality - 1;
71
            }
72
        }
73 7
    }
74
75 7
    public function incrementQuality($item): void
76
    {
77 7
        if ($item->quality >= 50)
78
        {
79 1
            return;
80
        }
81
82 6
        $item->quality = $item->quality + 1;
83
84 6
        if ($item->name !== 'Backstage passes to a TAFKAL80ETC concert')
85
        {
86 2
            return;
87
        }
88
89 4
        $this->incrementQualityForBackstagePasses($item);
90
    }
91
92 4
    private function incrementQualityForBackstagePasses($item): void
93
    {
94 4
        if ($item->sell_in < 11)
95
        {
96 3
            $this->increaseQualityByOneUnitWhenQualitySmallerThanFifty($item);
97
        }
98 1
        if ($item->sell_in < 6)
99
        {
100
            $this->increaseQualityByOneUnitWhenQualitySmallerThanFifty($item);
101
        }
102 1
    }
103
104 14
    private function factory_method($item)
105
    {
106 14
        if ($item->name == BackstagePassesItem::NAME)
107
        {
108 4
            $new_item = BackstagePassesItem::createBackstagePasses($item);
109
        }
110 10
        elseif ($item->name == AgedBrieItem::NAME)
111
        {
112 3
            $new_item = AgedBrieItem::createAgedBrieItem($item);
113
        }
114 7
        elseif ($item->name == SulfurasItem::NAME)
115
        {
116 2
            $new_item = SulfurasItem::createSulfurasItem($item);
117
        }else{
118 5
            $new_item = $item;
119
        }
120
121 14
        return $new_item;
122
    }
123
}
124
125
126