Passed
Push — master ( 980804...08b834 )
by Mihail
09:48
created

MutatorTrait::clear()   A

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 1
Bugs 0 Features 0
Metric Value
eloc 2
c 1
b 0
f 0
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
cc 1
nc 1
nop 0
crap 1
1
<?php
2
3
/*
4
 * This file is part of the Koded package.
5
 *
6
 * (c) Mihail Binev <[email protected]>
7
 *
8
 * Please view the LICENSE distributed with this source code
9
 * for the full copyright and license information.
10
 */
11
12
namespace Koded\Stdlib;
13
14
/**
15
 * @property array $storage
16
 */
17
trait MutatorTrait
18
{
19
    public function __set($index, $value)
20
    {
21
        return $this->set($index, $value);
22
    }
23
24 3
    public function set(string $index, mixed $value): static
25
    {
26 3
        $this->storage[$index] = $value;
27
        return $this;
28
    }
29 5
30
    public function bind(string $index, mixed &$variable): static
31 5
    {
32
        $this->storage[$index] = &$variable;
33 5
        return $this;
34
    }
35
36 1
    public function upsert(string $index, mixed $value): static
37
    {
38 1
        return $this->has($index) ? $this : $this->set($index, $value);
0 ignored issues
show
Bug introduced by
It seems like has() 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

38
        return $this->/** @scrutinizer ignore-call */ has($index) ? $this : $this->set($index, $value);
Loading history...
39
    }
40 1
41
    public function pull(string $index, mixed $default = null): mixed
42
    {
43 1
        $value = $this->get($index, $default);
0 ignored issues
show
Bug introduced by
It seems like get() 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

43
        /** @scrutinizer ignore-call */ 
44
        $value = $this->get($index, $default);
Loading history...
44
        unset($this->storage[$index]);
45 1
        return $value;
46
    }
47
48 1
    public function import(array $array): static
49
    {
50 1
        foreach ($array as $index => $value) {
51 1
            $this->storage[$index] = $value;
52
        }
53 1
        return $this;
54
    }
55
56 18
    public function delete(string $index): static
57
    {
58 18
        unset($this->storage[$index]);
59 17
        return $this;
60
    }
61
62 18
    public function clear(): static
63
    {
64
        $this->storage = [];
65 1
        return $this;
66
    }
67
}
68