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 ( 55456e...615247 )
by Norbert
03:52 queued 01:47
created

JsonMatcher::match()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 25
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 25
rs 8.439
c 0
b 0
f 0
cc 5
eloc 15
nc 5
nop 2
1
<?php
2
3
namespace Coduo\PHPMatcher\Matcher;
4
5
use Coduo\PHPMatcher\Matcher\Pattern\Assert\Json;
6
7
final class JsonMatcher extends Matcher
8
{
9
    /**
10
     * @var
11
     */
12
    private $matcher;
13
14
    /**
15
     * @param ValueMatcher $matcher
16
     */
17
    public function __construct(ValueMatcher $matcher)
18
    {
19
        $this->matcher = $matcher;
20
    }
21
22
    /**
23
     * {@inheritDoc}
24
     */
25
    public function match($value, $pattern)
26
    {
27
        if (parent::match($value, $pattern)) {
28
            return true;
29
        }
30
31
        if (!Json::isValid($value)) {
32
            $this->error = sprintf("Invalid given JSON of value. %s", $this->getErrorMessage());
33
            return false;
34
        }
35
36
        if (!Json::isValidPattern($pattern) ) {
37
            $this->error = sprintf("Invalid given JSON of pattern. %s", $this->getErrorMessage());
38
            return false;
39
        }
40
41
        $transformedPattern = Json::transformPattern($pattern);
42
        $match = $this->matcher->match(json_decode($value, true), json_decode($transformedPattern, true));
43
        if (!$match) {
44
            $this->error = $this->matcher->getError();
45
            return false;
46
        }
47
48
        return true;
49
    }
50
51
    /**
52
     * {@inheritDoc}
53
     */
54
    public function canMatch($pattern)
55
    {
56
        return Json::isValidPattern($pattern);
57
    }
58
59
    private function getErrorMessage()
60
    {
61
        switch(json_last_error()) {
62
            case JSON_ERROR_DEPTH:
63
                return 'Maximum stack depth exceeded';
64
            case JSON_ERROR_STATE_MISMATCH:
65
                return 'Underflow or the modes mismatch';
66
            case JSON_ERROR_CTRL_CHAR:
67
                return 'Unexpected control character found';
68
            case JSON_ERROR_SYNTAX:
69
                return 'Syntax error, malformed JSON';
70
            case JSON_ERROR_UTF8:
71
                return 'Malformed UTF-8 characters, possibly incorrectly encoded';
72
            default:
73
                return 'Unknown error';
74
        }
75
    }
76
}
77