Passed
Pull Request — master (#14)
by
unknown
02:46
created

varexport()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 6
nc 2
nop 2
dl 0
loc 7
ccs 6
cts 6
cp 1
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
declare(strict_types=1);
3
4
namespace Northwoods\Config;
5
6
/**
7
 * @param mixed $default
8
 * @return mixed
9
 */
10
function array_path(array $config, string $dotPath, $default = null)
0 ignored issues
show
Coding Style introduced by
As per coding-style, this function should be in camelCase.

CamelCase (...) is the practice of writing compound words or phrases such that
each word or abbreviation begins with a capital letter.

Learn more about camelCase.

Loading history...
Coding Style introduced by
Expected 2 blank lines before function; 1 found
Loading history...
11
{
12
    // Working from the first key, descend into the array until nothing is found
13 3
    return array_reduce(
14 3
        explode('.', $dotPath),
15
        function ($config, $key) use ($default) {
16 3
            return isset($config[$key]) ? $config[$key] : $default;
17 3
        },
18 3
        $config
19
    );
20
}
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
21
22
/**
23
 * @param mixed $value
24
 */
25
function array_path_set(array $config, string $dotPath, $value): array
0 ignored issues
show
Coding Style introduced by
As per coding-style, this function should be in camelCase.

CamelCase (...) is the practice of writing compound words or phrases such that
each word or abbreviation begins with a capital letter.

Learn more about camelCase.

Loading history...
26
{
27
    // Work backward from the last key, wrapping each key in an array
28 3
    $replace = array_reduce(
29 3
        array_reverse(explode('.', $dotPath)),
30
        function ($value, $key) {
31 3
            return [$key => $value];
32 3
        },
33 3
        $value
34
    );
35
36
    // Overwrite the array with the replacement
37 3
    return array_replace_recursive($config, $replace);
38
}
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
39
40
/**
41
 * alternative to var_export that exports an array to short array syntax
42
 * copyright disclaimer: orignally written by stemar: [https://gist.github.com/stemar/bb7c5cd2614b21b624bf57608f995ac0]
43
 */
44
function varexport($expression, $return=FALSE) {
0 ignored issues
show
Coding Style introduced by
Incorrect spacing between argument "$return" and equals sign; expected 1 but found 0
Loading history...
Coding Style introduced by
Incorrect spacing between default value and equals sign for argument "$return"; expected 1 but found 0
Loading history...
Coding Style introduced by
TRUE, FALSE and NULL must be lowercase; expected false, but found FALSE.
Loading history...
45 1
    $export = var_export($expression, TRUE);
0 ignored issues
show
Coding Style introduced by
TRUE, FALSE and NULL must be lowercase; expected true, but found TRUE.
Loading history...
46 1
    $export = preg_replace("/^([ ]*)(.*)/m", '$1$1$2', $export);
47 1
    $array = preg_split("/\r\n|\n|\r/", $export);
48 1
    $array = preg_replace(["/\s*array\s\($/", "/\)(,)?$/", "/\s=>\s$/"], [NULL, ']$1', ' => ['], $array);
0 ignored issues
show
Coding Style introduced by
TRUE, FALSE and NULL must be lowercase; expected null, but found NULL.
Loading history...
49 1
    $export = join(PHP_EOL, array_filter(["["] + $array));
50 1
    if ((bool)$return) return $export; else echo $export;
0 ignored issues
show
Coding Style Best Practice introduced by
It is generally a best practice to always use braces with control structures.

Adding braces to control structures avoids accidental mistakes as your code changes:

// Without braces (not recommended)
if (true)
    doSomething();

// Recommended
if (true) {
    doSomething();
}
Loading history...
Coding Style introduced by
As per coding-style, a cast statement should be followed by a single space.
Loading history...
Coding Style introduced by
It is generally recommended to place each PHP statement on a line by itself.

Let’s take a look at an example:

// Bad
$a = 5; $b = 6; $c = 7;

// Good
$a = 5;
$b = 6;
$c = 7;
Loading history...
51
}
52