Skip to content

[9.x] Add predefined_constants to reservedNames array#42832

Merged
taylorotwell merged 2 commits into
laravel:9.xfrom
AbdlrahmanSaberAbdo:add-predefined-constants-to-reserved-names
Jun 17, 2022
Merged

[9.x] Add predefined_constants to reservedNames array#42832
taylorotwell merged 2 commits into
laravel:9.xfrom
AbdlrahmanSaberAbdo:add-predefined-constants-to-reserved-names

Conversation

@AbdlrahmanSaberAbdo

Copy link
Copy Markdown
Contributor

What


This PR is for adding predefined_constants to reservedNames array in a generator command. for example:

php artisan make:model __FILE__

Why


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

For example, if there's a developer who wants to create a model called __FILE__
image

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 __FILE__ -a

image

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.

Note


I asked myself why developers even name classes with underscores but when I checked the reservedName array I found the __halt_compiler keyword prevented before so that encourages me to add predefined_constants in PHP to prevent it also.

Screenshot


image

@taylorotwell taylorotwell merged commit edb438a into laravel:9.x Jun 17, 2022
@GrahamCampbell GrahamCampbell changed the title Add predefined_constants to reservedNames array [9.x] Add predefined_constants to reservedNames array Jun 17, 2022
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.

2 participants