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 ( d182d5...392958 )
by Pavel
03:56
created

JsonValidator::validateAttribute()   A

Complexity

Conditions 4
Paths 6

Size

Total Lines 18
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 4

Importance

Changes 0
Metric Value
dl 0
loc 18
ccs 14
cts 14
cp 1
rs 9.2
c 0
b 0
f 0
cc 4
eloc 12
nc 6
nop 2
crap 4
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