AbstractCollection::count()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php
2
namespace Desmond\data_types;
3
4
abstract class AbstractCollection
5
{
6
    protected $collection = [];
7
8
    abstract protected function ends();
9
10 363
    public function __construct(array $elements=[])
11
    {
12 363
        $this->collection = $elements;
13 363
    }
14
15 339
    public function get($key)
16
    {
17 339
        return array_key_exists($key, $this->collection)
18 339
            ? $this->collection[$key] : null;
19
    }
20
21 342
    public function set($value, $key=null)
22
    {
23 342
        if (null !== $key) {
24 57
            $this->collection[$key] = $value;
25
        } else {
26 336
            $this->collection[] = $value;
27
        }
28 342
    }
29
30 3
    public function prepend($value)
31
    {
32 3
        array_unshift($this->collection, $value);
33 3
    }
34
35 6
    public function first()
36
    {
37 6
        return array_values($this->collection)[0];
38
    }
39
40 328
    public function rest()
41
    {
42 328
        $copy = $this->collection;
43 328
        array_shift($copy);
44 328
        return $copy;
45
    }
46
47 13
    public function count()
48
    {
49 13
        return count($this->collection);
50
    }
51
52 103
    public function value()
53
    {
54 103
        return $this->collection;
55
    }
56
57 7
    public function __toString()
58
    {
59 7
        $string = '';
60 7
        $first = true;
61 7
        foreach ($this->collection as $key => $val) {
62 7
            $separator = $first ? '' : ', ';
63 7
            $first = false;
64
            $string .=
65
                $separator.
66 7
                (is_string($key) ? $key. ' ' : '').
67 7
                (method_exists($val, '__toString') ? $val->__toString() : $val);
68
        }
69 7
        return $this->ends()[0] . $string . $this->ends()[1];
70
    }
71
}
72