AbstractConfig::createConfigKey()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 1
1
<?php
2
/**
3
 * Sandro Keil (https://sandro-keil.de)
4
 *
5
 * @link      http://github.com/sandrokeil/interop-config for the canonical source repository
6
 * @copyright Copyright (c) 2017-2020 Sandro Keil
7
 * @license   http://github.com/sandrokeil/interop-config/blob/master/LICENSE.md New BSD License
8
 */
9
10
namespace Interop\Config\Tool;
11
12
class AbstractConfig
13
{
14 1
    protected function prepareConfig(iterable $config, int $indentLevel = 1): string
15
    {
16 1
        $indent = str_repeat(' ', $indentLevel * 4);
17 1
        $entries = [];
18
19 1
        foreach ($config as $key => $value) {
20 1
            $key = $this->createConfigKey($key);
21 1
            $entries[] = sprintf(
22 1
                '%s%s%s,',
23
                $indent,
24 1
                $key ? sprintf('%s => ', $key) : '',
25 1
                $this->createConfigValue($value, $indentLevel)
26
            );
27
        }
28
29 1
        $outerIndent = str_repeat(' ', ($indentLevel - 1) * 4);
30
31 1
        return sprintf("[\n%s\n%s]", implode("\n", $entries), $outerIndent);
32
    }
33
34 1
    private function createConfigKey($key): string
35
    {
36 1
        return sprintf("'%s'", $key);
37
    }
38
39 1
    private function createConfigValue($value, int $indentLevel): string
40
    {
41 1
        if (is_array($value) || $value instanceof \Traversable) {
42 1
            return $this->prepareConfig($value, $indentLevel + 1);
43
        }
44
45 1
        if (is_string($value) && class_exists($value)) {
46 1
            return sprintf('\\%s::class', ltrim($value, '\\'));
47
        }
48
49 1
        return var_export($value, true);
50
    }
51
}
52