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

getParam function improvements #1

Open
intelrug opened this issue Dec 7, 2022 · 0 comments
Open

getParam function improvements #1

intelrug opened this issue Dec 7, 2022 · 0 comments

Comments

@intelrug
Copy link

intelrug commented Dec 7, 2022

return Arr::get($params, $key) ?: Arr::get($params, $fallbackKey);

return Arr::get($this->config, $key) ?: Arr::get($this->config, $fallbackKey);

I think it would be better to use Null coalescing (??) operator here, because the current code does not work correctly with boolean or zero values.
And we need an extra if because Arr::get returns an array if the provided key is null.

So the final code will look something like this:

protected function getParam(array $params, string $key, ?string $fallbackKey = null): mixed
{
    if (Arr::hasAny($params, [$key, $fallbackKey])) {
        return Arr::get($params, $key) ?? Arr::get($params, $fallbackKey);
    }

    if (Arr::hasAny($this->config, [$key, $fallbackKey])) {
        return Arr::get($this->config, $key) ?? Arr::get($this->config, $fallbackKey);
    }

    return null;
}

What do you think?

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

1 participant