VariableExporter   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 33
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
eloc 16
dl 0
loc 33
ccs 0
cts 21
cp 0
rs 10
c 0
b 0
f 0
wmc 5

1 Method

Rating   Name   Duplication   Size   Complexity  
A export() 0 28 5
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Doctrine\ORM\Mapping\Exporter;
6
7
use const PHP_EOL;
8
use function array_keys;
9
use function array_reduce;
10
use function implode;
11
use function is_array;
12
use function is_numeric;
13
use function ltrim;
14
use function sprintf;
15
use function str_pad;
16
use function str_repeat;
17
use function strlen;
18
use function var_export;
19
20
class VariableExporter implements Exporter
21
{
22
    /**
23
     * {@inheritdoc}
24
     */
25
    public function export($value, int $indentationLevel = 0) : string
26
    {
27
        if (! is_array($value)) {
28
            return var_export($value, true);
29
        }
30
31
        $indentation  = str_repeat(self::INDENTATION, $indentationLevel);
32
        $longestKey   = array_reduce(array_keys($value), static function ($k, $v) {
33
            return (string) (strlen((string) $k) > strlen((string) $v) ? $k : $v);
34
        });
35
        $maxKeyLength = strlen($longestKey) + (is_numeric($longestKey) ? 0 : 2);
36
37
        $lines = [];
38
39
        $lines[] = $indentation . '[';
40
41
        foreach ($value as $entryKey => $entryValue) {
42
            $lines[] = sprintf(
43
                '%s%s => %s,',
44
                $indentation . self::INDENTATION,
45
                str_pad(var_export($entryKey, true), $maxKeyLength),
46
                ltrim($this->export($entryValue, $indentationLevel + 1))
47
            );
48
        }
49
50
        $lines[] = $indentation . ']';
51
52
        return implode(PHP_EOL, $lines);
53
    }
54
}
55