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 ( 9d5c74...11439f )
by Gilles
02:02
created

Parser::parse()   C

Complexity

Conditions 8
Paths 9

Size

Total Lines 27
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 8
eloc 14
nc 9
nop 2
dl 0
loc 27
rs 5.3846
c 0
b 0
f 0
1
<?php
2
3
namespace FunctionalPHP\PatternMatching;
4
5
class Parser
6
{
7
    protected $reserved = ['true', 'false'];
8
9
    protected $rules = [
10
        '/^(true|false)$/i' => '_parseBooleanConstant',
11
        '/^([\'"])(?:(?!\\1).)*\\1$/' => '_parseStringConstant',
12
        '/^[a-zA-Z]+$/' => '_parseIdentifier',
13
        '/^_$/' => '_parseWildcard',
14
        '/^\\[.*\\]$/' => '_parseArray',
15
        '/^\\([^:]+:.+\\)$/' => '_parseCons',
16
        '/^[a-zA-Z]+@.+$/' => '_parseAs',
17
    ];
18
19
    protected function _parseBooleanConstant($value, $pattern)
20
    {
21
        $pattern_value = strtoupper($pattern) === 'TRUE' ? true : false;
22
        return is_bool($value) && $value === $pattern_value ? [] : false;
23
    }
24
25
    protected function _parseStringConstant($value, $pattern)
26
    {
27
        $string_pattern = substr($pattern, 1, -1);
28
        return is_string($value) && $string_pattern == $value ? [] : false;
29
    }
30
31
    protected function _parseIdentifier($value, $pattern)
32
    {
33
        return in_array(strtolower($pattern), $this->reserved) ? false : [$pattern => $value];
34
    }
35
36
    protected function _parseWildcard()
37
    {
38
        return [];
39
    }
40
41
    protected function _parseArray($value, $pattern)
42
    {
43
        if(! is_array($value)) {
44
            return false;
45
        }
46
47
        $patterns = array_filter(array_map('trim', split_enclosed(',', '[', ']', substr($pattern, 1, -1))));
48
49
        if(count($patterns) === 0) {
50
            return count($value) === 0 ? [] : false;
51
        }
52
53
        if(count($patterns) > count($value)) {
54
            return false;
55
        }
56
57
        $index = 0;
58
        $results = [];
59
        foreach($value as $v) {
60
            $new = $this->parse($v, $patterns[$index]);
61
62
            if($new === false) {
63
                return false;
64
            }
65
66
            $results = array_merge($results, $new);
67
            ++$index;
68
        }
69
70
        return $results;
71
    }
72
73
    protected function _parseCons($value, $pattern)
74
    {
75
        if(! is_array($value)) {
76
            return false;
77
        }
78
79
        $patterns = array_filter(array_map('trim', split_enclosed(':', '(', ')', substr($pattern, 1, -1))));
80
        $last = array_pop($patterns);
81
82
        $results = [];
83
        foreach($patterns as $p) {
84
            if(count($value) == 0) {
85
                return false;
86
            }
87
88
            $new = $this->parse(array_shift($value), $p);
89
90
            if($new === false) {
91
                return false;
92
            }
93
94
            $results = array_merge($results, $new);
95
        }
96
97
        $new = $this->parse($value, $last);
98
99
        return $new === false ? false : array_merge($results, $new);
100
    }
101
102
    protected function _parseAs($value, $pattern)
103
    {
104
        $patterns = explode('@', $pattern, 2);
105
106
        $rest = $this->parse($value, $patterns[1]);
107
        return $rest === false ? false : array_merge([$patterns[0] => $value], $rest);
108
    }
109
110
    /**
111
     * @param mixed $value
112
     * @param string $pattern
113
     * @return bool|array
114
     */
115
    public function parse($value, $pattern)
116
    {
117
        $pattern = trim($pattern);
118
119
        if(is_numeric($pattern) && is_numeric($value)) {
120
            return $pattern == $value ? [] : false;
121
        }
122
123
        $matched = false;
124
        foreach($this->rules as $regex => $method) {
125
            if(preg_match($regex, $pattern)) {
126
                $matched = true;
127
128
                $arguments = call_user_func_array([$this, $method], [$value, $pattern]);
129
130
                if($arguments !== false) {
131
                    return $arguments;
132
                }
133
            }
134
        }
135
136
        if(! $matched) {
137
            $this->_invalidPattern($pattern);
138
        }
139
140
        return false;
141
    }
142
143
    protected function _invalidPattern($pattern)
144
    {
145
        throw new \RuntimeException(sprintf('Invalid pattern |%s|.', $pattern));
146
    }
147
}