Completed
Push — IncomprehensibleFinder/base ( 348f7f...2b8e11 )
by Albert
04:07 queued 01:29
created

CoupleCollection::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
crap 1
1
<?php
2
3
namespace Kata\Algorithm\Model;
4
5
class CoupleCollection implements \Iterator, \Countable, \ArrayAccess
0 ignored issues
show
Coding Style introduced by
Since you have declared the constructor as private, maybe you should also declare the class as final.
Loading history...
6
{
7
    /** @var Couple[] */
8
    private $all_items;
9
10
    /**
11
     * @param Couple[] $couples_array
12
     */
13 6
    private function __construct(array $couples_array)
14
    {
15 6
        $this->all_items = $couples_array;
16 6
    }
17
18
    /**
19
     * @param array $people_array
20
     *
21
     * @return CoupleCollection
22
     */
23 6
    public static function buildAllPossibleCouplesFromPeopleArray(array $people_array): CoupleCollection
24
    {
25 6
        $couples = [];
26 6
        for ($i = 0; $i < count($people_array); $i++) {
0 ignored issues
show
Performance Best Practice introduced by
It seems like you are calling the size function count() as part of the test condition. You might want to compute the size beforehand, and not on each iteration.

If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration:

for ($i=0; $i<count($array); $i++) { // calls count() on each iteration
}

// Better
for ($i=0, $c=count($array); $i<$c; $i++) { // calls count() just once
}
Loading history...
27 5
            for ($j = $i + 1; $j < count($people_array); $j++) {
0 ignored issues
show
Performance Best Practice introduced by
It seems like you are calling the size function count() as part of the test condition. You might want to compute the size beforehand, and not on each iteration.

If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration:

for ($i=0; $i<count($array); $i++) { // calls count() on each iteration
}

// Better
for ($i=0, $c=count($array); $i<$c; $i++) { // calls count() just once
}
Loading history...
28 4
                $couples[] = new Couple($people_array[$i], $people_array[$j]);
29
            }
30
        }
31
32 6
        return new self($couples);
33
34
    }
35
36
    public function allItems()
37
    {
38
        return $this->all_items;
39
    }
40
41
    public function offsetExists($offset)
42
    {
43
        return isset($this->all_items[$offset]);
44
    }
45
46 4
    public function offsetGet($offset)
47
    {
48 4
        return $this->all_items[$offset];
49
    }
50
51
    public function offsetSet($offset, $value)
52
    {
53
        return $this->all_items[$offset] = $value;
54
    }
55
56
    public function offsetUnset($offset)
57
    {
58
        unset($this->all_items[$offset]);
59
    }
60
61 6
    public function count()
62
    {
63 6
        return count($this->all_items);
64
    }
65
66
67 4
    public function current()
68
    {
69 4
        return current($this->all_items);
70
    }
71
72 4
    public function next()
73
    {
74 4
        next($this->all_items);
75 4
    }
76
77
    public function key()
78
    {
79
        return key($this->all_items);
80
    }
81
82 4
    public function valid()
83
    {
84 4
        return current($this->all_items);
85
    }
86
87 4
    public function rewind()
88
    {
89 4
        reset($this->all_items);
90 4
    }
91
}
92