|
1
|
|
|
<?php declare(strict_types = 1); |
|
2
|
|
|
/** |
|
3
|
|
|
* Created by Vitaly Iegorov <[email protected]>. |
|
4
|
|
|
* on 03.09.16 at 09:58 |
|
5
|
|
|
*/ |
|
6
|
|
|
namespace samsonphp\generator; |
|
7
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* Class generator class. |
|
10
|
|
|
* |
|
11
|
|
|
* @author Vitaly Egorov <[email protected]> |
|
12
|
|
|
*/ |
|
13
|
|
|
class ClassGenerator extends GenericGenerator |
|
14
|
|
|
{ |
|
15
|
|
|
/** OOP public visibility */ |
|
16
|
|
|
const VISIBILITY_PUBLIC = 'public'; |
|
17
|
|
|
|
|
18
|
|
|
/** OOP protected visibility */ |
|
19
|
|
|
const VISIBILITY_PROTECTED = 'protected'; |
|
20
|
|
|
|
|
21
|
|
|
/** OOP private visibility */ |
|
22
|
|
|
const VISIBILITY_PRIVATE = 'private'; |
|
23
|
|
|
|
|
24
|
|
|
/** @var string Class name */ |
|
25
|
|
|
protected $className; |
|
26
|
|
|
|
|
27
|
|
|
/** @var string Class namespace */ |
|
28
|
|
|
protected $namespace; |
|
29
|
|
|
|
|
30
|
|
|
/** @var array Collection of class uses */ |
|
31
|
|
|
protected $uses; |
|
32
|
|
|
|
|
33
|
|
|
/** @var array Multiline class comment */ |
|
34
|
|
|
protected $classComment; |
|
35
|
|
|
|
|
36
|
|
|
/** @var array Multiline file description */ |
|
37
|
|
|
protected $fileDescription; |
|
38
|
|
|
|
|
39
|
|
|
/** @var array Class constants */ |
|
40
|
|
|
protected $constants; |
|
41
|
|
|
|
|
42
|
|
|
/** @var array Class static properties */ |
|
43
|
|
|
protected $staticProperties; |
|
44
|
|
|
|
|
45
|
|
|
/** @var array Class static methods */ |
|
46
|
|
|
protected $staticMethods; |
|
47
|
|
|
|
|
48
|
|
|
/** @var array Class properties */ |
|
49
|
|
|
protected $properties; |
|
50
|
|
|
|
|
51
|
|
|
/** @var array Class methods */ |
|
52
|
|
|
protected $methods; |
|
53
|
|
|
|
|
54
|
|
|
/** @var bool Flag that class is abstract */ |
|
55
|
|
|
protected $isAbstract; |
|
56
|
|
|
|
|
57
|
|
|
/** @var bool Flag that class is final */ |
|
58
|
|
|
protected $isFinal; |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* ClassGenerator constructor. |
|
62
|
|
|
* |
|
63
|
|
|
* @param GenericGenerator $parent Parent generator |
|
64
|
|
|
* @param string $className Class name |
|
65
|
|
|
*/ |
|
66
|
|
|
public function __construct(GenericGenerator $parent, string $className) |
|
67
|
|
|
{ |
|
68
|
|
|
$this->className = $className; |
|
69
|
|
|
|
|
70
|
|
|
parent::__construct($parent); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* Set class to be final. |
|
75
|
|
|
* |
|
76
|
|
|
* @return ClassGenerator |
|
77
|
|
|
*/ |
|
78
|
|
|
public function defFinal() : ClassGenerator |
|
79
|
|
|
{ |
|
80
|
|
|
if ($this->isAbstract) { |
|
81
|
|
|
throw new \InvalidArgumentException('Class cannot be final as it is already abstract'); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
$this->isFinal = true; |
|
85
|
|
|
|
|
86
|
|
|
return $this; |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
/** |
|
90
|
|
|
* Set class to be abstract. |
|
91
|
|
|
* |
|
92
|
|
|
* @return ClassGenerator |
|
93
|
|
|
*/ |
|
94
|
|
|
public function defAbstract() : ClassGenerator |
|
95
|
|
|
{ |
|
96
|
|
|
if ($this->isFinal) { |
|
97
|
|
|
throw new \InvalidArgumentException('Class cannot be abstract as it is already final'); |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
$this->isAbstract = true; |
|
101
|
|
|
|
|
102
|
|
|
return $this; |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
/** |
|
106
|
|
|
* Set class file description. |
|
107
|
|
|
* |
|
108
|
|
|
* @param string $description |
|
109
|
|
|
* |
|
110
|
|
|
* @return ClassGenerator |
|
111
|
|
|
*/ |
|
112
|
|
|
public function defDescription(string $description) : ClassGenerator |
|
113
|
|
|
{ |
|
114
|
|
|
$this->fileDescription = $description; |
|
|
|
|
|
|
115
|
|
|
|
|
116
|
|
|
return $this; |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
/** |
|
120
|
|
|
* Set class namespace. |
|
121
|
|
|
* |
|
122
|
|
|
* @param string $namespace |
|
123
|
|
|
* |
|
124
|
|
|
* @return ClassGenerator |
|
125
|
|
|
*/ |
|
126
|
|
|
public function defNamespace(string $namespace) : ClassGenerator |
|
127
|
|
|
{ |
|
128
|
|
|
$this->namespace = $namespace; |
|
129
|
|
|
|
|
130
|
|
|
return $this; |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
/** |
|
134
|
|
|
* Set class use. |
|
135
|
|
|
* |
|
136
|
|
|
* @param string $use |
|
137
|
|
|
* |
|
138
|
|
|
* @return ClassGenerator |
|
139
|
|
|
*/ |
|
140
|
|
|
public function defUse(string $use) : ClassGenerator |
|
141
|
|
|
{ |
|
142
|
|
|
$this->uses[] = $use; |
|
143
|
|
|
|
|
144
|
|
|
return $this; |
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
|
|
/** |
|
148
|
|
|
* Set class property. |
|
149
|
|
|
* |
|
150
|
|
|
* @param string $name Property name |
|
151
|
|
|
* @param mixed $value Property value |
|
152
|
|
|
* @param array $comment Property PHPDOC comment strings collection |
|
153
|
|
|
* @param string $visibility Property visibility |
|
154
|
|
|
* @param bool $static Flag that property is static |
|
155
|
|
|
* |
|
156
|
|
|
* @return ClassGenerator |
|
157
|
|
|
*/ |
|
158
|
|
|
public function defProperty( |
|
159
|
|
|
string $name, |
|
160
|
|
|
$value, |
|
161
|
|
|
array $comment = [], |
|
162
|
|
|
string $visibility = self::VISIBILITY_PUBLIC, |
|
163
|
|
|
bool $static = false |
|
164
|
|
|
) : ClassGenerator { |
|
165
|
|
|
if ($static) { |
|
166
|
|
|
$collection = &$this->staticProperties[$name]; |
|
167
|
|
|
} else { |
|
168
|
|
|
$collection = &$this->properties[$name]; |
|
169
|
|
|
} |
|
170
|
|
|
|
|
171
|
|
|
$collection[] = [$comment, $static, $visibility, $value]; |
|
172
|
|
|
|
|
173
|
|
|
return $this; |
|
174
|
|
|
} |
|
175
|
|
|
|
|
176
|
|
|
/** |
|
177
|
|
|
* Set protected class property. |
|
178
|
|
|
* |
|
179
|
|
|
* @param string $name Property name |
|
180
|
|
|
* @param mixed $value Property value |
|
181
|
|
|
* @param array $comment Property PHPDOC comment strings collection |
|
182
|
|
|
* |
|
183
|
|
|
* @return ClassGenerator |
|
184
|
|
|
*/ |
|
185
|
|
|
public function defProtectedProperty(string $name, $value, array $comment = []) |
|
186
|
|
|
{ |
|
187
|
|
|
return $this->defProperty($name, $value, $comment, self::VISIBILITY_PROTECTED); |
|
188
|
|
|
} |
|
189
|
|
|
|
|
190
|
|
|
/** |
|
191
|
|
|
* Set static class property. |
|
192
|
|
|
* |
|
193
|
|
|
* @param string $name Property name |
|
194
|
|
|
* @param mixed $value Property value |
|
195
|
|
|
* @param array $comment Property PHPDOC comment strings collection |
|
196
|
|
|
* @param string $visibility Property visibility |
|
197
|
|
|
* |
|
198
|
|
|
* @return ClassGenerator |
|
199
|
|
|
*/ |
|
200
|
|
|
public function defStaticProperty( |
|
201
|
|
|
string $name, |
|
202
|
|
|
$value, |
|
203
|
|
|
array $comment = [], |
|
204
|
|
|
string $visibility = self::VISIBILITY_PUBLIC |
|
205
|
|
|
) : ClassGenerator { |
|
206
|
|
|
return $this->defProperty($name, $value, $comment, $visibility, true); |
|
207
|
|
|
} |
|
208
|
|
|
|
|
209
|
|
|
/** |
|
210
|
|
|
* Set protected static class property. |
|
211
|
|
|
* |
|
212
|
|
|
* @param string $name Property name |
|
213
|
|
|
* @param mixed $value Property value |
|
214
|
|
|
* @param array $comment Property PHPDOC comment strings collection |
|
215
|
|
|
* |
|
216
|
|
|
* @return ClassGenerator |
|
217
|
|
|
*/ |
|
218
|
|
|
public function defProtectedStaticProperty(string $name, $value, array $comment = []) |
|
219
|
|
|
{ |
|
220
|
|
|
return $this->defStaticProperty($name, $value, $comment, self::VISIBILITY_PROTECTED); |
|
221
|
|
|
} |
|
222
|
|
|
} |
|
223
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..