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

PackedItemList   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 75
Duplicated Lines 10.67 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 7
lcom 1
cbo 2
dl 8
loc 75
ccs 0
cts 17
cp 0
rs 10
c 1
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A insert() 0 4 1
A getIterator() 8 9 2
A count() 0 4 1
A asItemArray() 0 6 1
A compare() 0 7 2

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 packed items, ordered by volume.
18
 *
19
 * @author Doug Wright
20
 */
21
class PackedItemList implements Countable, IteratorAggregate
22
{
23
    /**
24
     * List containing items.
25
     *
26
     * @var PackedItem[]
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 PackedItem $item
39
     */
40
    public function insert(PackedItem $item)
41
    {
42
        $this->list[] = $item;
43
    }
44
45
    /**
46
     * @return Traversable
47
     */
48 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...
49
    {
50
        if (!$this->isSorted) {
51
            usort($this->list, [$this, 'compare']);
52
            $this->isSorted = true;
53
        }
54
55
        return new ArrayIterator($this->list);
56
    }
57
58
    /**
59
     * Number of items in list.
60
     *
61
     * @return int
62
     */
63
    public function count(): int
64
    {
65
        return count($this->list);
66
    }
67
68
    /**
69
     * Get copy of this list as a standard PHP array.
70
     *
71
     * @internal
72
     *
73
     * @return Item[]
74
     */
75
    public function asItemArray(): array
76
    {
77
        return array_map(function (PackedItem $packedItem) {
78
            return $packedItem->getItem();
79
        }, $this->list);
80
    }
81
82
    /**
83
     * @param PackedItem $itemA
84
     * @param PackedItem $itemB
85
     *
86
     * @return int
87
     */
88
    private function compare(PackedItem $itemA, PackedItem $itemB): int
89
    {
90
        $itemAVolume = $itemA->getItem()->getWidth() * $itemA->getItem()->getLength() * $itemA->getItem()->getDepth();
91
        $itemBVolume = $itemB->getItem()->getWidth() * $itemB->getItem()->getLength() * $itemB->getItem()->getDepth();
92
93
        return ($itemBVolume <=> $itemAVolume) ?: ($itemB->getItem()->getWeight() <=> $itemA->getItem()->getWeight());
94
    }
95
}
96