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.
Completed
Push — master ( a4a304...ee4c72 )
by Pavel
12:31 queued 07:50
created

JsonBehavior   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 72
Duplicated Lines 40.28 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 93.75%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 14
c 1
b 0
f 0
lcom 1
cbo 3
dl 29
loc 72
ccs 30
cts 32
cp 0.9375
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A events() 0 13 1
A initialization() 0 6 2
A decode() 10 10 3
A encode() 10 10 4
A encodeValidate() 9 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
    /**
19
     * @inheritdoc
20
     */
21
    public function events()
22
    {
23
        return [
24
            ActiveRecord::EVENT_INIT            => function () { $this->initialization(); },
25
            ActiveRecord::EVENT_AFTER_FIND      => function () { $this->decode(); },
26
            ActiveRecord::EVENT_BEFORE_VALIDATE => function () { $this->encodeValidate(); },
27
            ActiveRecord::EVENT_BEFORE_INSERT   => function () { $this->encode(); },
28
            ActiveRecord::EVENT_BEFORE_UPDATE   => function () { $this->encode(); },
29
            ActiveRecord::EVENT_AFTER_VALIDATE  => function () { $this->decode(); },
30
            ActiveRecord::EVENT_AFTER_INSERT    => function () { $this->decode(); },
31
            ActiveRecord::EVENT_AFTER_UPDATE    => function () { $this->decode(); },
32 6
        ];
33
    }
34
35
    /**
36
     */
37 6
    protected function initialization()
38
    {
39 6
        foreach ($this->attributes as $attribute) {
40 6
            $this->owner->setAttribute($attribute, new JsonField());
41 6
        }
42 6
    }
43
44
    /**
45
     */
46 6 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...
47
    {
48 6
        foreach ($this->attributes as $attribute) {
49 6
            $value = $this->owner->getAttribute($attribute);
50 6
            if (!$value instanceof JsonField) {
51 5
                $value = new JsonField($value);
52 5
            }
53 6
            $this->owner->setAttribute($attribute, $value);
54 6
        }
55 6
    }
56
57
    /**
58
     */
59 4 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...
60
    {
61 4
        foreach ($this->attributes as $attribute) {
62 4
            $field = $this->owner->getAttribute($attribute);
63 4
            if (!$field instanceof JsonField) {
64
                $field = new JsonField($field);
65
            }
66 4
            $this->owner->setAttribute($attribute, (string)$field ?: null);
67 4
        }
68 4
    }
69
70
    /**
71
     */
72 3 View Code Duplication
    protected function encodeValidate()
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...
73
    {
74 3
        foreach ($this->attributes as $attribute) {
75 3
            $field = $this->owner->getAttribute($attribute);
76 3
            if ($field instanceof JsonField) {
77 2
                $this->owner->setAttribute($attribute, (string)$field ?: null);
78 2
            }
79 3
        }
80 3
    }
81
}
82