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   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 70
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 13
lcom 1
cbo 3
dl 0
loc 70
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
B match() 0 25 5
A canMatch() 0 4 1
B getErrorMessage() 0 17 6
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