GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

JsonField::offsetSet()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 8

Duplication

Lines 8
Ratio 100 %

Code Coverage

Tests 4
CRAP Score 2.1481

Importance

Changes 0
Metric Value
dl 8
loc 8
ccs 4
cts 6
cp 0.6667
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 2
crap 2.1481
1
<?php
2
namespace paulzi\jsonBehavior;
3
4
use yii\base\Arrayable;
5
use yii\base\InvalidParamException;
6
use yii\helpers\Json;
7
8
class JsonField implements \ArrayAccess, Arrayable
9
{
10
    /**
11
     * @var array
12
     */
13
    protected $value;
14
15
16
    /**
17
     * @param string|array $value
18
     */
19 29
    public function __construct($value = [])
20
    {
21 29
        $this->set($value);
22 22
    }
23
24
    /**
25
     * @return string
26
     */
27 13
    public function __toString()
28
    {
29 13
        return $this->value ? Json::encode($this->value) : '';
30
    }
31
32
    /**
33
     * @param string|array $value
34
     */
35 29
    public function set($value)
36
    {
37 29
        if ($value === null || $value === '') {
38 4
            $value = [];
39 29
        } elseif (is_string($value)) {
40 24
            $value = Json::decode($value, true);
41 22
            if (!is_array($value)) {
42 9
                throw new InvalidParamException('Value is scalar');
43
            }
44 14
        }
45 24
        if (!is_array($value)) {
46 2
            throw new InvalidParamException('Value is not array');
47
        } else {
48 22
            $this->value = $value;
49
        }
50 22
    }
51
52
    /**
53
     * @inheritdoc
54
     */
55 1
    public function fields()
56
    {
57 1
        $fields = array_keys($this->value);
58 1
        return array_combine($fields, $fields);
59
    }
60
61
    /**
62
     * @inheritdoc
63
     */
64
    public function extraFields()
65
    {
66
        return [];
67
    }
68
69
    /**
70
     * @inheritdoc
71
     */
72 4
    public function toArray(array $fields = [], array $expand = [], $recursive = true)
73
    {
74 4
        return empty($fields) ? $this->value : array_intersect_key($this->value, array_flip($fields));
75
    }
76
77
    /**
78
     * @return bool
79
     */
80 1
    public function isEmpty()
81
    {
82 1
        return !$this->value;
83
    }
84
85
    /**
86
     * @inheritdoc
87
     */
88
    public function offsetExists($offset)
89
    {
90
        return isset($this->value[$offset]);
91
    }
92
93
    /**
94
     * @inheritdoc
95
     */
96 3 View Code Duplication
    public function &offsetGet($offset)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
97
    {
98 3
        $null = null;
99 3
        if (isset($this->value[$offset])) {
100 3
            return $this->value[$offset];
101
        } else {
102 1
            return $null;
103
        }
104
    }
105
106
    /**
107
     * @inheritdoc
108
     */
109 7 View Code Duplication
    public function offsetSet($offset, $value)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
110
    {
111 7
        if ($offset === null) {
112
            $this->value[] = $value;
113
        } else {
114 7
            $this->value[$offset] = $value;
115
        }
116 7
    }
117
118
    /**
119
     * @inheritdoc
120
     */
121
    public function offsetUnset($offset)
122
    {
123
        unset($this->value[$offset]);
124
    }
125
}