helpers.php ➔ varexport()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 2
dl 0
loc 20
rs 9.6
c 0
b 0
f 0
1
<?php declare(strict_types=1);
2
3
namespace MazenTouati\Simple2wayConfig;
4
5
/*
6
* copyright disclaimer: orignally written by stemar:
7
* [ https://gist.github.com/stemar/bb7c5cd2614b21b624bf57608f995ac0 ]
8
*/
9
10
/**
11
 * Alternative to var_export that exports an array to short array syntax
12
 * @param  mixed $expression variable to export
13
 * @param  boolean $return whenever to return or print the result
14
 * @return string|void
15
 */
16
function varexport($expression, $return = false)
17
{
18
    $export = var_export($expression, true);
19
    $export = preg_replace("/^([ ]*)(.*)/m", '$1$1$2', $export);
20
    $array = preg_split("/\r\n|\n|\r/", $export);
21
22
    $array = preg_replace(
23
        ["/\s*array\s\($/", "/\)(,)?$/", "/\s=>\s$/"],
24
        [null, ']$1', ' => ['],
25
        $array
26
    );
27
28
    $export = join(PHP_EOL, array_filter(["["] + $array));
29
30
    if ((bool)$return) {
31
        return $export;
32
    } else {
33
        echo $export;
34
    }
35
}
36