PairStackable   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 85
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2
Metric Value
wmc 9
lcom 1
cbo 2
dl 0
loc 85
rs 10

8 Methods

Rating   Name   Duplication   Size   Complexity  
A toArray() 0 4 1
A isEmpty() 0 4 1
A count() 0 4 1
A getIterator() 0 4 1
A first() 0 5 1
A last() 0 5 1
A returnValue() 0 9 2
A createArray() 0 7 1
1
<?php
2
3
/**
4
 * This file is part of cloak.
5
 *
6
 * (c) Noritaka Horio <[email protected]>
7
 *
8
 * This source file is subject to the MIT license that is bundled
9
 * with this source code in the file LICENSE.
10
 */
11
12
namespace cloak\collection;
13
14
use PhpOption\Option;
15
use PhpCollection\AbstractMap;
16
17
18
/**
19
 * Trait PairStackable
20
 * @package cloak\collection
21
 */
22
trait PairStackable
23
{
24
25
    /**
26
     * @var \PhpCollection\Map
27
     */
28
    protected $collection;
29
30
    /**
31
     * @return mixed|null
32
     */
33
    public function first()
34
    {
35
        $first = $this->collection->first();
36
        return $this->returnValue($first);
37
    }
38
39
    /**
40
     * @return mixed|null
41
     */
42
    public function last()
43
    {
44
        $last = $this->collection->last();
45
        return $this->returnValue($last);
46
    }
47
48
    /**
49
     * @param Option $value
50
     * @return mixed|null
51
     */
52
    private function returnValue(Option $value)
53
    {
54
        if ($value->isEmpty()) {
55
            return null;
56
        }
57
        $kv = $value->get();
58
59
        return array_pop($kv);
60
    }
61
62
    /**
63
     * @return array
64
     */
65
    public function toArray()
66
    {
67
        return $this->createArray($this->collection);
68
    }
69
70
    /**
71
     * @param AbstractMap $collection
72
     * @return array
73
     */
74
    protected function createArray(AbstractMap $collection)
75
    {
76
        $keys = $collection->keys();
77
        $values = $collection->values();
78
79
        return array_combine($keys, $values);
80
    }
81
82
    /**
83
     * @return int
84
     */
85
    public function isEmpty()
86
    {
87
        return $this->collection->isEmpty();
88
    }
89
90
    /**
91
     * @return int
92
     */
93
    public function count()
94
    {
95
        return $this->collection->count();
96
    }
97
98
    /**
99
     * @return \ArrayIterator
100
     */
101
    public function getIterator()
102
    {
103
        return $this->collection->getIterator();
104
    }
105
106
}
107