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.

JsonBehavior   A
last analyzed

Complexity

Total Complexity 16

Size/Duplication

Total Lines 90
Duplicated Lines 22.22 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 95.24%

Importance

Changes 0
Metric Value
wmc 16
lcom 1
cbo 3
dl 20
loc 90
ccs 40
cts 42
cp 0.9524
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A events() 0 21 3
A initialization() 0 6 2
A decode() 10 10 3
A encode() 10 10 4
A encodeValidate() 0 9 4

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\Behavior;
5
use yii\db\ActiveRecord;
6
7
/**
8
 *  @property ActiveRecord $owner
9
 */
10
class JsonBehavior extends Behavior
11
{
12
    /**
13
     * @var array
14
     */
15
    public $attributes = [];
16
17
    /**
18
     * @var null|string
19
     */
20
    public $emptyValue;
21
22
    /**
23
     * @var bool
24
     */
25
    public $encodeBeforeValidation = true;
26
27
28
    /**
29
     * @inheritdoc
30
     */
31 7
    public function events()
32
    {
33
        return [
34
            ActiveRecord::EVENT_INIT            => function () { $this->initialization(); },
35
            ActiveRecord::EVENT_AFTER_FIND      => function () { $this->decode(); },
36
            ActiveRecord::EVENT_BEFORE_INSERT   => function () { $this->encode(); },
37
            ActiveRecord::EVENT_BEFORE_UPDATE   => function () { $this->encode(); },
38
            ActiveRecord::EVENT_AFTER_INSERT    => function () { $this->decode(); },
39
            ActiveRecord::EVENT_AFTER_UPDATE    => function () { $this->decode(); },
40
            ActiveRecord::EVENT_BEFORE_VALIDATE => function () {
41 3
                if ($this->encodeBeforeValidation) {
42 3
                    $this->encodeValidate();
43 3
                }
44 7
            },
45 7
            ActiveRecord::EVENT_AFTER_VALIDATE  => function () {
46 3
                if ($this->encodeBeforeValidation) {
47 3
                    $this->decode();
48 3
                }
49 7
            },
50 7
        ];
51
    }
52
53
    /**
54
     */
55 7
    protected function initialization()
56
    {
57 7
        foreach ($this->attributes as $attribute) {
58 7
            $this->owner->setAttribute($attribute, new JsonField());
59 7
        }
60 7
    }
61
62
    /**
63
     */
64 7 View Code Duplication
    protected function decode()
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...
65
    {
66 7
        foreach ($this->attributes as $attribute) {
67 7
            $value = $this->owner->getAttribute($attribute);
68 7
            if (!$value instanceof JsonField) {
69 6
                $value = new JsonField($value);
70 6
            }
71 7
            $this->owner->setAttribute($attribute, $value);
72 7
        }
73 7
    }
74
75
    /**
76
     */
77 5 View Code Duplication
    protected function encode()
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...
78
    {
79 5
        foreach ($this->attributes as $attribute) {
80 5
            $field = $this->owner->getAttribute($attribute);
81 5
            if (!$field instanceof JsonField) {
82
                $field = new JsonField($field);
83
            }
84 5
            $this->owner->setAttribute($attribute, (string)$field ?: $this->emptyValue);
85 5
        }
86 5
    }
87
88
    /**
89
     */
90 3
    protected function encodeValidate()
91
    {
92 3
        foreach ($this->attributes as $attribute) {
93 3
            $field = $this->owner->getAttribute($attribute);
94 3
            if ($field instanceof JsonField) {
95 2
                $this->owner->setAttribute($attribute, (string)$field ?: null);
96 2
            }
97 3
        }
98 3
    }
99
}
100