Completed
Push — master ( f81cf6...7fbbb0 )
by Oleg
03:40
created

NsArrayPrinter::__construct()   A

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
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
ccs 2
cts 2
cp 1
crap 1
1
<?php
2
declare(strict_types=1);
3
4
namespace SlayerBirden\DFCodeGeneration\Code\Printer;
5
6
use Nette\PhpGenerator\PhpLiteral;
7
use Nette\PhpGenerator\PhpNamespace;
8
use SlayerBirden\DFCodeGeneration\Util\ArrayUtils;
9
10
final class NsArrayPrinter
11
{
12
    /**
13
     * @var PhpNamespace
14
     */
15
    private $phpNamespace;
16
17 2
    public function __construct(PhpNamespace $phpNamespace)
18
    {
19 2
        $this->phpNamespace = $phpNamespace;
20 2
    }
21
22
    /**
23
     * Prints array with short tags
24
     *
25
     * @param array $list
26
     * @param int $indentationLevel
27
     * @param string $closingChar
28
     * @param null|mixed $key
29
     * @param bool $shouldShowPrefixBracket
30
     * @return string
31
     */
32 2
    public function printArray(
33
        array $list,
34
        int $indentationLevel = 1,
35
        string $closingChar = ",\n",
36
        $key = null,
37
        $shouldShowPrefixBracket = true
38
    ): string {
39 2
        $parts = [];
40 2
        $this->iterateArray($list, $parts, $indentationLevel);
41
42 2
        $bracketsSpaces = $this->getSpaces(($indentationLevel - 1) * 4);
43 2
        if ($key === null) {
44 2
            $keyPart = '';
45
        } else {
46 2
            $keyPart = $this->varExport($key) . ' => ';
47
        }
48
49 2
        if ($shouldShowPrefixBracket) {
50 2
            $prefixBracket = $bracketsSpaces;
51
        } else {
52 2
            $prefixBracket = '';
53
        }
54
55 2
        return $prefixBracket . $keyPart . "[\n" . implode("\n", $parts) . "\n" . $bracketsSpaces . ']' . $closingChar;
56
    }
57
58 2
    private function iterateArray(array $list, array &$parts, int$indentationLevel): void
59
    {
60 2
        $isList = ArrayUtils::isSequential($list);
61 2
        foreach ($list as $itemKey => $item) {
62 2
            if (is_array($item)) {
63 2
                if ($isList) {
64 2
                    $itemKey = null;
65
                }
66 2
                $parts[] = $this->printArray($item, $indentationLevel + 1, ',', $itemKey);
67
            } else {
68 2
                $spacesCount = $indentationLevel * 4;
69 2
                if ($isList) {
70 2
                    $keyLiteral = '';
71
                } else {
72 2
                    $keyLiteral = $this->varExport($itemKey) . ' => ';
73
                }
74 2
                $parts[] = $this->getSpaces($spacesCount) . $keyLiteral . $this->varExport($item) . ',';
75
            }
76
        }
77 2
    }
78
79 2
    private function getSpaces($count): string
80
    {
81 2
        $spaces = '';
82 2
        while ($count > 0) {
83 2
            $spaces .= ' ';
84 2
            $count -= 1;
85
        }
86
87 2
        return $spaces;
88
    }
89
90 2
    private function varExport($variable): string
91
    {
92 2
        if ($variable instanceof PhpLiteral) {
93 2
            if (strpos((string)$variable, '::class') !== false) {
94 2
                $className = str_replace('::class', '', (string)$variable);
95 2
                return $this->phpNamespace->unresolveName($className) . '::class';
96
            } else {
97 2
                return (string)$variable;
98
            }
99
        }
100
        // firs check for ::class strings
101 2
        if (is_string($variable) && (strpos($variable, '::class') !== false)) {
102 2
            $className = str_replace('::class', '', $variable);
103 2
            return $this->phpNamespace->unresolveName($className) . '::class';
104
        }
105
        // then checks for backslash
106 2
        if (is_string($variable) && (strpos($variable, '\\') !== false)) {
107 2
            return $this->phpNamespace->unresolveName($variable) . '::class';
108
        }
109 2
        return var_export($variable, true);
110
    }
111
}
112