Failed Conditions
Pull Request — master (#13)
by Adrien
14:06
created

Debug   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 39
Duplicated Lines 0 %

Test Coverage

Coverage 94.12%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 20
c 1
b 0
f 0
dl 0
loc 39
rs 10
ccs 16
cts 17
cp 0.9412
wmc 7

1 Method

Rating   Name   Duplication   Size   Complexity  
B export() 0 28 7
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) && !$data) {
22 4
            $result .= '[]';
23 4
        } elseif (is_array($data)) {
24 4
            $needKey = array_keys($data) !== range(0, count($data) - 1);
25 4
            $result .= '[' . PHP_EOL;
26 4
            foreach ($data as $key => $value) {
27 3
                $result .= str_repeat(' ', 4 * ($level + 1));
28 3
                if ($needKey) {
29
                    $result .= self::export($key, true, $level + 1);
30
                    $result .= ' => ';
31 4
                }
32 4
33
                $result .= self::export($value, true, $level + 1);
34 4
                $result .= ',' . PHP_EOL;
35
            }
36 6
            $result .= str_repeat(' ', 4 * $level) . ']';
37
        } else {
38
            $result .= var_export($data, true);
39 6
        }
40
41
        if (!$return) {
42
            echo $result;
43 6
        }
44
45
        return $result;
46
    }
47
}
48