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 ( 6a9997...b22946 )
by Gilles
07:15
created

Matcher::parse()   B

Complexity

Conditions 8
Paths 5

Size

Total Lines 21
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 21
rs 7.1428
c 0
b 0
f 0
cc 8
eloc 11
nc 5
nop 2
1
<?php
2
3
namespace PHPFunctional\PatternMatching;
4
5
class Matcher
6
{
7
    private static $booleans = "/^(true|false)$/i";
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...
8
    private static $strings = "/^(['\"])(?:(?!\\1).)*\\1$/";
9
10
    /**
11
     * @param mixed $value
12
     * @param string $pattern
13
     * @return bool|array
14
     */
15
    private static function parse($value, $pattern)
16
    {
17
        $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...
18
19
        if(is_numeric($pattern) && is_numeric($value)) {
20
            return [];
21
        }
22
23
        if(preg_match(self::$booleans, $pattern) === 1 && is_bool($value)) {
24
            return [];
25
        }
26
27
        if(preg_match(self::$strings, $pattern) === 1 && is_string($value)) {
28
            $string_pattern = substr($pattern, 1, strlen($pattern) - 2);
29
            if($string_pattern == $value) {
30
                return [];
31
            }
32
        }
33
34
        return false;
35
    }
36
37
    /**
38
     * @param mixed $value
39
     * @param array $patterns
40
     * @return mixed
41
     */
42
    public static function match($value, array $patterns)
43
    {
44
        foreach($patterns as $pattern => $callback) {
45
            $match = self::parse($value, $pattern);
46
47
            if($match !== false) {
48
                return call_user_func_array($callback, $match);
49
            }
50
        }
51
52
        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...
53
    }
54
}