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 ( 11439f...c98a24 )
by Gilles
01:55
created

functions.php ➔ split_enclosed()   D

Complexity

Conditions 10
Paths 3

Size

Total Lines 37
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 10
eloc 24
nc 3
nop 4
dl 0
loc 37
rs 4.8196
c 0
b 0
f 0

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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
 * The given string will be trimmed as will all values in the
52
 * resulting array. An empty string will result in an empty
53
 * array. If at any time a string that should be added to the
54
 * result is empty, the function will return false instead.
55
 *
56
 * This won't work if the opening and closing character for the
57
 * enclosure is the same (ie quotes), $open and $close need to
58
 * be different.
59
 *
60
 * The enclosing can have multiple depth. Each opening character needs
61
 * to be closed by exactly one closing character. No balancing is done.
62
 *
63
 * @param string $delimiter one character that will be the delimiter
64
 * @param string $open one character that starts the enclosing
65
 * @param string $close one character that stops the enclosing
66
 * @param string $string the string to split
67
 * @return array|string[]|bool The split result, false if any of the value was empty
68
 */
69
function split_enclosed($delimiter, $open, $close, $string)
70
{
71
    $string = trim($string);
1 ignored issue
show
Coding Style introduced by
Consider using a different name than the parameter $string. This often makes code more readable.
Loading history...
72
    if(strlen($string) === 0) {
73
        return [];
74
    }
75
76
    $chars = str_split($string);
77
78
    $result = array_reduce($chars, function($acc, $c) use($delimiter, $open, $close) {
79
        if($acc === false) {
80
            return $acc;
81
        }
82
83
        switch($c) {
84
            case $delimiter:
85
                if($acc[2] === 0) {
86
                    return strlen(trim($acc[1])) === 0 ?
87
                        false :
88
                        [array_merge($acc[0], [trim($acc[1])]), '', 0];
89
                }
90
                break;
91
            case $open:
92
                return [$acc[0], $acc[1].$c, $acc[2] + 1];
93
            case $close:
94
                return [$acc[0], $acc[1].$c, $acc[2] - 1];
95
        }
96
97
        return [$acc[0], $acc[1].$c, $acc[2]];
98
    }, [[], '', 0]);
99
100
    if($result === false || strlen(trim($result[1])) === 0) {
101
        return false;
102
    }
103
104
    return array_merge($result[0], [trim($result[1])]);
105
}
106
107