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 ( 0bfe83...d7abbf )
by Gilles
02:33
created

Parser::_parseWildcard()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 4
ccs 1
cts 1
cp 1
crap 1
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 1
        $pattern_value = strtoupper($pattern) === 'TRUE' ? true : false;
22 1
        return is_bool($value) && $value === $pattern_value ? [] : false;
23
    }
24
25
    protected function _parseStringConstant($value, $pattern)
26
    {
27 1
        $string_pattern = substr($pattern, 1, -1);
28 1
        return is_string($value) && $string_pattern == $value ? [] : false;
29
    }
30
31
    protected function _parseIdentifier($value, $pattern)
32
    {
33 1
        return in_array(strtolower($pattern), $this->reserved) ? false : [$pattern => $value];
34
    }
35
36
    protected function _parseWildcard()
37
    {
38 1
        return [];
39
    }
40
41
    protected function _parseArray($value, $pattern)
42
    {
43 1
        $patterns = $this->_split(',', '[', ']', substr($pattern, 1, -1));
44
45 1
        if(count($patterns) === 0) {
46 1
            return count($value) === 0 ? [] : false;
47
        }
48
49 1
        return $this->_recurse($value, $patterns);
50
    }
51
52
    protected function _parseCons($value, $pattern)
53
    {
54 1
        $patterns = $this->_split(':', '(', ')', substr($pattern, 1, -1));
55 1
        $last_pattern = array_pop($patterns);
56
57 1
        $results = $this->_recurse($value, $patterns);
58
59 1
        if($results !== false) {
60 1
            $last_result = $this->parse(array_splice($value, count($patterns)), $last_pattern);
61
62 1
            $results = $last_result === false ? false : array_merge($results, $last_result);
63 1
        }
64
65 1
        return $results;
66
    }
67
68
    protected function _parseAs($value, $pattern)
69
    {
70 1
        $patterns = explode('@', $pattern, 2);
71
72 1
        $rest = $this->parse($value, $patterns[1]);
73 1
        return $rest === false ? false : array_merge([$patterns[0] => $value], $rest);
74
    }
75
76
    /**
77
     * @param mixed $value
78
     * @param string $pattern
79
     * @return bool|array
80
     */
81
    public function parse($value, $pattern)
82
    {
83 1
        $pattern = trim($pattern);
84
85 1
        if(is_numeric($pattern)) {
86 1
            return is_numeric($value) && $pattern == $value ? [] : false;
87
        }
88
89 1
        $matched = false;
90 1
        foreach($this->rules as $regex => $method) {
91 1
            if(preg_match($regex, $pattern)) {
92 1
                $matched = true;
93
94 1
                $arguments = call_user_func_array([$this, $method], [$value, $pattern]);
95
96 1
                if($arguments !== false) {
97 1
                    return $arguments;
98
                }
99 1
            }
100 1
        }
101
102 1
        if(! $matched) {
103 1
            $this->_invalidPattern($pattern);
104
        }
105
106 1
        return false;
107
    }
108
109
    protected function _split($delimiter, $start, $stop, $pattern)
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
110
    {
111 1
        $result = split_enclosed($delimiter, $start, $stop, $pattern);
112
113 1
        if($result === false) {
114 1
            $this->_invalidPattern($pattern);
115
        }
116
117 1
        return $result;
118
    }
119
120
    protected function _recurse($value, $patterns)
121
    {
122 1
        if(! is_array($value) || count($patterns) > count($value)) {
123 1
            return false;
124
        }
125
126 1
        $results = [];
127 1
        foreach($patterns as $p) {
128 1
            $new = $this->parse(array_shift($value), $p);
129
130 1
            if($new === false) {
131 1
                return false;
132
            }
133
134 1
            $results = array_merge($results, $new);
135 1
        }
136
137 1
        return $results;
138
    }
139
140
    protected function _invalidPattern($pattern)
141
    {
142 1
        throw new \RuntimeException(sprintf('Invalid pattern "%s".', $pattern));
143
    }
144
}