RendersCode::render()   B
last analyzed

Complexity

Conditions 8
Paths 9

Size

Total Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 8
nc 9
nop 3
dl 0
loc 15
rs 8.4444
c 0
b 0
f 0
1
<?php
2
3
namespace Reallyli\LaravelDeployer\Concerns;
4
5
trait RendersCode
6
{
7
    protected function render($value, $indent = 1, $allow_env = true)
8
    {
9
        switch (gettype($value)) {
10
            case 'array':
11
                return $this->renderArray($value, $indent, $allow_env);
12
            case 'string':
13
                return (starts_with($value, 'env(') && $allow_env)
14
                    ? $value
15
                    : "'".addslashes($value)."'";
16
            case 'boolean':
17
                return $value ? 'true' : 'false';
18
            default:
19
                return is_null($value) ? 'null' : $value;
20
        }
21
    }
22
23
    protected function renderArray($value, $indent, $allow_env)
24
    {
25
        $indentParent = str_repeat('    ', $indent);
26
        $indentChildren = str_repeat('    ', $indent + 1);
27
28
        if (empty($value)) {
29
            return "[\n$indentChildren//\n$indentParent]";
30
        }
31
32
        $arrayContent = collect($value)
33
            ->map(function ($v, $k) use ($indent, $allow_env, $indentChildren) {
34
                $v = $this->render($v, $indent + 1, $allow_env);
35
36
                return is_string($k)
37
                    ? "$indentChildren'$k' => $v"
38
                    : "$indentChildren$v";
39
            })
40
            ->implode(",\n");
41
42
        return "[\n$arrayContent,\n$indentParent]";
43
    }
44
}
45