Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

[HttpFoundation] Add nullable getters for InputBag/ParameterBag #53668

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

Closed
wants to merge 2 commits into from

Conversation

VincentLanglet
Copy link
Contributor

@VincentLanglet VincentLanglet commented Jan 29, 2024

Q A
Branch? 7.1
Bug fix? no
New feature? yes
Deprecations? no
Issues Fix #...
License MIT

Rework of the #49972 PR.

The getNullable* are different from the

return $this->has($key) ? $this->getString($key) : null;

because the has check is using array_key_exists.
So in case of a custom bag build with null values, the getNullable method will return null, when the has/get* will cast the value (or throw an error in some cases).

The getNullable* is also different from the \FILTER_NULL_ON_FAILURE flag because because getNullable* methods are still validating non-null values and throwing exception (when the FILTER_NULL_ON_FAILURE flag fallback to null).

So currently, the getNullable* behavior is not an easy one to produce with the given methods from Symfony.
The #45775 issue was closed without the nullable getters, but this was still a requested feature.

@VincentLanglet
Copy link
Contributor Author

Fabbot.io error is unrelated https://fabbot.io/report/symfony/symfony/53668/f6d3c61717fd06c8a5380bc2afbde0fa7efae2ce

But I can fix it if wanted.

@nicolas-grekas
Copy link
Member

nicolas-grekas commented Jan 29, 2024

We shouldn't have made a difference between "null" and "unset". Is there a path toward this instead?
About the name: getStringOrNull (so that nobody has to wonder about what a "nullable string" is)
But I'd prefer not adding the methods, this API feels bloated...

@VincentLanglet
Copy link
Contributor Author

We shouldn't have made a difference between "null" and "unset". Is there a path toward this instead?

It would require to change

    public function get(string $key, mixed $default = null): mixed
    {
        return \array_key_exists($key, $this->parameters) ? $this->parameters[$key] : $default;
    }

    public function has(string $key): bool
    {
        return \array_key_exists($key, $this->parameters);
    }

to

    public function get(string $key, mixed $default = null): mixed
    {
        return $this->parameters[$key] ?? $default;
    }

    public function has(string $key): bool
    {
        return isset($this->parameters[$key]);
    }

This would a BC-break, I don't know the impact.

The usage of array_key_exist seems here since SF2 with the commit d568437 from @fabpot.

I don't know why this behavior was preferred, but there are some tests about it @nicolas-grekas, like
https://github.com/symfony/symfony/blob/7.1/src/Symfony/Component/HttpFoundation/Tests/HeaderBagTest.php#L107-L108
so maybe it's important for headers (?)

About the name: getStringOrNull (so that nobody has to wonder about what a "nullable string" is)

I made the change.

/**
* Returns the parameter value converted to boolean or null.
*/
public function getBooleanOrNull(string $key, ?bool $default = null): ?bool
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We use Boolean (long) but Int (short), what about using either getIntegerOrNull or getBoolOrNull?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I agree this is weird, but the decision should be taken first on getBoolean and getInt.
I'll be happy to update my PR as soon as a decision is taken first on which method between getBoolean and getInt should be deprecated (in favor of getBool or getInteger)

@OskarStark OskarStark changed the title [HttpFoundation] Add nullable getters for InputBag/ParameterBag [HttpFoundation] Add nullable getters for InputBag/ParameterBag Jan 29, 2024
@sstok
Copy link
Contributor

sstok commented Jan 29, 2024

The usage of array_key_exist seems here since SF2 with the commit d568437 from @fabpot.

This is the safest way to ensure a key in the array actual exists, because isset() works on the value and gives false when the key doesn't exist (unset) or is null.

@VincentLanglet
Copy link
Contributor Author

The usage of array_key_exist seems here since SF2 with the commit d568437 from @fabpot.

This is the safest way to ensure a key in the array actual exists, because isset() works on the value and gives false when the key doesn't exist (unset) or is null.

Yes, but @nicolas-grekas said

We shouldn't have made a difference between "null" and "unset".

If Symfony doesn't want to make a difference between null and unset, the method isset should be preferred over array_key_exist.

@VincentLanglet
Copy link
Contributor Author

Not sure about the status of this PR @nicolas-grekas @OskarStark, is there something I need to change ?

I answered #53668 (comment) and #53668 (comment) and was waiting about your feedback again :)

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

Successfully merging this pull request may close these issues.

6 participants
Morty Proxy This is a proxified and sanitized view of the page, visit original site.