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 ( 9d5c74...11439f )
by Gilles
02:02
created

functions.php ➔ match()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 16
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 9
nc 4
nop 2
dl 0
loc 16
rs 9.2
c 0
b 0
f 0
1
<?php
2
3
namespace FunctionalPHP\PatternMatching;
4
5
/**
6
 * Destructure the given value using the given pattern, then returns
7
 * the resulting values as an array indexed using the identifiers of
8
 * the pattern.
9
 *
10
 * If the extraction failed, will return False.
11
 *
12
 * @param mixed $value
13
 * @param string $pattern
14
 * @return array|bool
0 ignored issues
show
Documentation introduced by
Should the return type not be integer?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
15
 */
16
function extract($value, $pattern)
17
{
18
    return (new Parser())->parse($value, $pattern);
19
}
20
21
/**
22
 * Given a value and an array with the format <pattern> => <callback>,
23
 * matches the value to the first pattern possible and execute the
24
 * callback by passing the arguments destructured from the value.
25
 *
26
 * @param mixed $value
27
 * @param array $patterns <pattern> => <callback>
28
 * @return mixed
29
 */
30
function match($value, array $patterns)
31
{
32
    $parser = new Parser();
33
34
    foreach($patterns as $pattern => $callback) {
35
        $match = $parser->parse($value, $pattern);
36
37
        if($match !== false) {
38
            return is_callable($callback) ?
39
                call_user_func_array($callback, $match) :
40
                $callback;
41
        }
42
    }
43
44
    throw new \RuntimeException('Non-exhaustive patterns.');
45
}
46
47
/**
48
 * Helper function to split a string using a given delimiter except
49
 * if said delimiter is enclosed between two different characters.
50
 *
51
 * This won't work if the opening and closing character for the
52
 * enclosure is the same (ie quotes), $open and $close need to
53
 * be different.
54
 *
55
 * The enclosing can have multiple depth. Each opening character needs
56
 * to be closed by exactly one closing character. No balancing is done.
57
 *
58
 * @param string $delimiter one character that will be the delimiter
59
 * @param string $open one character that starts the enclosing
60
 * @param string $close one character that stops the enclosing
61
 * @param string $string the string to split
62
 * @return array|string[] The split result.
63
 */
64
function split_enclosed($delimiter, $open, $close, $string)
65
{
66
    $chars = str_split($string);
67
68
    $result = [];
69
    $buffer = '';
70
    $level = 0;
71
    foreach($chars as $c) {
72
        if($c === $delimiter && $level === 0) {
73
            $result[] = $buffer;
74
            $buffer = '';
75
        } else {
76
            $buffer .= $c;
77
78
            if($c === $open) {
79
                ++$level;
80
            } else if($c === $close) {
81
                --$level;
82
            }
83
        }
84
    }
85
86
    if(strlen($buffer) > 0) {
87
        $result[] = $buffer;
88
    }
89
90
    return $result;
91
}
92
93