Passed
Push — behat ( a74de5...4872f1 )
by Doug
02:39
created

ItemList   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 99
Duplicated Lines 25.25 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 54.84%

Importance

Changes 0
Metric Value
wmc 11
lcom 1
cbo 1
dl 25
loc 99
ccs 17
cts 31
cp 0.5484
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A getIterator() 8 9 2
A insert() 0 4 1
A extract() 8 9 2
A top() 9 10 2
A count() 0 4 1
A compare() 0 14 3

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
/**
3
 * Box packing (3D bin packing, knapsack problem).
4
 *
5
 * @author Doug Wright
6
 */
7
declare(strict_types=1);
8
9
namespace DVDoug\BoxPacker;
10
11
use ArrayIterator;
12
use Countable;
13
use IteratorAggregate;
14
use Traversable;
15
16
/**
17
 * List of items to be packed, ordered by volume.
18
 *
19
 * @author Doug Wright
20
 */
21
class ItemList implements Countable, IteratorAggregate
22
{
23
    /**
24
     * List containing items.
25
     *
26
     * @var Item[]
27
     */
28
    private $list = [];
29
30
    /**
31
     * Has this list already been sorted?
32
     *
33
     * @var bool
34
     */
35
    private $isSorted = false;
36
37
    /**
38
     * @param Item $item
39
     */
40 2
    public function insert(Item $item)
41
    {
42 2
        $this->list[] = $item;
43 2
    }
44
45
    /**
46
     * @internal
47
     *
48
     * @return Item
49
     */
50 View Code Duplication
    public function extract(): Item
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
51
    {
52
        if (!$this->isSorted) {
53
            usort($this->list, [$this, 'compare']);
54
            $this->isSorted = true;
55
        }
56
57
        return array_shift($this->list);
58
    }
59
60
    /**
61
     * @internal
62
     *
63
     * @return Item
64
     */
65 View Code Duplication
    public function top(): Item
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
66
    {
67
        if (!$this->isSorted) {
68
            usort($this->list, [$this, 'compare']);
69
            $this->isSorted = true;
70
        }
71
        $temp = $this->list;
72
73
        return reset($temp);
74
    }
75
76
    /**
77
     * @return Traversable
78
     */
79 2 View Code Duplication
    public function getIterator(): Traversable
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
80
    {
81 2
        if (!$this->isSorted) {
82 2
            usort($this->list, [$this, 'compare']);
83 2
            $this->isSorted = true;
84
        }
85
86 2
        return new ArrayIterator($this->list);
87
    }
88
89
    /**
90
     * Number of items in list.
91
     *
92
     * @return int
93
     */
94
    public function count(): int
95
    {
96
        return count($this->list);
97
    }
98
99
    /**
100
     * @param Item $itemA
101
     * @param Item $itemB
102
     *
103
     * @return int
104
     */
105 2
    private function compare(Item $itemA, Item $itemB): int
106
    {
107 2
        $itemAVolume = $itemA->getWidth() * $itemA->getLength() * $itemA->getDepth();
108 2
        $itemBVolume = $itemB->getWidth() * $itemB->getLength() * $itemB->getDepth();
109 2
        $volumeDecider = $itemBVolume <=> $itemAVolume;
110 2
        $weightDecider = $itemB->getWeight() - $itemA->getWeight();
111 2
        if ($volumeDecider !== 0) {
112 1
            return $volumeDecider;
113 1
        } elseif ($weightDecider !== 0) {
114
            return $weightDecider;
115
        } else {
116 1
            return $itemA->getDescription() <=> $itemB->getDescription();
117
        }
118
    }
119
}
120