Skip to content

[7.x] Disallow generation commands with reserved names#33037

Merged
taylorotwell merged 2 commits intolaravel:7.xfrom
musa11971:disallow-php-keywords-in-generation
Jun 1, 2020
Merged

[7.x] Disallow generation commands with reserved names#33037
taylorotwell merged 2 commits intolaravel:7.xfrom
musa11971:disallow-php-keywords-in-generation

Conversation

@musa11971
Copy link
Copy Markdown
Contributor

What

This PR makes it so that an error is displayed to the user when they attempt to use a reserved keyword in a generator command. For example:

php artisan make:model Case

Why

Using PHP keywords as the name when using generation commands is not only bad practice, but it simply does not even work.

In my situation I wanted to create a model called Case (in the context of a "crime case").

This will not work. A context sensitive lexer was added in PHP 7, which allows you to use reserved keywords in method names. However, this does not go for class names.

Some could say:

"Developers should just be cautious and not use keywords when scaffolding."

I partially agree, but when you use the following generation command for example:

php artisan make:model Case -a

It will successfully create a model class, factory, migration, seeder, and then... oops:

And now your project is polluted with a model class, factory, migration and seeder, while the model isn't usable in the first place. It would be much better to disallow the scaffolding immediately and prevent this.

Screenshot

The error that is displayed when attempting to scaffold with a reserved name.

@musa11971 musa11971 changed the title Disallow generation commands with reserved names [7.x] Disallow generation commands with reserved names May 31, 2020
@anouarabdsslm
Copy link
Copy Markdown
Contributor

@musa11971 you may need to add a test

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants