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 ( b22946...56139c )
by Gilles
02:11
created

Matcher::_parseStringConstant()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 3
nc 4
nop 2
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace PHPFunctional\PatternMatching;
4
5
class Matcher
6
{
7
    private static $rules = [
8
        "/^(true|false)$/i" => '_parseBooleanConstant',
0 ignored issues
show
Coding Style Comprehensibility introduced by
The string literal /^(true|false)$/i 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...
9
        "/^(['\"])(?:(?!\\1).)*\\1$/" => '_parseStringConstant',
10
        "/^[a-zA-Z]+$/" => '_parseIdentifier',
0 ignored issues
show
Coding Style Comprehensibility introduced by
The string literal /^[a-zA-Z]+$/ 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...
11
        "/^_$/" => '_parseWildcard',
0 ignored issues
show
Coding Style Comprehensibility introduced by
The string literal /^_$/ 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...
12
        "/^\\[.*\\]$/" => '_parseArray',
0 ignored issues
show
Coding Style Comprehensibility introduced by
The string literal /^\\[.*\\]$/ 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...
13
        "/^\\([^:]+:.+\\)$/" => '_parseCons',
0 ignored issues
show
Coding Style Comprehensibility introduced by
The string literal /^\\([^:]+:.+\\)$/ 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...
14
    ];
15
16
    private static function _parseBooleanConstant($value, $pattern)
2 ignored issues
show
Unused Code introduced by
This method is not used, and could be removed.
Loading history...
Unused Code introduced by
The parameter $pattern 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...
17
    {
18
        return is_bool($value) ? [] : false;
19
    }
20
21
    private static function _parseStringConstant($value, $pattern)
1 ignored issue
show
Unused Code introduced by
This method is not used, and could be removed.
Loading history...
22
    {
23
        $string_pattern = substr($pattern, 1, -1);
24
        return is_string($value) && $string_pattern == $value ? [] : false;
25
    }
26
27
    private static function _parseIdentifier($value, $pattern)
2 ignored issues
show
Unused Code introduced by
This method is not used, and could be removed.
Loading history...
Unused Code introduced by
The parameter $pattern 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...
28
    {
29
        return [$value];
30
    }
31
32
    private static function _parseWildcard($value, $pattern)
3 ignored issues
show
Unused Code introduced by
This method is not used, and could be removed.
Loading history...
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...
Unused Code introduced by
The parameter $pattern 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...
33
    {
34
        return [];
35
    }
36
37
    private static function _parseArray($value, $pattern)
1 ignored issue
show
Unused Code introduced by
This method is not used, and could be removed.
Loading history...
38
    {
39
        if(! is_array($value)) {
40
            return false;
41
        }
42
43
        $patterns = array_filter(array_map('trim', split_enclosed(',', '[', ']', substr($pattern, 1, -1))));
44
45
        if(count($patterns) === 0) {
46
            return count($value) === 0 ? [] : false;
47
        }
48
49
        if(count($patterns) > count($value)) {
50
            return false;
51
        }
52
53
        $index = 0;
54
        $results = [];
55
        foreach($value as $v) {
56
            $new = self::parse($v, $patterns[$index]);
57
58
            if($new === false) {
59
                return false;
60
            }
61
62
            $results = array_merge($results, $new);
63
            ++$index;
64
        }
65
66
        return $results;
67
    }
68
69
    private static function _parseCons($value, $pattern)
1 ignored issue
show
Unused Code introduced by
This method is not used, and could be removed.
Loading history...
70
    {
71
        if(! is_array($value)) {
72
            return false;
73
        }
74
75
        $patterns = array_filter(array_map('trim', split_enclosed(':', '(', ')', substr($pattern, 1, -1))));
76
        $last = array_pop($patterns);
77
78
        $results = [];
79
        foreach($patterns as $p) {
80
            if(count($value) == 0) {
81
                return false;
82
            }
83
84
            $new = self::parse(array_shift($value), $p);
85
86
            if($new === false) {
87
                return false;
88
            }
89
90
            $results = array_merge($results, $new);
91
        }
92
93
        $new = self::parse($value, $last);
94
95
        return $new === false ? false : array_merge($results, $new);
96
    }
97
98
    /**
99
     * @param mixed $value
100
     * @param string $pattern
101
     * @return bool|array
102
     */
103
    private static function parse($value, $pattern)
104
    {
105
        $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...
106
107
        if(is_numeric($pattern) && is_numeric($value)) {
108
            return [];
109
        }
110
111
        foreach(self::$rules as $regex => $method) {
112
            if(preg_match($regex, $pattern)) {
113
                $arguments = call_user_func_array(['static', $method], [$value, $pattern]);
114
115
                if($arguments !== false) {
116
                    return $arguments;
117
                }
118
            }
119
        }
120
121
        return false;
122
    }
123
124
    /**
125
     * @param mixed $value
126
     * @param array $patterns
127
     * @return mixed
128
     */
129
    public static function match($value, array $patterns)
130
    {
131
        foreach($patterns as $pattern => $callback) {
132
            $match = self::parse($value, $pattern);
133
134
            if($match !== false) {
135
                return call_user_func_array($callback, $match);
136
            }
137
        }
138
139
        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...
140
    }
141
}