Skip to content

Commit ac11036

Browse files
committed
Merge branch 'disallow-php-keywords-in-generation' of https://github.com/musa11971/framework into musa11971-disallow-php-keywords-in-generation
2 parents 1fb3001 + 57de71c commit ac11036

File tree

1 file changed

+41
-1
lines changed

1 file changed

+41
-1
lines changed

src/Illuminate/Console/GeneratorCommand.php

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,24 @@ abstract class GeneratorCommand extends Command
2222
*/
2323
protected $type;
2424

25+
/**
26+
* Reserved names that cannot be used for generation.
27+
*
28+
* @var array
29+
*/
30+
protected $reservedNames = [
31+
'__halt_compiler', 'break', 'clone', 'die', 'empty', 'endswitch', 'final',
32+
'function', 'include', 'isset', 'print', 'require_once', 'trait', 'while',
33+
'abstract', 'callable', 'const', 'do', 'enddeclare', 'endwhile', 'finally',
34+
'global', 'include_once', 'list', 'private', 'return', 'try', 'xor', 'and',
35+
'case', 'continue', 'echo', 'endfor', 'eval', 'fn', 'goto', 'instanceof',
36+
'namespace', 'protected', 'static', 'unset', 'yield', 'array', 'catch',
37+
'declare', 'else', 'endforeach', 'exit', 'for', 'if', 'insteadof', 'new',
38+
'public', 'switch', 'use', 'as', 'class', 'default', 'elseif', 'endif',
39+
'extends', 'foreach', 'implements', 'interface', 'or', 'require', 'throw',
40+
'var',
41+
];
42+
2543
/**
2644
* Create a new controller creator command instance.
2745
*
@@ -51,11 +69,20 @@ abstract protected function getStub();
5169
*/
5270
public function handle()
5371
{
72+
// First we will check whether the name can be found in the reserved names.
73+
// We have so called "reserved names" to ensure that no files are generated
74+
// using PHP keywords for example, because that would cause errors.
75+
if ($this->isReservedName($this->getNameInput())) {
76+
$this->error('The name "'.$this->getNameInput().'" is reserved. Please pick something else.');
77+
78+
return false;
79+
}
80+
5481
$name = $this->qualifyClass($this->getNameInput());
5582

5683
$path = $this->getPath($name);
5784

58-
// First we will check to see if the class already exists. If it does, we don't want
85+
// We will check to see if the class already exists. If it does, we don't want
5986
// to create the class and overwrite the user's code. So, we will bail out so the
6087
// code is untouched. Otherwise, we will continue generating this class' files.
6188
if ((! $this->hasOption('force') ||
@@ -279,4 +306,17 @@ protected function getArguments()
279306
['name', InputArgument::REQUIRED, 'The name of the class'],
280307
];
281308
}
309+
310+
/**
311+
* Checks whether the given name is reserved.
312+
*
313+
* @param string $name
314+
* @return bool
315+
*/
316+
protected function isReservedName($name)
317+
{
318+
$name = strtolower($name);
319+
320+
return in_array($name, $this->reservedNames);
321+
}
282322
}

0 commit comments

Comments
 (0)