Skip to content

[Config] Add conditional types to conditional builders #46581

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

Conversation

wouterj
Copy link
Member

@wouterj wouterj commented Jun 4, 2022

Q A
Branch? 5.4
Bug fix? yes
New feature? no
Deprecations? no
Tickets Fix #46546
License MIT
Doc PR -

On second thought, one of the main benefits of using the new PHP config builders in application code is the ability to use PHP community tooling to autocomplete and validate the configuration. In this light, I think it makes sense to add the correct PHPdocs to make Psalm and PHPstan understand the config builders.

On top of that, as these config classes are live generated, this is not something that can be easily stubbed in the special PHPstan and Psalm Symfony plugins.

Comment on lines 147 to 148
* @psalm-param T $value
* @psalm-return (T is array ? CLASS : static)
Copy link
Member Author

@wouterj wouterj Jun 4, 2022

Choose a reason for hiding this comment

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

Using vendor prefixed annotations as e.g. PhpStorm has no support for conditional types yet afaik. This allows using @return with something everyone understands, while providing the necessary extra details to static analysis tools.

Copy link
Member

Choose a reason for hiding this comment

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

let's still move this after @return in all cases :)

Copy link
Member Author

Choose a reason for hiding this comment

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

Let's just say I was a bit unfocused yesterday 😄

@wouterj wouterj force-pushed the issue-46546/static-analysis-configbuilder branch 2 times, most recently from fbf15a8 to e69e509 Compare June 10, 2022 10:05
This allows static analysis tools to understand what type is returned.
@wouterj wouterj force-pushed the issue-46546/static-analysis-configbuilder branch from e69e509 to d8500c1 Compare June 10, 2022 11:09
@@ -285,7 +293,11 @@ public function NAME(array $value = []): CLASS
} else {
$body = $hasNormalizationClosures ? '
/**
* @template T
* @psalm-param T $value
Copy link
Member

Choose a reason for hiding this comment

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

should be $VALUE to be replaced by the appropriate name.

@@ -85,7 +93,11 @@ public function listObject($value = [])
}

/**
* @template T
* @psalm-param T $value
Copy link
Member

Choose a reason for hiding this comment

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

shouldn't these all be just @param?

@wouterj
Copy link
Member Author

wouterj commented Jul 30, 2022

I've re-submitted this PR against the 6.2 branch: #47127

As the config builder class has had a refactor, creating a new PR was easier :)

@wouterj wouterj closed this Jul 30, 2022
@wouterj wouterj deleted the issue-46546/static-analysis-configbuilder branch July 30, 2022 13:29
nicolas-grekas added a commit that referenced this pull request Aug 1, 2022
…wouterj)

This PR was merged into the 6.2 branch.

Discussion
----------

[Config] Add conditional types to conditional builders

| Q             | A
| ------------- | ---
| Branch?       | 6.2
| Bug fix?      | no
| New feature?  | no
| Deprecations? | no
| Tickets       | Fix #46546
| License       | MIT
| Doc PR        | n/a

Replaces #46581 , submitted to 6.2 instead. This matches with the changes proposed in #47016 and the config builder contained quite a big refactor for comments in 6.x so this avoids nasty conflicts.

Original description:

> One of the main benefits of using the new PHP config builders in application code is the ability to use PHP community tooling to autocomplete and validate the configuration. In this light, I think it makes sense to add the correct PHPdocs to make Psalm and PHPstan understand the config builders.
>
> On top of that, as these config classes are live generated, this is not something that can be easily stubbed in the special PHPstan and Psalm Symfony plugins.

Commits
-------

ce1087e [Config] Add conditional types to conditional builders
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.

4 participants