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.

JsonValidator   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 49
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 94.74%

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 3
dl 0
loc 49
ccs 18
cts 19
cp 0.9474
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A validateAttribute() 0 18 4
A getErrorMessage() 0 8 2
1
<?php
2
namespace paulzi\jsonBehavior;
3
4
use yii\base\InvalidParamException;
5
use yii\db\BaseActiveRecord;
6
use yii\validators\Validator;
7
8
class JsonValidator extends Validator
9
{
10
    /**
11
     * @var bool
12
     */
13
    public $merge = false;
14
15
    /**
16
     * Map json error constant to message
17
     * @see: http://php.net/manual/ru/json.constants.php
18
     * @var array
19
     */
20
    public $errorMessages = [];
21
22
    /**
23
     * @inheritdoc
24
     */
25 3
    public function validateAttribute($model, $attribute)
26
    {
27 3
        $value = $model->$attribute;
28 3
        if (!$value instanceof JsonField) {
29
            try {
30 3
                $new = new JsonField($value);
31 3
                if ($this->merge) {
32
                    /** @var BaseActiveRecord $model */
33 1
                    $old = new JsonField($model->getOldAttribute($attribute));
34 1
                    $new = new JsonField(array_merge($old->toArray(), $new->toArray()));
35 1
                }
36 3
                $model->$attribute = $new;
37 3
            } catch (InvalidParamException $e) {
38 1
                $this->addError($model, $attribute, $this->getErrorMessage($e));
39 1
                $model->$attribute = new JsonField();
40
            }
41 3
        }
42 3
    }
43
44
    /**
45
     * @param \Exception $exception
46
     * @return string
47
     */
48 1
    protected function getErrorMessage($exception)
49
    {
50 1
        $code = $exception->getCode();
51 1
        if (isset($this->errorMessages[$code])) {
52
            return $this->errorMessages[$code];
53
        }
54 1
        return $exception->getMessage();
55
    }
56
}
57