Passed
Push — master ( d76f9c...e93f22 )
by Adrien
02:31
created

Debug::export()   A

Complexity

Conditions 5
Paths 4

Size

Total Lines 26
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 16
CRAP Score 5.005

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
eloc 17
c 1
b 0
f 0
nc 4
nop 3
dl 0
loc 26
ccs 16
cts 17
cp 0.9412
crap 5.005
rs 9.3888
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Ecodev\Felix;
6
7
final class Debug
8
{
9
    /**
10
     * Export variables omitting array keys that are strictly numeric
11
     *
12
     * By default will output result
13
     *
14
     * @param mixed $data
15
     *
16
     * @return string string representation of variable
17
     */
18 6
    public static function export($data, bool $return = false, int $level = 0): string
19
    {
20 6
        $result = '';
21 6
        if (is_array($data)) {
22 4
            $needKey = array_keys($data) !== range(0, count($data) - 1);
23 4
            $result .= '[' . PHP_EOL;
24 4
            foreach ($data as $key => $value) {
25 4
                $result .= str_repeat(' ', 4 * ($level + 1));
26 4
                if ($needKey) {
27 3
                    $result .= self::export($key, true, $level + 1);
28 3
                    $result .= ' => ';
29
                }
30
31 4
                $result .= self::export($value, true, $level + 1);
32 4
                $result .= ',' . PHP_EOL;
33
            }
34 4
            $result .= str_repeat(' ', 4 * $level) . ']';
35
        } else {
36 6
            $result .= var_export($data, true);
37
        }
38
39 6
        if (!$return) {
40
            echo $result;
41
        }
42
43 6
        return $result;
44
    }
45
}
46