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   A
last analyzed

Complexity

Total Complexity 20

Size/Duplication

Total Lines 118
Duplicated Lines 14.41 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 78.56%

Importance

Changes 0
Metric Value
wmc 20
lcom 1
cbo 2
dl 17
loc 118
ccs 33
cts 42
cp 0.7856
rs 10
c 0
b 0
f 0

11 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A __toString() 0 4 2
A set() 0 16 6
A fields() 0 5 1
A extraFields() 0 4 1
A toArray() 0 4 2
A isEmpty() 0 4 1
A offsetExists() 0 4 1
A offsetGet() 9 9 2
A offsetSet() 8 8 2
A offsetUnset() 0 4 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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
}