Skip to content

Commit

Permalink
minor #54279 [HttpFoundation] Improve the return type of ParameterBag…
Browse files Browse the repository at this point in the history
…::getEnum (stof)

This PR was merged into the 7.1 branch.

Discussion
----------

[HttpFoundation] Improve the return type of ParameterBag::getEnum

| Q             | A
| ------------- | ---
| Branch?       | 7.1
| Bug fix?      | no
| New feature?  | no
| Deprecations? | no
| Issues        | n/a
| License       | MIT

The return value can only be null when using null as default value. when providing an enum instance as default value, we know that the return value will always be an enum instance.

This will help projects using static analysis tools. See https://phpstan.org/r/885d8322-b16c-4ebb-97ab-e5babfa8b692 which corresponds to the behavior with this PR applied vs https://phpstan.org/r/14693953-bad8-4e61-b026-527bf33cf342 which corresponds to the current behavior.

Commits
-------

6fbacb6 Improve the return type of ParameterBag::getEnum
  • Loading branch information
nicolas-grekas committed Mar 13, 2024
2 parents 1d43999 + 6fbacb6 commit 20d5453
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/Symfony/Component/HttpFoundation/InputBag.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,8 @@ public function set(string $key, mixed $value): void
* @param ?T $default
*
* @return ?T
*
* @psalm-return ($default is null ? T|null : T)
*/
public function getEnum(string $key, string $class, ?\BackedEnum $default = null): ?\BackedEnum
{
Expand Down
2 changes: 2 additions & 0 deletions src/Symfony/Component/HttpFoundation/ParameterBag.php
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,8 @@ public function getBoolean(string $key, bool $default = false): bool
* @param ?T $default
*
* @return ?T
*
* @psalm-return ($default is null ? T|null : T)
*/
public function getEnum(string $key, string $class, ?\BackedEnum $default = null): ?\BackedEnum
{
Expand Down

0 comments on commit 20d5453

Please sign in to comment.