Completed
Push — master ( 2820ae...deb522 )
by Gabriel
02:22
created

AccessMethodsTrait::values()   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
eloc 1
c 1
b 0
f 0
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
cc 1
nc 1
nop 0
crap 1
1
<?php
2
3
namespace Nip\Collections\Traits;
4
5
/**
6
 * Class AccessMethodsTrait
7
 * @package Nip\Collections\Traits
8
 */
9
trait AccessMethodsTrait
10
{
11
12
    /**
13
     * @param array $items
14
     */
15
    public function setItems($items)
16
    {
17
        $this->items = $items;
0 ignored issues
show
Bug Best Practice introduced by
The property items does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
18
    }
19
20
    /**
21
     * {@inheritDoc}
22
     */
23 27
    public function add($element, $key = null)
24
    {
25 27
        $this->set($key, $element);
26 13
    }
27
28
    /**
29
     * @param $key
30
     * @param mixed $value
31
     */
32 29
    public function set($key, $value)
33
    {
34 29
        $this->offsetSet($key, $value);
0 ignored issues
show
Bug introduced by
It seems like offsetSet() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

34
        $this->/** @scrutinizer ignore-call */ 
35
               offsetSet($key, $value);
Loading history...
35 15
    }
36
37
    /**
38
     * Returns a parameter by name.
39
     *
40
     * @param string $key The key
41
     * @param mixed $default The default value if the parameter key does not exist
42
     *
43
     * @return mixed
44
     */
45 2
    public function get($key, $default = null)
46
    {
47 2
        if ($this->offsetExists($key)) {
0 ignored issues
show
Bug introduced by
It seems like offsetExists() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

47
        if ($this->/** @scrutinizer ignore-call */ offsetExists($key)) {
Loading history...
48 2
            return $this->offsetGet($key);
0 ignored issues
show
Bug introduced by
It seems like offsetGet() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

48
            return $this->/** @scrutinizer ignore-call */ offsetGet($key);
Loading history...
49
        }
50
51 1
        return value($default);
52
    }
53
54
    /**
55
     * @return boolean
56
     * @param string $key
57
     */
58 1
    public function has($key)
59
    {
60 1
        $keys = is_array($key) ? $key : func_get_args();
61
62 1
        foreach ($keys as $value) {
63 1
            if (!$this->offsetExists($value)) {
64 1
                return false;
65
            }
66
        }
67
68 1
        return true;
69
    }
70
71
    /**
72
     * Returns the parameters.
73
     *
74
     * @return array An array of parameters
75
     */
76 3
    public function all()
77
    {
78 3
        return $this->items;
79
    }
80
81
    /**
82
     * Returns the parameter keys.
83
     *
84
     * @return array An array of parameter keys
85
     */
86 2
    public function keys()
87
    {
88 2
        return array_keys($this->items);
89
    }
90
91
    /**
92
     * Returns the parameter values.
93
     *
94
     * @return array An array of parameter values
95
     */
96 1
    public function values()
97
    {
98 1
        return array_values($this->items);
99
    }
100
101
    /**
102
     * Remove an item from the collection by key.
103
     *
104
     * @param string|array $keys
105
     * @return $this
106
     */
107
    public function forget($keys)
108
    {
109
        foreach ((array)$keys as $key) {
110
            $this->offsetUnset($key);
0 ignored issues
show
Bug introduced by
It seems like offsetUnset() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

110
            $this->/** @scrutinizer ignore-call */ 
111
                   offsetUnset($key);
Loading history...
111
        }
112
113
        return $this;
114
    }
115
116
    /**
117
     * @param string $key
118
     * @return null
119
     */
120
    public function unset($key)
121
    {
122
        $this->offsetUnset($key);
123
    }
124
125
    /**
126
     * Alias of unshift
127
     *
128
     * @param  mixed $value
129
     * @param  mixed $key
130
     * @return $this
131
     */
132
    public function prepend($value, $key = null)
133
    {
134
        return $this->unshift($value, $key);
135
    }
136
137
    /**
138
     * Push an item onto the beginning of the collection.
139
     *
140
     * @param $value
141
     * @param null $key
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $key is correct as it would always require null to be passed?
Loading history...
142
     * @return $this
143
     */
144 14
    public function unshift($value, $key = null)
145
    {
146 14
        if (is_null($key)) {
0 ignored issues
show
introduced by
The condition is_null($key) is always true.
Loading history...
147 14
            array_unshift($this->items, $value);
148
        } else {
149 1
            $this->items = [$key => $value] + $this->items;
150
        }
151
152 14
        return $this;
153
    }
154
155
    /**
156
     * Push one or more items onto the end of the collection.
157
     *
158
     * @param mixed $values [optional]
159
     * @return $this
160
     */
161 27
    public function push(...$values)
162
    {
163 27
        foreach ($values as $value) {
164 27
            $this->add($value);
165
        }
166
167 13
        return $this;
168
    }
169
}
170