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

Argument::setSpread()   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 Argument extends DependencyAwareGenerator implements FunctionMemberInterface
8
{
9
    /**
10
     * Special value to represent that there is no argument passed.
11
     */
12
    public const NO_PARAM = INF;
13
14
    private string  $type;
15
    private string  $name;
16
    private bool    $isSpread = false;
17
    private bool    $isByReference = false;
18
    private bool    $isNullable = false;
19
20
    /**
21
     * @var mixed
22
     */
23
    private $defaultValue;
24
25
    /**
26
     * Argument constructor.
27
     *
28
     * @param mixed $defaultValue
29
     */
30
    public function __construct(string $name, string $type = '', $defaultValue = self::NO_PARAM)
31
    {
32
        $this->name = $name;
33
        $this->type = $this->resolveQualifier($type);
34
35
        if (INF !== $defaultValue) {
36
            $this->setDefaultValue($defaultValue);
37
        }
38
    }
39
40
    public static function new(string $name, string $type = '', $defaultValue = self::NO_PARAM): self
41
    {
42
        return new self($name, $type, $defaultValue);
43
    }
44
45
    public function generate(): string
46
    {
47
        $code = '';
48
49
        if ($this->type) {
50
            if ($this->isNullable && '?' !== $this->type[0]) {
51
                $code .= '?';
52
            }
53
            $code .= $this->type.' ';
54
        }
55
56
        if ($this->isByReference) {
57
            $code .= '&';
58
        }
59
60
        if ($this->isSpread) {
61
            $code .= '...';
62
        }
63
64
        $code .= ('$' === $this->name[0]) ? $this->name : "$$this->name";
65
66
        if ($this->defaultValue) {
67
            $code .= " = $this->defaultValue";
68
        }
69
70
        return $code;
71
    }
72
73
    public function __toString(): string
74
    {
75
        return $this->generate();
76
    }
77
78
    public function isSpread(): bool
79
    {
80
        return $this->isSpread;
81
    }
82
83
    public function setSpread(): self
84
    {
85
        $this->isSpread = true;
86
87
        return $this;
88
    }
89
90
    public function unsetSpread(): self
91
    {
92
        $this->isSpread = false;
93
94
        return $this;
95
    }
96
97
    public function isByReference(): bool
98
    {
99
        return $this->isByReference;
100
    }
101
102
    public function setByReference(): self
103
    {
104
        $this->isByReference = true;
105
106
        return $this;
107
    }
108
109
    public function unsetByReference(): self
110
    {
111
        $this->isByReference = false;
112
113
        return $this;
114
    }
115
116
    public function setType(string $type): self
117
    {
118
        $this->type = $type;
119
120
        return $this;
121
    }
122
123
    public function setDefaultValue($value): self
124
    {
125
        $this->defaultValue = Utils::stringify($value);
126
127
        return $this;
128
    }
129
130
    public function unsetNullable(): self
131
    {
132
        $this->isNullable = false;
133
134
        return $this;
135
    }
136
137
    public function setNullable(): self
138
    {
139
        $this->isNullable = true;
140
141
        return $this;
142
    }
143
}
144