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 ( 14e86b...9d5c74 )
by Gilles
02:13
created

Parser::_parseBooleanConstant()   A

Complexity

Conditions 4
Paths 8

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 3
nc 8
nop 2
dl 0
loc 5
rs 9.2
c 0
b 0
f 0
1
<?php
2
3
namespace PHPFunctional\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 : [$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([$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);
1 ignored issue
show
Coding Style introduced by
Consider using a different name than the parameter $pattern. This often makes code more readable.
Loading history...
118
119
        if(is_numeric($pattern) && is_numeric($value)) {
120
            return $pattern == $value ? [] : false;
121
        }
122
123
        foreach($this->rules as $regex => $method) {
124
            if(preg_match($regex, $pattern)) {
125
                $arguments = call_user_func_array([$this, $method], [$value, $pattern]);
126
127
                if($arguments !== false) {
128
                    return $arguments;
129
                }
130
            }
131
        }
132
133
        return false;
134
    }
135
}