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

Parser::_parseWildcard()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 4
rs 10
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
        $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
        return array_reduce($value, function($result, $v) use($patterns) {
63
            if($result === false) {
64
                return $result;
65
            }
66
67
            $new = $this->parse($v, $patterns[count($result)]);
68
            return $new === false ? false : array_merge($result, [$new]);
69
        }, []);
70
    }
71
72
    protected function _parseCons($value, $pattern)
73
    {
74
        $patterns = split_enclosed(':', '(', ')', substr($pattern, 1, -1));
75
76
        if($patterns === false) {
77
            $this->_invalidPattern($pattern);
78
        }
79
80
        if(! is_array($value)) {
81
            return false;
82
        }
83
84
        $last = array_pop($patterns);
85
86
        $results = array_reduce($patterns, function($acc, $p) {
87
            if($acc === false || count($acc[1]) === 0) {
88
                return false;
89
            }
90
91
            $new = $this->parse(array_shift($acc[1]), $p);
92
93
            return $new === false ? false : [array_merge($acc[0], $new), $acc[1]];
94
        }, [[], $value]);
95
96
        $new = $this->parse($value, $last);
97
98
        return $new === false ? false : array_merge($results[0], $new);
99
    }
100
101
    protected function _parseAs($value, $pattern)
102
    {
103
        $patterns = explode('@', $pattern, 2);
104
105
        $rest = $this->parse($value, $patterns[1]);
106
        return $rest === false ? false : array_merge([$patterns[0] => $value], $rest);
107
    }
108
109
    /**
110
     * @param mixed $value
111
     * @param string $pattern
112
     * @return bool|array
113
     */
114
    public function parse($value, $pattern)
115
    {
116
        $pattern = trim($pattern);
117
118
        if(is_numeric($pattern) && is_numeric($value)) {
119
            return $pattern == $value ? [] : false;
120
        }
121
122
        $result = array_reduce(array_keys($this->rules), function($result, $regex) use($value, $pattern) {
123
            if(preg_match($regex, $pattern)) {
124
                $arguments = call_user_func_array([$this, $this->rules[$regex]], [$value, $pattern]);
125
126
                return $arguments !== false ? $arguments : true;
127
            }
128
129
            return $result;
130
        }, false);
131
132
        if(! $result) {
133
            $this->_invalidPattern($pattern);
134
        }
135
136
        return false;
137
    }
138
139
    protected function _invalidPattern($pattern)
140
    {
141
        throw new \RuntimeException(sprintf('Invalid pattern "%s".', $pattern));
142
    }
143
}