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 ( 56139c...893ddf )
by Gilles
02:17
created

Matcher   A

Complexity

Total Complexity 31

Size/Duplication

Total Lines 146
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
dl 0
loc 146
rs 9.8
c 0
b 0
f 0
wmc 31
lcom 1
cbo 0

9 Methods

Rating   Name   Duplication   Size   Complexity  
A _parseBooleanConstant() 0 4 2
A _parseStringConstant() 0 5 3
A _parseIdentifier() 0 4 1
A _parseWildcard() 0 4 1
C _parseArray() 0 31 7
B _parseCons() 0 28 6
A _parseAs() 0 7 2
B parse() 0 20 6
A match() 0 12 3
1
<?php
2
3
namespace PHPFunctional\PatternMatching;
4
5
class Matcher
6
{
7
    private static $rules = [
8
        '/^(true|false)$/i' => '_parseBooleanConstant',
9
        '/^([\'"])(?:(?!\\1).)*\\1$/' => '_parseStringConstant',
10
        '/^[a-zA-Z]+$/' => '_parseIdentifier',
11
        '/^_$/' => '_parseWildcard',
12
        '/^\\[.*\\]$/' => '_parseArray',
13
        '/^\\([^:]+:.+\\)$/' => '_parseCons',
14
        '/^[a-zA-Z]+@.+$/' => '_parseAs',
15
    ];
16
17
    private static function _parseBooleanConstant($value, $pattern)
18
    {
19
        return is_bool($value) ? [] : false;
20
    }
21
22
    private static function _parseStringConstant($value, $pattern)
23
    {
24
        $string_pattern = substr($pattern, 1, -1);
25
        return is_string($value) && $string_pattern == $value ? [] : false;
26
    }
27
28
    private static function _parseIdentifier($value, $pattern)
29
    {
30
        return [$value];
31
    }
32
33
    private static function _parseWildcard($value, $pattern)
1 ignored issue
show
Unused Code introduced by
The parameter $value is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
34
    {
35
        return [];
36
    }
37
38
    private static function _parseArray($value, $pattern)
39
    {
40
        if(! is_array($value)) {
41
            return false;
42
        }
43
44
        $patterns = array_filter(array_map('trim', split_enclosed(',', '[', ']', substr($pattern, 1, -1))));
45
46
        if(count($patterns) === 0) {
47
            return count($value) === 0 ? [] : false;
48
        }
49
50
        if(count($patterns) > count($value)) {
51
            return false;
52
        }
53
54
        $index = 0;
55
        $results = [];
56
        foreach($value as $v) {
57
            $new = self::parse($v, $patterns[$index]);
58
59
            if($new === false) {
60
                return false;
61
            }
62
63
            $results = array_merge($results, $new);
64
            ++$index;
65
        }
66
67
        return $results;
68
    }
69
70
    private static function _parseCons($value, $pattern)
71
    {
72
        if(! is_array($value)) {
73
            return false;
74
        }
75
76
        $patterns = array_filter(array_map('trim', split_enclosed(':', '(', ')', substr($pattern, 1, -1))));
77
        $last = array_pop($patterns);
78
79
        $results = [];
80
        foreach($patterns as $p) {
81
            if(count($value) == 0) {
82
                return false;
83
            }
84
85
            $new = self::parse(array_shift($value), $p);
86
87
            if($new === false) {
88
                return false;
89
            }
90
91
            $results = array_merge($results, $new);
92
        }
93
94
        $new = self::parse($value, $last);
95
96
        return $new === false ? false : array_merge($results, $new);
97
    }
98
99
    private static function _parseAs($value, $pattern)
100
    {
101
        $patterns = explode('@', $pattern, 2);
102
103
        $rest = self::parse($value, $patterns[1]);
104
        return $rest === false ? false : array_merge([$value], $rest);
105
    }
106
107
    /**
108
     * @param mixed $value
109
     * @param string $pattern
110
     * @return bool|array
111
     */
112
    private static function parse($value, $pattern)
113
    {
114
        $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...
115
116
        if(is_numeric($pattern) && is_numeric($value)) {
117
            return [];
118
        }
119
120
        foreach(self::$rules as $regex => $method) {
121
            if(preg_match($regex, $pattern)) {
122
                $arguments = call_user_func_array(['static', $method], [$value, $pattern]);
123
124
                if($arguments !== false) {
125
                    return $arguments;
126
                }
127
            }
128
        }
129
130
        return false;
131
    }
132
133
    /**
134
     * @param mixed $value
135
     * @param array $patterns
136
     * @return mixed
137
     */
138
    public static function match($value, array $patterns)
139
    {
140
        foreach($patterns as $pattern => $callback) {
141
            $match = self::parse($value, $pattern);
142
143
            if($match !== false) {
144
                return call_user_func_array($callback, $match);
145
            }
146
        }
147
148
        throw new \RuntimeException("Non-exhaustive patterns.");
0 ignored issues
show
Coding Style Comprehensibility introduced by
The string literal Non-exhaustive patterns. does not require double quotes, as per coding-style, please use single quotes.

PHP provides two ways to mark string literals. Either with single quotes 'literal' or with double quotes "literal". The difference between these is that string literals in double quotes may contain variables with are evaluated at run-time as well as escape sequences.

String literals in single quotes on the other hand are evaluated very literally and the only two characters that needs escaping in the literal are the single quote itself (\') and the backslash (\\). Every other character is displayed as is.

Double quoted string literals may contain other variables or more complex escape sequences.

<?php

$singleQuoted = 'Value';
$doubleQuoted = "\tSingle is $singleQuoted";

print $doubleQuoted;

will print an indented: Single is Value

If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear.

For more information on PHP string literals and available escape sequences see the PHP core documentation.

Loading history...
149
    }
150
}