Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Idea] config(...) call to typed Config::* calls #279

Open
canvural opened this issue Dec 13, 2024 · 3 comments
Open

[Idea] config(...) call to typed Config::* calls #279

canvural opened this issue Dec 13, 2024 · 3 comments

Comments

@canvural
Copy link
Contributor

Hello 👋🏽

Laravel 11 introduced typed config retrieval methods like Config::string, Config::integer etc.

We could then convert config('foo.bar') to Config::string('foo.bar') We need to parse the config file and determine the actual type to be able to do this.

@GeniJaho
Copy link
Collaborator

This would be such a cool rule to have. I wonder how would we determine the type from the config file though.

  • One way would be to check for the default value of calls like env('TEST_VAR', false)
  • For hardcoded config values it would be easy
  • We'd have issues with float and int types

@peterfox
Copy link
Collaborator

I've thought about this as well in the past. It's the same for the Request object as well.

The biggest problem is being sure what the intention of the user is. It could be that it's a configurable rule that makes it more obvious e.g. you specify, ['foo.bar' => 'string'] but that doesn't feel very smart. The other option would be to use the configs themselves to make an educated guess but that could run into problems. I feel like there might be a middle ground for this, namely a mix of the two.

We know that Laravel predefines certain Laravel config keys, so we could have a compiled list of keys from the framework. We could also introduce a command that would do an analysis of the keys in the config. This command could then try to guess the appropriate type and build the config, then the end user can edit the config as appropriate.

@xHeaven
Copy link

xHeaven commented Dec 31, 2024

If a configuration value accepts string|null, Config::string(...) is doomed if the value is null.
Or if the configuration value accepts multiple types, for example, which you can't guess just by looking at it. E.g.: string|int|null
Could be a good idea, but I doubt it can be automatized in such a manner without any kind of compromise.

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

No branches or pull requests

4 participants