Completed
Push — GildedRose/php ( 8e5d5b )
by
unknown
04:03
created

GildedRose::update_quality()   D

Complexity

Conditions 19
Paths 197

Size

Total Lines 49
Code Lines 29

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 15
CRAP Score 50.6832

Importance

Changes 0
Metric Value
dl 0
loc 49
ccs 15
cts 27
cp 0.5556
rs 4.787
c 0
b 0
f 0
cc 19
eloc 29
nc 197
nop 0
crap 50.6832

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace Kata\GildedRoseKata;
4
5
class GildedRose {
6
7
    private $items;
8
9 1
    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...
10 1
        $this->items = $items;
11 1
    }
12
13 1
    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...
14 1
        foreach ($this->items as $item) {
15 1
            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...
16 1
                if ($item->quality > 0) {
17
                    if ($item->name != 'Sulfuras, Hand of Ragnaros') {
18 1
                        $item->quality = $item->quality - 1;
19
                    }
20
                }
21
            } else {
22
                if ($item->quality < 50) {
23
                    $item->quality = $item->quality + 1;
24
                    if ($item->name == 'Backstage passes to a TAFKAL80ETC concert') {
25
                        if ($item->sell_in < 11) {
26
                            if ($item->quality < 50) {
27
                                $item->quality = $item->quality + 1;
28
                            }
29
                        }
30
                        if ($item->sell_in < 6) {
31
                            if ($item->quality < 50) {
32
                                $item->quality = $item->quality + 1;
33
                            }
34
                        }
35
                    }
36
                }
37
            }
38
39 1
            if ($item->name != 'Sulfuras, Hand of Ragnaros') {
40 1
                $item->sell_in = $item->sell_in - 1;
41
            }
42
43 1
            if ($item->sell_in < 0) {
44 1
                if ($item->name != 'Aged Brie') {
45 1
                    if ($item->name != 'Backstage passes to a TAFKAL80ETC concert') {
46 1
                        if ($item->quality > 0) {
47
                            if ($item->name != 'Sulfuras, Hand of Ragnaros') {
48 1
                                $item->quality = $item->quality - 1;
49
                            }
50
                        }
51
                    } else {
52 1
                        $item->quality = $item->quality - $item->quality;
53
                    }
54
                } else {
55
                    if ($item->quality < 50) {
56 1
                        $item->quality = $item->quality + 1;
57
                    }
58
                }
59
            }
60
        }
61 1
    }
62
}
63
64
65