Test Failed
Push — master ( c51706...f968f3 )
by Anton
01:54
created

helpers.php ➔ array_to_string()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 1
dl 0
loc 10
ccs 0
cts 5
cp 0
crap 2
rs 9.9332
c 0
b 0
f 0
1
<?php
2
/* (c) Anton Medvedev <[email protected]>
3
 *
4
 * For the full copyright and license information, please view the LICENSE
5
 * file that was distributed with this source code.
6
 */
7
8
namespace Deployer\Support;
9
10
/**
11
 * Flatten array
12
 *
13
 * @param array $array
14
 * @return array
15
 */
16
function array_flatten(array $array)
17
{
18 11
    $flatten = [];
19
    array_walk_recursive($array, function ($value) use (&$flatten) {
20 11
        $flatten[] = $value;
21 11
    });
22 11
    return $flatten;
23
}
24
25
/**
26
 * Recursively merge two config arrays with a specific behavior:
27
 *
28
 * 1. scalar values are overridden
29
 * 2. array values are extended uniquely if all keys are numeric
30
 * 3. all other array values are merged
31
 *
32
 * @param array $original
33
 * @param array $override
34
 * @return array
35
 * @see http://stackoverflow.com/a/36366886/6812729
36
 */
37
function array_merge_alternate(array $original, array $override)
38
{
39 8
    foreach ($override as $key => $value) {
40 8
        if (isset($original[$key])) {
41 5
            if (!is_array($original[$key])) {
42 5
                if (is_numeric($key)) {
43
                    // Append scalar value
44 5
                    $original[] = $value;
45
                } else {
46
                    // Override scalar value
47 5
                    $original[$key] = $value;
48
                }
49 3
            } elseif (array_keys($original[$key]) === range(0, count($original[$key]) - 1)) {
50
                // Uniquely append to array with numeric keys
51 3
                $original[$key] = array_unique(array_merge($original[$key], $value));
52
            } else {
53
                // Merge all other arrays
54 5
                $original[$key] = array_merge_alternate($original[$key], $value);
55
            }
56
        } else {
57
            // Simply add new key/value
58 8
            $original[$key] = $value;
59
        }
60
    }
61
62 8
    return $original;
63
}
64
65
/**
66
 * Determines if the given string contains the given value.
67
 *
68
 * @param string $haystack
69
 * @param string $needle
70
 * @return bool
71
 */
72
function str_contains(string $haystack, string $needle)
73
{
74
    return strpos($haystack, $needle) !== false;
75
}
76
77
/**
78
 * Take array of key/value and create string of it.
79
 *
80
 * This function used for create environment string.
81
 *
82
 * @param array $array
83
 *
84
 * @return string
85
 */
86
function array_to_string(array $array): string
87
{
88
    return implode(' ', array_map(
89
        function ($key, $value) {
90
            return sprintf("%s='%s'", $key, $value);
91
        },
92
        array_keys($array),
93
        $array
94
    ));
95
}
96