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

Parser::_parseBooleanConstant()   A

Complexity

Conditions 4
Paths 8

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 4

Importance

Changes 0
Metric Value
cc 4
eloc 3
nc 8
nop 2
dl 0
loc 5
ccs 2
cts 2
cp 1
crap 4
rs 9.2
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
        if(! is_array($value)) {
58 1
            return false;
59
        }
60
61 1
        return $this->_mergeResults(
62 1
            $this->parse(array_splice($value, count($patterns)), $last_pattern),
63 1
            $this->_recurse($value, $patterns)
64 1
        );
65
    }
66
67
    protected function _parseAs($value, $pattern)
68
    {
69 1
        $patterns = explode('@', $pattern, 2);
70
71 1
        $rest = $this->parse($value, $patterns[1]);
72 1
        return $this->_mergeResults([$patterns[0] => $value], $rest);
73
    }
74
75
    /**
76
     * @param mixed $value
77
     * @param string $pattern
78
     * @return bool|array
79
     */
80
    public function parse($value, $pattern)
81
    {
82 1
        $pattern = trim($pattern);
83
84 1
        if(is_numeric($pattern)) {
85 1
            return is_numeric($value) && $pattern == $value ? [] : false;
86
        }
87
88
        // a true value will mean that no regex matched
89
        // a false value will mean that at least one regex matched but the pattern didn't
90
        // anything else is the result of the pattern matching
91 1
        $result = array_reduce(array_keys($this->rules), function($result, $regex) use($value, $pattern) {
92 1
            if(is_bool($result) && preg_match($regex, $pattern)) {
93 1
                $result = call_user_func_array([$this, $this->rules[$regex]], [$value, $pattern]);
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $result. This often makes code more readable.
Loading history...
94 1
            }
95
96 1
            return $result;
97 1
        }, true);
98
99 1
        if($result === true) {
100 1
            $this->_invalidPattern($pattern);
101
        }
102
103 1
        return $result;
104
    }
105
106
    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...
107
    {
108 1
        $result = split_enclosed($delimiter, $start, $stop, $pattern);
109
110 1
        if($result === false) {
111 1
            $this->_invalidPattern($pattern);
112
        }
113
114 1
        return $result;
115
    }
116
117
    protected function _recurse($value, $patterns)
118
    {
119 1
        if(! is_array($value) || count($patterns) > count($value)) {
120 1
            return false;
121
        }
122
123 1
        return array_reduce($patterns, function($results, $p) use(&$value) {
124 1
            return $this->_mergeResults($results, $this->parse(array_shift($value), $p));
125 1
        }, []);
126
    }
127
128
    protected function _mergeResults($new, $current)
129
    {
130 1
        return $new === false || $current === false ?
131 1
            false :
132 1
            array_merge($current, $new);
133
    }
134
135
    protected function _invalidPattern($pattern)
136
    {
137 1
        throw new \RuntimeException(sprintf('Invalid pattern "%s".', $pattern));
138
    }
139
}