Passed
Push — master ( 11e596...ed648f )
by Gabriel
13:25
created

HasDataTrait::assign()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 3

Importance

Changes 0
Metric Value
eloc 4
c 0
b 0
f 0
dl 0
loc 9
rs 10
ccs 1
cts 1
cp 1
cc 3
nc 3
nop 1
crap 3
1
<?php
2
declare(strict_types=1);
3
4
namespace Nip\View\Traits;
5
6
/**
7
 * Trait HasDataTrait
8
 * @package Nip\View\Traits
9
 */
10
trait HasDataTrait
11
{
12
13
    /**
14
     * Determine if a piece of data is bound.
15
     *
16
     * @param string $key
17
     * @return bool
18
     */
19
    public function offsetExists($key): bool
20
    {
21
        return array_key_exists($key, $this->data);
0 ignored issues
show
Bug Best Practice introduced by
The property data does not exist on Nip\View\Traits\HasDataTrait. Since you implemented __get, consider adding a @property annotation.
Loading history...
22
    }
23
24
    /**
25
     * Get a piece of bound data to the view.
26
     *
27
     * @param string $key
28
     * @return mixed
29
     */
30
    public function offsetGet($key): mixed
31
    {
32
        return $this->data[$key];
0 ignored issues
show
Bug Best Practice introduced by
The property data does not exist on Nip\View\Traits\HasDataTrait. Since you implemented __get, consider adding a @property annotation.
Loading history...
33
    }
34
35
    /**
36
     * Set a piece of data on the view.
37
     *
38
     * @param string $key
39
     * @param mixed $value
40
     * @return void
41
     */
42
    public function offsetSet($key, $value): void
43
    {
44
        $this->with($key, $value);
45
    }
46
47
    /**
48
     * Unset a piece of data from the view.
49
     *
50
     * @param string $key
51
     * @return void
52
     */
53
    public function offsetUnset($key): void
54
    {
55
        unset($this->data[$key]);
0 ignored issues
show
Bug Best Practice introduced by
The property data does not exist on Nip\View\Traits\HasDataTrait. Since you implemented __get, consider adding a @property annotation.
Loading history...
56
    }
57
58
    /**
59
     * @param $key
60
     * @return mixed|null
61
     */
62 1
    public function &__get($key)
63
    {
64 1
        $var = $this->get($key);
65
        return $var;
66
    }
67
68
    /**
69
     * @param $name
70
     * @param $value
71
     * @return $this
72
     */
73
    public function __set($name, $value)
74
    {
75
        return $this->set($name, $value);
76
    }
77
78
    /**
79
     * @param string $name
80
     * @param null $default
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $default is correct as it would always require null to be passed?
Loading history...
81
     * @return mixed|null
82 2
     */
83
    public function get($name, $default = null)
84 2
    {
85 2
        $data = $this->getData();
0 ignored issues
show
Bug introduced by
It seems like getData() 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

85
        /** @scrutinizer ignore-call */ 
86
        $data = $this->getData();
Loading history...
86
        if (isset($data[$name])) {
87
            return $data[$name];
88
        } else {
89
            return $default;
90
        }
91
    }
92
93
    /**
94
     * @param string $name
95 2
     * @return bool
96
     */
97 2
    public function has(string $name)
98
    {
99
        $data = $this->getData();
100
        return isset($data[$name]);
101
    }
102
103
    /**
104
     * @param $key
105 2
     * @param null $value
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $value is correct as it would always require null to be passed?
Loading history...
106
     * @return $this
107 2
     */
108 1
    public function with($key, $value = null)
109
    {
110 2
        $data = (is_array($key)) ? $key : [$key => $value];
111
        $this->addData($data);
0 ignored issues
show
Bug introduced by
It seems like addData() 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

111
        $this->/** @scrutinizer ignore-call */ 
112
               addData($data);
Loading history...
112 2
113
        return $this;
114
    }
115
116
    /**
117
     * @param string|array $key
118
     * @param mixed $value
119
     * @return $this
120
     */
121
    public function set($key, $value)
122
    {
123
        $this->data->add([$key => $value]);
0 ignored issues
show
Bug introduced by
The method add() does not exist on null. ( Ignorable by Annotation )

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

123
        $this->data->/** @scrutinizer ignore-call */ 
124
                     add([$key => $value]);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
Bug Best Practice introduced by
The property data does not exist on Nip\View\Traits\HasDataTrait. Since you implemented __get, consider adding a @property annotation.
Loading history...
124
125
        return $this;
126
    }
127
128
    /**
129
     * @param $name
130
     * @return bool
131
     */
132
    public function __isset($name)
133
    {
134
        return isset($this->data[$name]);
0 ignored issues
show
Bug Best Practice introduced by
The property data does not exist on Nip\View\Traits\HasDataTrait. Since you implemented __get, consider adding a @property annotation.
Loading history...
135
    }
136
137
    /**
138
     * @param $name
139
     */
140
    public function __unset($name)
141
    {
142
        unset($this->data[$name]);
0 ignored issues
show
Bug Best Practice introduced by
The property data does not exist on Nip\View\Traits\HasDataTrait. Since you implemented __get, consider adding a @property annotation.
Loading history...
143
    }
144
145
    /**
146
     * @param string $name
147
     * @param string $appended
148
     * @return $this
149
     */
150
    public function append($name, $appended)
151
    {
152
        $value = $this->has($name) ? $this->get($name) : '';
153
        $value .= $appended;
154
155
        return $this->set($name, $value);
156
    }
157
158
    /**
159
     * Assigns variables in bulk in the current scope
160
     *
161
     * @param array $array
162 2
     * @return $this
163
     */
164 2
    public function assign($array = [])
165
    {
166
        foreach ($array as $key => $value) {
167
            if (is_string($key)) {
168
                $this->set($key, $value);
169
            }
170
        }
171
172
        return $this;
173
    }
174
}
175