Skip to content

Commit

Permalink
Prevent fatal error on reserved keywords as migration names.
Browse files Browse the repository at this point in the history
  • Loading branch information
dereuromark committed Nov 29, 2024
1 parent 89b6331 commit a52db17
Showing 1 changed file with 30 additions and 1 deletion.
31 changes: 30 additions & 1 deletion src/Command/BakeSimpleMigrationCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,14 @@ abstract class BakeSimpleMigrationCommand extends SimpleBakeCommand
{
public const DEFAULT_MIGRATION_FOLDER = 'Migrations';

protected const RESERVED_KEYWORDS = [
'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', 'for', 'foreach',
'function', 'global', 'goto', 'if', 'implements', 'include', 'include_once', 'instanceof', 'insteadof', 'interface',
'isset', 'list', 'namespace', 'new', 'or', 'parent', 'private', 'protected', 'public', 'return','static',
];

/**
* path to Migration directory
*
Expand Down Expand Up @@ -117,8 +125,18 @@ public function bake(string $name, Arguments $args, ConsoleIo $io): void
{
$this->io = $io;
$this->args = $args;
if ($this->isReservedKeyword($name)) {
$prefix = $io->ask('Reserved keywords cannot be used for class names. What prefix would you like to use, defaults to `Migration`', 'Migration');
if (!$prefix) {
$io->err('You must provide a prefix when using a reserved keyword as name');
$this->abort();
}

$name = $prefix . ucfirst($name);
}

$migrationWithSameName = glob($this->getPath($args) . '*_' . $name . '.php');
if (!empty($migrationWithSameName)) {
if ($migrationWithSameName) {
$force = $args->getOption('force');
if (!$force) {
$io->abort(
Expand Down Expand Up @@ -218,4 +236,15 @@ public function buildOptionParser(ConsoleOptionParser $parser): ConsoleOptionPar

return $parser;
}

/**
* If reserved PHP keyword.
*
* @param string $name
* @return bool
*/
protected function isReservedKeyword(string $name): bool
{
return in_array(strtolower($name), static::RESERVED_KEYWORDS);
}
}

0 comments on commit a52db17

Please sign in to comment.