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 ( cc1afa...c98a24 )
by Gilles
01:41
created

Parser   B

Complexity

Total Complexity 36

Size/Duplication

Total Lines 153
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 0

Importance

Changes 0
Metric Value
dl 0
loc 153
rs 8.8
c 0
b 0
f 0
wmc 36
lcom 2
cbo 0

9 Methods

Rating   Name   Duplication   Size   Complexity  
A _parseBooleanConstant() 0 5 4
A _parseStringConstant() 0 5 3
A _parseIdentifier() 0 4 2
A _parseWildcard() 0 4 1
C _parseArray() 0 36 8
C _parseCons() 0 33 7
A _parseAs() 0 7 2
C parse() 0 27 8
A _invalidPattern() 0 4 1
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
        $patterns = split_enclosed(',', '[', ']', substr($pattern, 1, -1));
44
45
        if($patterns === false) {
46
            $this->_invalidPattern($pattern);
47
        }
48
49
        if(! is_array($value)) {
50
            return false;
51
        }
52
53
        $count = count($patterns);
54
        if($count === 0) {
55
            return count($value) === 0 ? [] : false;
56
        }
57
58
        if($count > count($value)) {
59
            return false;
60
        }
61
62
        $index = 0;
63
        $results = [];
64
        foreach($value as $v) {
65
            $new = $this->parse($v, $patterns[$index]);
66
67
            if($new === false) {
68
                return false;
69
            }
70
71
            $results = array_merge($results, $new);
72
            ++$index;
73
        }
74
75
        return $results;
76
    }
77
78
    protected function _parseCons($value, $pattern)
79
    {
80
        $patterns = split_enclosed(':', '(', ')', substr($pattern, 1, -1));
81
82
        if($patterns === false) {
83
            $this->_invalidPattern($pattern);
84
        }
85
86
        if(! is_array($value)) {
87
            return false;
88
        }
89
90
        $last = array_pop($patterns);
91
92
        $results = [];
93
        foreach($patterns as $p) {
94
            if(count($value) == 0) {
95
                return false;
96
            }
97
98
            $new = $this->parse(array_shift($value), $p);
99
100
            if($new === false) {
101
                return false;
102
            }
103
104
            $results = array_merge($results, $new);
105
        }
106
107
        $new = $this->parse($value, $last);
108
109
        return $new === false ? false : array_merge($results, $new);
110
    }
111
112
    protected function _parseAs($value, $pattern)
113
    {
114
        $patterns = explode('@', $pattern, 2);
115
116
        $rest = $this->parse($value, $patterns[1]);
117
        return $rest === false ? false : array_merge([$patterns[0] => $value], $rest);
118
    }
119
120
    /**
121
     * @param mixed $value
122
     * @param string $pattern
123
     * @return bool|array
124
     */
125
    public function parse($value, $pattern)
126
    {
127
        $pattern = trim($pattern);
128
129
        if(is_numeric($pattern) && is_numeric($value)) {
130
            return $pattern == $value ? [] : false;
131
        }
132
133
        $matched = false;
134
        foreach($this->rules as $regex => $method) {
135
            if(preg_match($regex, $pattern)) {
136
                $matched = true;
137
138
                $arguments = call_user_func_array([$this, $method], [$value, $pattern]);
139
140
                if($arguments !== false) {
141
                    return $arguments;
142
                }
143
            }
144
        }
145
146
        if(! $matched) {
147
            $this->_invalidPattern($pattern);
148
        }
149
150
        return false;
151
    }
152
153
    protected function _invalidPattern($pattern)
154
    {
155
        throw new \RuntimeException(sprintf('Invalid pattern "%s".', $pattern));
156
    }
157
}