16
16
*
17
17
* @author David Grudl
18
18
*
19
+ * @method PhpNamespace getNamespace()
19
20
* @method ClassType setName(string)
20
21
* @method string getName()
21
22
* @method ClassType setType(string)
45
46
*/
46
47
class ClassType extends Nette \Object
47
48
{
49
+ const TYPE_CLASS = 'class ' ;
50
+
51
+ const TYPE_INTERFACE = 'interface ' ;
52
+
53
+ const TYPE_TRAIT = 'trait ' ;
54
+
55
+ /** @var PhpNamespace */
56
+ private $ namespace ;
57
+
48
58
/** @var string */
49
59
private $ name ;
50
60
@@ -79,7 +89,10 @@ class ClassType extends Nette\Object
79
89
private $ methods = array ();
80
90
81
91
82
- /** @return ClassType */
92
+ /**
93
+ * @param \ReflectionClass|string
94
+ * @return ClassType
95
+ */
83
96
public static function from ($ from )
84
97
{
85
98
$ from = $ from instanceof \ReflectionClass ? $ from : new \ReflectionClass ($ from );
@@ -116,29 +129,41 @@ public static function from($from)
116
129
}
117
130
118
131
119
- public function __construct ($ name = NULL )
132
+ public function __construct ($ name = NULL , PhpNamespace $ namespace = NULL )
120
133
{
121
134
$ this ->name = $ name ;
135
+ $ this ->namespace = $ namespace ;
122
136
}
123
137
124
138
125
- /** @return ClassType */
139
+ /**
140
+ * @param string
141
+ * @param mixed
142
+ * @return ClassType
143
+ */
126
144
public function addConst ($ name , $ value )
127
145
{
128
146
$ this ->consts [$ name ] = $ value ;
129
147
return $ this ;
130
148
}
131
149
132
150
133
- /** @return Property */
151
+ /**
152
+ * @param string
153
+ * @param mixed
154
+ * @return Property
155
+ */
134
156
public function addProperty ($ name , $ value = NULL )
135
157
{
136
158
$ property = new Property ;
137
159
return $ this ->properties [$ name ] = $ property ->setName ($ name )->setValue ($ value );
138
160
}
139
161
140
162
141
- /** @return Method */
163
+ /**
164
+ * @param string
165
+ * @return Method
166
+ */
142
167
public function addMethod ($ name )
143
168
{
144
169
$ method = new Method ;
@@ -151,35 +176,63 @@ public function addMethod($name)
151
176
}
152
177
153
178
154
- /** @return string PHP code */
179
+ /**
180
+ * @return string PHP code
181
+ */
155
182
public function __toString ()
156
183
{
157
184
$ consts = array ();
158
185
foreach ($ this ->consts as $ name => $ value ) {
159
186
$ consts [] = "const $ name = " . Helpers::dump ($ value ) . "; \n" ;
160
187
}
188
+
161
189
$ properties = array ();
162
190
foreach ($ this ->properties as $ property ) {
163
- $ properties [] = ($ property ->documents ? str_replace ("\n" , "\n * " , "/** \n" . implode ("\n" , (array ) $ property ->documents )) . "\n */ \n" : '' )
164
- . $ property ->visibility . ($ property ->static ? ' static ' : '' ) . ' $ ' . $ property ->name
191
+ $ properties [] = ($ property ->getDocuments () ? str_replace ("\n" , "\n * " , "/** \n" . implode ("\n" , (array ) $ property ->getDocuments () )) . "\n */ \n" : '' )
192
+ . $ property ->getVisibility () . ($ property ->isStatic () ? ' static ' : '' ) . ' $ ' . $ property ->getName ()
165
193
. ($ property ->value === NULL ? '' : ' = ' . Helpers::dump ($ property ->value ))
166
194
. "; \n" ;
167
195
}
196
+
197
+ $ extends = $ implements = $ traits = array ();
198
+
199
+ if ($ this ->namespace ) {
200
+ $ fqnToAlias = array_flip ($ this ->namespace ->getUses ());
201
+
202
+ foreach ((array ) $ this ->extends as $ fqn ) {
203
+ $ extends [] = isset ($ fqnToAlias [$ fqn ]) ? $ fqnToAlias [$ fqn ] : '\\' . $ fqn ;
204
+ }
205
+
206
+ foreach ((array ) $ this ->implements as $ fqn ) {
207
+ $ implements [] = isset ($ fqnToAlias [$ fqn ]) ? $ fqnToAlias [$ fqn ] : '\\' . $ fqn ;
208
+ }
209
+
210
+ foreach ((array ) $ this ->traits as $ fqn ) {
211
+ $ traits [] = isset ($ fqnToAlias [$ fqn ]) ? $ fqnToAlias [$ fqn ] : '\\' . $ fqn ;
212
+ }
213
+
214
+ } else {
215
+ $ extends = (array ) $ this ->extends ;
216
+ $ implements = (array ) $ this ->implements ;
217
+ $ traits = (array ) $ this ->traits ;
218
+ }
219
+
168
220
return Strings::normalize (
169
221
($ this ->documents ? str_replace ("\n" , "\n * " , "/** \n" . implode ("\n" , (array ) $ this ->documents )) . "\n */ \n" : '' )
170
222
. ($ this ->abstract ? 'abstract ' : '' )
171
223
. ($ this ->final ? 'final ' : '' )
172
224
. $ this ->type . ' '
173
225
. $ this ->name . ' '
174
- . ($ this ->extends ? 'extends ' . implode (', ' , ( array ) $ this -> extends ) . ' ' : '' )
175
- . ($ this ->implements ? 'implements ' . implode (', ' , ( array ) $ this -> implements ) . ' ' : '' )
226
+ . ($ this ->extends ? 'extends ' . implode (', ' , $ extends ) . ' ' : '' )
227
+ . ($ this ->implements ? 'implements ' . implode (', ' , $ implements ) . ' ' : '' )
176
228
. "\n{ \n\n"
177
229
. Strings::indent (
178
- ($ this ->traits ? " use " . implode (', ' , ( array ) $ this -> traits ) . "; \n\n" : '' )
230
+ ($ this ->traits ? ' use ' . implode (', ' , $ traits ) . "; \n\n" : '' )
179
231
. ($ this ->consts ? implode ('' , $ consts ) . "\n\n" : '' )
180
232
. ($ this ->properties ? implode ("\n" , $ properties ) . "\n\n" : '' )
181
233
. implode ("\n\n\n" , $ this ->methods ), 1 )
182
- . "\n\n} " ) . "\n" ;
234
+ . "\n\n} "
235
+ ) . "\n" ;
183
236
}
184
237
185
238
}
0 commit comments