15
15
16
16
class Generator
17
17
{
18
+ public const RESERVED_KEYWORDS = ['__halt_compiler ' , 'abstract ' , 'and ' , 'array ' , 'as ' , 'break ' , 'callable ' , 'case ' , 'catch ' , 'class ' , 'clone ' , 'const ' , 'continue ' , 'declare ' , 'default ' , 'die ' , 'do ' , 'echo ' , 'else ' , 'elseif ' , 'empty ' , 'enddeclare ' , 'endfor ' , 'endforeach ' , 'endif ' , 'endswitch ' , 'endwhile ' , 'eval ' , 'exit ' , 'extends ' , 'final ' , 'finally ' , 'fn ' , 'for ' , 'foreach ' , 'function ' , 'global ' , 'goto ' , 'if ' , 'implements ' , 'include ' , 'include_once ' , 'instanceof ' , 'insteadof ' , 'interface ' , 'isset ' , 'list ' , 'match ' , 'namespace ' , 'new ' , 'or ' , 'print ' , 'private ' , 'protected ' , 'public ' , 'readonly ' , 'require ' , 'require_once ' , 'return ' , 'static ' , 'switch ' , 'throw ' , 'trait ' , 'try ' , 'unset ' , 'use ' , 'var ' , 'while ' , 'xor ' , 'yield ' , 'yield_from ' ];
19
+ public const RESERVED_CONSTANTS = ['__CLASS__ ' , '__DIR__ ' , '__FILE__ ' , '__FUNCTION__ ' , '__LINE__ ' , '__METHOD__ ' , '__NAMESPACE__ ' , '__TRAIT__ ' ];
20
+
18
21
/**
19
22
* Checks if template is bundle
20
23
*
@@ -107,7 +110,7 @@ public static function generate(object $body, array $args): PhpFile
107
110
*/
108
111
private static function class (object $ object , array $ args , mixed &$ parent = null ): ClassType
109
112
{
110
- $ class = !$ parent ? new ClassType (Parser::parseString ($ object ->name , $ args )) : $ parent ->addClass (Parser::parseString ($ object ->name , $ args ));
113
+ $ class = !$ parent ? new ClassType (self :: makeNameSafe ( Parser::parseString ($ object ->name , $ args ))) : $ parent ->addClass (self :: makeNameSafe ( Parser::parseString ($ object ->name , $ args) ));
111
114
112
115
return self ::defineObject ($ object , $ args , $ class );
113
116
}
@@ -120,7 +123,7 @@ private static function class(object $object, array $args, mixed &$parent = null
120
123
*/
121
124
private static function interface (object $ object , array $ args , mixed &$ parent = null ): ClassType
122
125
{
123
- $ interface = !$ parent ? ClassType::interface (Parser::parseString ($ object ->name , $ args )) : $ parent ->addInterface (Parser::parseString ($ object ->name , $ args ));
126
+ $ interface = !$ parent ? ClassType::interface (self :: makeNameSafe ( Parser::parseString ($ object ->name , $ args ))) : $ parent ->addInterface (self :: makeNameSafe ( Parser::parseString ($ object ->name , $ args) ));
124
127
125
128
return self ::defineObject ($ object , $ args , $ interface );
126
129
}
@@ -133,7 +136,7 @@ private static function interface(object $object, array $args, mixed &$parent =
133
136
*/
134
137
private static function trait (object $ object , array $ args , mixed &$ parent = null ): ClassType
135
138
{
136
- $ trait = !$ parent ? ClassType::trait (Parser::parseString ($ object ->name , $ args )) : $ parent ->addTrait (Parser::parseString ($ object ->name , $ args ));
139
+ $ trait = !$ parent ? ClassType::trait (self :: makeNameSafe ( Parser::parseString ($ object ->name , $ args ))) : $ parent ->addTrait (self :: makeNameSafe ( Parser::parseString ($ object ->name , $ args) ));
137
140
138
141
return self ::defineObject ($ object , $ args , $ trait );
139
142
}
@@ -163,7 +166,7 @@ private static function defineObject(object $object, array $args, ClassType $cla
163
166
}
164
167
if (self ::is ($ object ->constants )) {
165
168
foreach ($ object ->constants as $ const ) {
166
- $ constant = $ class ->addConstant (Parser::parseString ($ const ->name , $ args ), Parser::parseString ($ const ->value , $ args ));
169
+ $ constant = $ class ->addConstant (self :: makeNameSafe ( Parser::parseString ($ const ->name , $ args) ), Parser::parseString ($ const ->value , $ args ));
167
170
if (self ::is ($ const ->visibility )) $ constant ->setVisibility ($ const ->visibility );
168
171
if (self ::is ($ const ->comments )) {
169
172
foreach ($ const ->comments as $ comment ) {
@@ -195,7 +198,7 @@ private static function defineObject(object $object, array $args, ClassType $cla
195
198
196
199
private static function namespace (object $ object , PhpFile &$ file , array $ args ): PhpNamespace
197
200
{
198
- $ namespace = $ file ->addNamespace (Parser::parseString ($ object ->name , $ args ));
201
+ $ namespace = $ file ->addNamespace (self :: makeNameSafe ( Parser::parseString ($ object ->name , $ args) ));
199
202
200
203
if (self ::is ($ object ->use )) {
201
204
foreach ($ object ->use as $ use ) {
@@ -251,7 +254,7 @@ private static function property(object $prop, array $args, ?ClassType $class =
251
254
252
255
private static function getter (string $ name , string $ type ): Method
253
256
{
254
- $ getter = new Method ('get ' . ucfirst ($ name ));
257
+ $ getter = new Method (self :: makeNameSafe ( 'get ' . ucfirst ($ name) ));
255
258
$ getter ->addComment ("@return $ type " );
256
259
257
260
$ getter ->setReturnType ($ type );
@@ -262,7 +265,7 @@ private static function getter(string $name, string $type): Method
262
265
263
266
private static function setter (string $ name , string $ type ): Method
264
267
{
265
- $ setter = new Method ('set ' . ucfirst ($ name ));
268
+ $ setter = new Method (self :: makeNameSafe ( 'set ' . ucfirst ($ name) ));
266
269
$ setter ->addComment ("@param $ type \$$ name " );
267
270
268
271
$ param = $ setter ->addParameter ($ name );
@@ -346,4 +349,28 @@ public static function is(mixed &$subject): bool
346
349
{
347
350
return is_array ($ subject ) ? isset ($ subject ) && count ($ subject ) > 0 : isset ($ subject );
348
351
}
352
+
353
+ /**
354
+ * Check if the given name is a reserved keyword
355
+ * @param string $name
356
+ * @return bool
357
+ */
358
+ public static function isReservedKeyword (string $ name ): bool
359
+ {
360
+ return in_array ($ name , self ::RESERVED_KEYWORDS ) || in_array ($ name , self ::RESERVED_CONSTANTS );
361
+ }
362
+
363
+ /**
364
+ * Make the given name safe by adding an underscore if it is a reserved keyword
365
+ * @param string $name
366
+ * @return string
367
+ */
368
+ public static function makeNameSafe (string $ name ): string
369
+ {
370
+ $ newName = self ::isReservedKeyword ($ name ) ? '_ ' . $ name : $ name ;
371
+ if (self ::isReservedKeyword ($ newName )) {
372
+ return self ::makeNameSafe ($ newName );
373
+ }
374
+ return $ newName ;
375
+ }
349
376
}
0 commit comments