Test Setup Failed
Pull Request — master (#3)
by Timur
05:47 queued 03:49
created

Property::setPrivate()   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
nc 1
nop 0
dl 0
loc 5
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Murtukov\PHPCodeGenerator;
6
7
class Property extends DependencyAwareGenerator
8
{
9
    /**
10
     * Special value to represent that there is no argument passed.
11
     */
12
    public const NO_PARAM = INF;
13
14
    public string   $name;
15
    public ?Comment $docBlock = null;
16
    public bool     $isNullable = false;
17
    public bool     $isStatic = false;
18
    public bool     $isConst = false;
19
20
    private string $value = '';
21
    private string $modifier;
22
    private string $typeHint;
23
24
    public function __construct(string $name, ?string $modifier, string $typeHint = '', $defaultValue = self::NO_PARAM)
25
    {
26
        $this->name = $name;
27
        $this->modifier = $modifier ?? Modifier::PUBLIC;
28
        $this->typeHint = $this->resolveQualifier($typeHint);
29
30
        if (INF !== $defaultValue) {
31
            $this->value = Utils::stringify($defaultValue);
32
33
            if (null === $defaultValue) {
34
                $this->isNullable = true;
35
            }
36
        }
37
    }
38
39
    public static function new(
40
        string $name,
41
        ?string $modifier = Modifier::PUBLIC,
42
        string $typeHint = '',
43
        $value = self::NO_PARAM
44
    ) {
45
        return new static($name, $modifier, $typeHint, $value);
46
    }
47
48
    public function generate(): string
49
    {
50
        $docBlock = $this->docBlock ? "$this->docBlock\n" : '';
51
        $value = $this->value ? " = $this->value" : '';
52
        $isStatic = $this->isStatic ? 'static ' : '';
53
54
        $typeHint = '';
55
        if ($this->typeHint) {
56
            if ($this->isNullable) {
57
                $typeHint = "?";
58
            }
59
60
            $typeHint .= "$this->typeHint ";
61
        }
62
63
        if ($this->isConst) {
64
            return "$docBlock$this->modifier const $this->name$value";
65
        }
66
67
        return "{$docBlock}{$this->modifier} {$isStatic}{$typeHint}$$this->name{$value}";
68
    }
69
70
    public function getName(): string
71
    {
72
        return $this->name;
73
    }
74
75
    public function setName(string $name): self
76
    {
77
        $this->name = $name;
78
79
        return $this;
80
    }
81
82
    public function getModifier(): string
83
    {
84
        return $this->modifier;
85
    }
86
87
    public function getDefaultValue(): string
88
    {
89
        return $this->value;
90
    }
91
92
    public function setDefaultValue($value): self
93
    {
94
        $this->value = Utils::stringify($value);
95
96
        return $this;
97
    }
98
99
    public function setStatic(): self
100
    {
101
        $this->isStatic = true;
102
        $this->isConst = false;
103
104
        return $this;
105
    }
106
107
    public function unsetStatic(): self
108
    {
109
        $this->isStatic = false;
110
111
        return $this;
112
    }
113
114
    public function setConst(): self
115
    {
116
        $this->isConst = true;
117
        $this->isStatic = false;
118
119
        return $this;
120
    }
121
122
    public function unsetConst()
123
    {
124
        $this->isConst = false;
125
126
        return $this;
127
    }
128
129
    public function setPublic(): self
130
    {
131
        $this->modifier = 'public';
132
133
        return $this;
134
    }
135
136
    public function setPrivate(): self
137
    {
138
        $this->modifier = 'private';
139
140
        return $this;
141
    }
142
143
    public function setProtected(): self
144
    {
145
        $this->modifier = 'protected';
146
147
        return $this;
148
    }
149
150
    public function getTypeHint(): string
151
    {
152
        return $this->typeHint;
153
    }
154
155
    public function setTypeHint(string $typeHint): self
156
    {
157
        $this->typeHint = $this->resolveQualifier($typeHint);
158
159
        return $this;
160
    }
161
162
    public function setNullable()
163
    {
164
        $this->isNullable = true;
165
    }
166
167
    public function unsetNullable()
168
    {
169
        $this->isNullable = false;
170
    }
171
}
172