Passed
Push — develop ( bf9913...29023c )
by Septianata
01:50 queued 14s
created

DataSet::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
1
<?php
2
3
namespace Ianriizky\CodingInterview\DataSet;
4
5
use SplFixedArray;
6
7
class DataSet
8
{
9
    /**
10
     * Current size of the collection.
11
     */
12
    protected int $size = 0;
13
14
    /**
15
     * Collection of the data set.
16
     */
17
    protected SplFixedArray $collection;
18
19
    /**
20
     * Create a new instance class.
21
     *
22
     * @return void
23
     */
24 5
    public function __construct(int $initialSize = 10)
25
    {
26 5
        $this->collection = new SplFixedArray($initialSize);
27 5
    }
28
29
    /**
30
     * Add a value to the collection.
31
     *
32
     * @param  mixed  $value
33
     */
34 5
    public function add($value): bool
35
    {
36 5
        if ($this->contains($value)) {
37 2
            return false;
38
        }
39
40 5
        $this->ensureSize();
41
42 5
        $this->collection[$this->size] = $value;
43 5
        $this->size++;
44
45 5
        return true;
46
    }
47
48
    /**
49
     * Determine whether the given value is exists on the collection or not.
50
     *
51
     * @param  mixed  $value
52
     */
53 5
    public function contains($value): bool
54
    {
55 5
        foreach ($this->collection as $item) {
56 5
            if ($value === $item) {
57 4
                return true;
58
            }
59
        }
60
61 5
        return false;
62
    }
63
64
    /**
65
     * Return size of collection.
66
     */
67 3
    public function size(): int
68
    {
69 3
        return $this->size;
70
    }
71
72
    /**
73
     * Return collection of the data set.
74
     */
75
    public function collection(): SplFixedArray
76
    {
77
        return $this->collection;
78
    }
79
80
    /**
81
     * Remove a value from the collection.
82
     *
83
     * @param  mixed  $value
84
     */
85 1
    public function remove($value): bool
86
    {
87 1
        if (! $this->contains($value)) {
88
            return false;
89
        }
90
91 1
        $removedIndex = $this->indexOf($value);
92
93 1
        for ($index = $removedIndex; $index <= $this->size; $index++) {
94 1
            $this->collection[$index] = $this->collection[$index + 1];
95
        }
96
97 1
        $this->size--;
98
99 1
        return true;
100
    }
101
102
    /**
103
     * Return index of given value.
104
     *
105
     * @param  mixed  $value
106
     */
107 1
    protected function indexOf($value): int
108
    {
109 1
        for ($index = 0; $index < $this->collection->count(); $index++) {
110 1
            if ($value === $this->collection[$index]) {
111 1
                return $index;
112
            }
113
        }
114
115
        return -1;
116
    }
117
118
    /**
119
     * Ensure that the data set size is enough to contain the collection.
120
     */
121 5
    protected function ensureSize(): void
122
    {
123 5
        if ($this->size < $this->collection->count()) {
124 5
            return;
125
        }
126
127 1
        $tempCollection = new SplFixedArray($this->size + 1);
128
129 1
        for ($index = 0; $index < $this->collection->count(); $index++) {
130 1
            $tempCollection[$index] = $this->collection[$index];
131
        }
132
133 1
        $this->collection = $tempCollection;
134 1
    }
135
}
136