Passed
Branch master (da5350)
by Timur
02:19
created

Instance::setInline()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 5
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Murtukov\PHPCodeGenerator;
6
7
class Instance extends DependencyAwareGenerator
8
{
9
    private array  $args;
10
    private string $qualifier;
11
    public bool   $multiline = false;
12
13
    public function __construct(string $qualifier, ...$args)
14
    {
15
        $this->qualifier = $this->resolveQualifier($qualifier);
16
        $this->args = $args;
17
    }
18
19
    /**
20
     * @throws Exception\UnrecognizedValueTypeException
21
     */
22
    public function generate(): string
23
    {
24
        if (empty($this->args)) {
25
            return "new $this->qualifier()";
26
        }
27
28
        if ($this->multiline) {
29
            $args = "\n";
30
            $suffix = ",\n";
31
        } else {
32
            $args = '';
33
            $suffix = ', ';
34
        }
35
36
        foreach ($this->args as $arg) {
37
            $args .= Utils::stringify($arg).$suffix;
0 ignored issues
show
Bug introduced by
Are you sure Murtukov\PHPCodeGenerator\Utils::stringify($arg) of type false|string can be used in concatenation? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

37
            $args .= /** @scrutinizer ignore-type */ Utils::stringify($arg).$suffix;
Loading history...
38
        }
39
40
        if ($this->multiline) {
41
            $args = Utils::indent($args, false);
42
        }
43
44
        $args = rtrim($args, ', ');
45
46
        return "new $this->qualifier($args)";
47
    }
48
49
    public function addArgument($arg): self
50
    {
51
        $this->args[] = $arg;
52
53
        return $this;
54
    }
55
56
    public static function multiline(string $qualifier, ...$args)
57
    {
58
        $instance = new self($qualifier, ...$args);
59
        $instance->multiline = true;
60
61
        return $instance;
62
    }
63
64
    public static function new(string $qualifier, ...$args)
65
    {
66
        return new self($qualifier, ...$args);
67
    }
68
69
    public function setMultiline(): self
70
    {
71
        $this->multiline = true;
72
73
        return $this;
74
    }
75
76
    public function setInline()
77
    {
78
        $this->multiline = false;
79
80
        return $this;
81
    }
82
}
83