Skip to content

Commit 74e1266

Browse files
Error on using reserved keyword for migration name (#2328)
Co-authored-by: Mark Scherer <[email protected]>
1 parent 47462b3 commit 74e1266

File tree

2 files changed

+32
-0
lines changed

2 files changed

+32
-0
lines changed

src/Phinx/Console/Command/Create.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,15 @@ class Create extends AbstractCommand
3636
*/
3737
public const CREATION_INTERFACE = 'Phinx\Migration\CreationInterface';
3838

39+
// PHP keywords from https://www.php.net/manual/en/reserved.keywords.php
40+
private array $keywords = [
41+
'abstract', 'and', 'array', 'as', 'break', 'callable', 'case', 'catch', 'class', 'clone', 'const',
42+
'continue', 'declare', 'default', 'die', 'do', 'echo', 'else', 'elseif', 'empty', 'enddeclare', 'endfor',
43+
'endforeach', 'endif', 'endswitch', 'endwhile', 'eval', 'exit', 'extends', 'final', 'finally', 'for', 'foreach',
44+
'function', 'global', 'goto', 'if', 'implements', 'include', 'include_once', 'instanceof', 'insteadof', 'interface',
45+
'isset', 'list', 'namespace', 'new', 'or', 'parent', 'private', 'protected', 'public', 'return','static',
46+
];
47+
3948
/**
4049
* {@inheritDoc}
4150
*
@@ -167,6 +176,13 @@ protected function execute(InputInterface $input, OutputInterface $output): int
167176

168177
$path = realpath($path);
169178
$className = $input->getArgument('name');
179+
if ($className !== null && in_array(strtolower($className), $this->keywords)) {
180+
throw new InvalidArgumentException(sprintf(
181+
'The migration class name "%s" is a reserved PHP keyword. Please choose a different class name.',
182+
$className
183+
));
184+
}
185+
170186
$offset = 0;
171187
do {
172188
$timestamp = Util::getCurrentTimestamp($offset++);

tests/Phinx/Console/Command/CreateTest.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -613,4 +613,20 @@ public function testCreateMigrationWithInvalidStyleFlagThrows(): void
613613
$exitCode = $commandTester->execute(['command' => $command->getName(), '--style' => 'foo']);
614614
$this->assertSame(AbstractCommand::CODE_ERROR, $exitCode);
615615
}
616+
617+
public function testCreateWithKeywordNameThrows(): void
618+
{
619+
$application = new PhinxApplication();
620+
$application->add(new Create());
621+
622+
/** @var Create $command */
623+
$command = $application->find('create');
624+
$command->setConfig($this->config);
625+
626+
$commandTester = new CommandTester($command);
627+
628+
$this->expectException(InvalidArgumentException::class);
629+
$this->expectExceptionMessage('The migration class name "Class" is a reserved PHP keyword. Please choose a different class name.');
630+
$commandTester->execute(['command' => $command->getName(), 'name' => 'Class']);
631+
}
616632
}

0 commit comments

Comments
 (0)