ConfigGenerator::generate()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 18
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 2.032

Importance

Changes 0
Metric Value
dl 0
loc 18
ccs 8
cts 10
cp 0.8
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 9
nc 2
nop 2
crap 2.032
1
<?php
2
3
namespace Jumilla\Generators\Php;
4
5
class ConfigGenerator
6
{
7
    /**
8
     * Generate php source text.
9
     *
10
     * @param array $config
11
     * @param string $namespace
12
     *
13
     * @return static
14
     */
15 3
    public static function generateText(array $config, $namespace = null)
16
    {
17 3
        $instance = new static();
18
19 3
        return $instance->generate($config, $namespace);
20
    }
21
22
    /**
23
     * @var string
24
     */
25
    protected $text;
26
27
    /**
28
     * @var int
29
     */
30
    protected $indent;
31
32
    /**
33
     * Generate php source text.
34
     *
35
     * @param array $config
36
     * @param string $namespace
37
     *
38
     * @return string
39
     */
40 5
    public function generate(array $config, $namespace = null)
41
    {
42 5
        $this->writeLine('<?php'.PHP_EOL);
43
44 5
        if ($namespace) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $namespace of type string|null is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
45
            $this->writeLine("namespace $namespace;".PHP_EOL);
46
        }
47
48 5
        $this->indent = 0;
49
50 5
        $this->writeLine('return [');
51
52 5
        $this->generateArray($config);
53
54 5
        $this->writeLine('];');
55
56 5
        return $this->text;
57
    }
58
59
    /**
60
     * Generate php array elements
61
     *
62
     * @param array $config
63
     */
64 5
    private function generateArray(array $config)
65
    {
66 5
        ++$this->indent;
67
68 5
        foreach ($config as $key => $value) {
69 4
            if (is_null($value)) {
70 2
                if (is_string($key)) {
71 1
                    $this->writeLine(sprintf("'%s' => %s,", $key, 'null'));
72 1
                } else {
73 1
                    $this->writeLine(sprintf('%s,', 'null'));
74
                }
75 4
            } elseif (is_bool($value)) {
76 2
                if (is_string($key)) {
77 1
                    $this->writeLine(sprintf("'%s' => %s,", $key, $value ? 'true' : 'false'));
78 1
                } else {
79 1
                    $this->writeLine(sprintf('%s,', $value ? 'true' : 'false'));
80
                }
81 4 View Code Duplication
            } elseif (is_string($value)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
82 4
                if (is_string($key)) {
83 3
                    $this->writeLine(sprintf("'%s' => '%s',", $key, $value));
84 3
                } else {
85 1
                    $this->writeLine(sprintf("'%s',", $value));
86
                }
87 4
            } elseif (is_array($value)) {
88 2
                if (is_string($key)) {
89 1
                    $this->writeLine(sprintf("'%s' => [", $key));
90 1
                } else {
91 1
                    $this->writeLine('[');
92
                }
93
94 2
                $this->generateArray($value);
95
96 2
                $this->writeLine('],');
97 2 View Code Duplication
            } elseif ($value instanceof ClassName) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
98 2
                if (is_string($key)) {
99 1
                    $this->writeLine(sprintf("'%s' => %s,", $key, (string) $value));
100 1
                } else {
101 1
                    $this->writeLine(sprintf('%s,', (string) $value));
102
                }
103 2
            } else {
104 2
                if (is_string($key)) {
105 1
                    $this->writeLine(sprintf("'%s' => %s,", $key, $value));
106 1
                } else {
107 1
                    $this->writeLine($value.',');
108
                }
109
            }
110 5
        }
111
112 5
        --$this->indent;
113 5
    }
114
115
    /**
116
     * Write a source line.
117
     *
118
     * @param string $line
119
     */
120 5
    private function writeLine($line)
121
    {
122 5
        $this->text .= str_repeat(' ', $this->indent * 4);
123 5
        $this->text .= $line;
124 5
        $this->text .= PHP_EOL;
125 5
    }
126
}
127