Thanks to visit codestin.com
Credit goes to github.com

Skip to content

FrameworkConfig methods not found by PHPStan and psalm #46546

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
ThomasLandauer opened this issue Jun 1, 2022 · 9 comments Β· Fixed by #47127
Closed

FrameworkConfig methods not found by PHPStan and psalm #46546

ThomasLandauer opened this issue Jun 1, 2022 · 9 comments Β· Fixed by #47127

Comments

@ThomasLandauer
Copy link
Contributor

Symfony version(s) affected

5.4.9

Description

Similar problem as in #42342

With a framework.php similar to the example shown at https://symfony.com/doc/current/http_client.html#scoping-client

// config/packages/framework.php
use Symfony\Config\FrameworkConfig;

return static function (FrameworkConfig $framework) {
    $framework->httpClient()->scopedClient('github.client')
        ->scope('https://api\.github\.com')
        ->header('Accept', 'application/vnd.github.v3+json')
        ->header('Authorization', 'token %env(GITHUB_API_TOKEN)%')
    ;
};

...psalm is reporting:

PossiblyUndefinedMethod - config/packages/framework.php - Method Symfony\Config\FrameworkConfig::scopedClient does not exist

and PHPStan:

Call to an undefined method Symfony\Config\Framework\HttpClientConfig|Symfony\Config\FrameworkConfig::scopedClient().

Even though I do have public function scopedClient(string $name, $value = []) in my var/cache/dev/Symfony/Config/Framework/HttpClientConfig.php

So it looks like adding this to phpstan.neon isn't enough anymore:

scanDirectories:
    - var/cache/dev/Symfony/Config

The same error is raised for other packages, e.g.:

  • Call to an undefined method Symfony\Config\Twig\GlobalConfig|Symfony\Config\TwigConfig::value().
  • Call to an undefined method Symfony\Config\Monolog\HandlerConfig|Symfony\Config\MonologConfig::type().

Was there a change recently?

How to reproduce

See above

Possible Solution

No response

Additional Context

No response

@wouterj
Copy link
Member

wouterj commented Jun 1, 2022

Seems like they don't like the unions added on e.g. FrameworkConfig::httpClient() by #46328

This can be improved by using conditional types, but that would make things unnecessary bloated imho. I think the best recommendation is to not static analyze the configuration.

@fabpot
Copy link
Member

fabpot commented Jun 2, 2022

Closing then.

@wouterj
Copy link
Member

wouterj commented Jun 4, 2022

On second thought, I think this might be worth reconsidering (given that one of the main benefits of PHP config is the ability to use industry tools for validating and autocompletion). I've submitted a fix: #46581

nicolas-grekas added a commit that referenced this issue 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
@Tofandel
Copy link
Contributor

Tofandel commented Aug 1, 2022

I might be missing something but I don't have or see a Symfony\Config\FrameworkConfig class anywhere in the source of symfony and in my project it's just in the cache, it makes for a bad development experience because of the missing autocompletion as the cache folder should not be indexed, maybe there should be a real file in symfony that gets overriden in the cache to help with this

@tacman
Copy link
Contributor

tacman commented Sep 20, 2022

It's not a great solution, but you can at least hide it by ignoring it in phpstan.neon:

    ignoreErrors:
            -
                message: '#FrameworkConfig not found#'
                path: packages/

@stof
Copy link
Member

stof commented Sep 21, 2022

@Tofandel those classes are generated on the fly for each bundle in your project. We cannot force each bundle to ship the generated code with their code (and that would be a maintenance nightmare as the generated code might depend on the version of the DI component). And we definitely cannot ship them all in Symfony.

@Tofandel
Copy link
Contributor

Tofandel commented Sep 21, 2022

Yes I figured as much, in the end I set my ide to index the var/cache/local/Symfony directory (I use local env for local dev, test for unit tests, dev for staging and prod for production, so in my case it's local and not dev) so that autocompletion works correctly

For psalm I added to psalm.xml

  <extraFiles>
    <directory name="var/cache/*/Symfony/Config"/>
  </extraFiles>

@ThomasLandauer
Copy link
Contributor Author

Implemented in Symfony 6.2 through #44166

For PHPStan, see https://phpstan.org/user-guide/discovering-symbols#third-party-code-outside-of-composer-dependencies

parameters:
    scanDirectories:
	- var/cache/dev/Symfony/Config

@atakchidi
Copy link

Implemented in Symfony 6.2 through #44166

For PHPStan, see https://phpstan.org/user-guide/discovering-symbols#third-party-code-outside-of-composer-dependencies

parameters:
scanDirectories:

  • var/cache/dev/Symfony/Config

Provided solution does not consider very important detail: var/cache/dev might not exist. For example in ci pipeline where everything works in test env. Setting this to var/cache/test is also not an option since this is also missing on the first run on the dev machine. So stan would fail with
[PHPStan\File\PathNotFoundException]
Path /home/www-data/project/var/cache/dev/Symfony/Config does not exist

I did not find any solution to this at least in PhpStan docs. E.g. to make scanDirectories optional

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

Successfully merging a pull request may close this issue.

8 participants