Skip to content

Add a dynamic stub loader to prefer upstream types - #481

Merged
VincentLanglet merged 2 commits into
phpstan:2.0.xfrom
stof:stub_loading
Apr 22, 2026
Merged

Add a dynamic stub loader to prefer upstream types#481
VincentLanglet merged 2 commits into
phpstan:2.0.xfrom
stof:stub_loading

Conversation

@stof

@stofstof commented Apr 21, 2026

Copy link
Copy Markdown
Contributor

Once the precise types are added in Symfony, the upstream types should be used to benefit from the maintenance work of the Symfony team.

Closes#431

@stof

stof commented Apr 21, 2026

Copy link
Copy Markdown
ContributorAuthor

The remaining stubs belong to a few categories:

Once the precise types are added in Symfony, the upstream types should
be used to benefit from the maintenance work of the Symfony team.
private function isInstalledVersionBelow(string $package, string $maxVersion): bool
{
if (!class_exists(InstalledVersions::class)) {
return false;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

In which case this will occur ?

Should we return true, just to avoid a BC break ?

@stofstofApr 21, 2026

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

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

I think this could happen when using a phpstan phar without loading the project composer autoloader (which likely causes issues). Otherwise, it should not happen when using composer 2+.

I copied this condition from the stub loader of phpstan-doctrine.

if ($this->isInstalledVersionBelow('symfony/http-foundation', '7.4.0.0')) {
$files[] = $stubsDir . '/Symfony/Component/HttpFoundation/ParameterBag.stub';

try {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Can't we check that the version is >= some version ?

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

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

I could. I copied the way it was done in the existing code that I moved here (to avoid having to handle coordinated conditions between 2 loaders).
Tell me whether you want me to do the change).

For reference, InputBag shipped in version 5.1.0 of the component.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Didn't see it was coming from another file

@stof

stof commented Apr 21, 2026

Copy link
Copy Markdown
ContributorAuthor

Most remaining stub types (i.e. all except form types and the Options class) have been submitted upstream in symfony/symfony#64009.
I'll prepare a follow-up PR to load those types conditionally, to be merged once the Symfony PR gets accepted.

Comment threadextension.neon
- stubs/Psr/Cache/CacheException.stub
- stubs/Psr/Cache/CacheItemInterface.stub
- stubs/Psr/Cache/InvalidArgumentException.stub
- stubs/Symfony/Bundle/FrameworkBundle/Controller/AbstractController.stub

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

what is the case for files which still are referenced in extension.neon?

are these required for every symfony version?

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

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

See #481 (comment) where I explain that

@stof

stof commented Apr 22, 2026

Copy link
Copy Markdown
ContributorAuthor

@VincentLanglet As the Symfony PR is merged, do you prefer that I update this PR with the conditional loading for Symfony 8.1.0 or keeping it as a follow-up PR (as this PR is already approved) ?

@VincentLanglet

Copy link
Copy Markdown
Contributor

@VincentLanglet As the Symfony PR is merged, do you prefer that I update this PR with the conditional loading for Symfony 8.1.0 or keeping it as a follow-up PR (as this PR is already approved) ?

I'm fine with both. I wait for the approval from staabm anyway

@stof

stof commented Apr 22, 2026

Copy link
Copy Markdown
ContributorAuthor

@VincentLanglet@staabm I pushed my commit handling the stubs covered in Symfony 8.1 then (I already had it ready locally).

@stof
stofforce-pushed the stub_loading branch 2 times, most recently from 31f4fba to 02a7a81CompareApril 22, 2026 14:25
@VincentLanglet
VincentLanglet merged commit 422f25e into phpstan:2.0.xApr 22, 2026
43 checks passed
@stof
stof deleted the stub_loading branch April 22, 2026 15:05
Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Stubs should not be loaded when using versions of Symfony with upstream generic types

3 participants

@stof@VincentLanglet@staabm