Skip to content

feat: add support for phpstan>=2.1.12 #47

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

Open
wants to merge 8 commits into
base: 3.x
Choose a base branch
from

Conversation

maxwellmezadre
Copy link

Since PHPStan 2.1.12 introduces an additional constructor parameter in NodeScopeResolver, this PR adjusts the constructor invocation for PHPStan ≥ 2.1.12.

CleanShot 2025-04-16 at 17 14 55

@nimah79
Copy link
Contributor

nimah79 commented Apr 17, 2025

But it needs another argument after the other arguments (narrowMethodScopeFromConstructor).

I think it'll be much better if the NodeScopeResolver is instantiated using named arguments. No need to check PHPStan version:

$nodeScopeResolver = new NodeScopeResolver( // @phpstan-ignore-line
    reflectionProvider: $reflectionProvider,
    initializerExprTypeResolver: $container->getByType(InitializerExprTypeResolver::class),
    reflector: $container->getService('betterReflectionReflector'), // @phpstan-ignore-line
    classReflectionExtensionRegistryProvider: $container->getByType(ClassReflectionExtensionRegistryProvider::class),
    parameterOutTypeExtensionProvider: $container->getByType(ParameterOutTypeExtensionProvider::class),
    parser: $container->getService('defaultAnalysisParser'), // @phpstan-ignore-line
    fileTypeMapper: $container->getByType(FileTypeMapper::class),
    stubPhpDocProvider: $container->getByType(StubPhpDocProvider::class),
    phpVersion: $container->getByType(PhpVersion::class),
    signatureMapProvider: $container->getByType(SignatureMapProvider::class),
    deprecationProvider: $container->getByType(DeprecationProvider::class),
    attributeReflectionFactory: $container->getByType(AttributeReflectionFactory::class),
    phpDocInheritanceResolver: $container->getByType(PhpDocInheritanceResolver::class),
    fileHelper: $container->getByType(FileHelper::class),
    typeSpecifier: $typeSpecifier, // @phpstan-ignore-line
    dynamicThrowTypeExtensionProvider: $container->getByType(DynamicThrowTypeExtensionProvider::class),
    readWritePropertiesExtensionProvider: $container->getByType(ReadWritePropertiesExtensionProvider::class),
    parameterClosureTypeExtensionProvider: $container->getByType(ParameterClosureTypeExtensionProvider::class),
    scopeFactory: $scopeFactory,
    polluteScopeWithLoopInitialAssignments: false,
    polluteScopeWithAlwaysIterableForeach: true,
    polluteScopeWithBlock: true, // @phpstan-ignore-line
    earlyTerminatingMethodCalls: [],
    earlyTerminatingFunctionCalls: [],
    universalObjectCratesClasses: [], // @phpstan-ignore-line
    implicitThrows: true,
    treatPhpDocTypesAsCertain: true,
    narrowMethodScopeFromConstructor: true,
);

@maxwellmezadre

@maxwellmezadre
Copy link
Author

I’ve switched the NodeScopeResolver instantiation to named arguments and added the extra narrowMethodScopeFromConstructor parameter as requested.
However, the PHPStan version check has to stay—other contributors and CI jobs may still be running different PHPStan releases, so removing the guard would break their builds. Keeping the check lets the code work reliably across the full range of supported versions.

@nimah79

@ondrejmirtes
Copy link

NodeScopeResolver constructor isn't covered by BC promise (https://phpstan.org/developing-extensions/backward-compatibility-promise) because it often changes.

The proper solution is to fetch NodeScopeResolver via Container::getByType().

@pelmered
Copy link

NodeScopeResolver constructor isn't covered by BC promise (https://phpstan.org/developing-extensions/backward-compatibility-promise) because it often changes.

The proper solution is to fetch NodeScopeResolver via Container::getByType().

Do you mean that all this code including all the version checks could be replaced with:
$nodeScopeResolver = Container::getByType(NodeScopeResolver::class)

@maxwellmezadre
Copy link
Author

maxwellmezadre commented Apr 19, 2025

I’ve applied the improvements focusing on readability and maintainability.

Here’s how the code looks now — I’m retrieving the required services (NodeScopeResolver and FileAnalyser) using $container->getByType(), as suggested. I also moved the rule and collector registry creation into clearly named variables for better clarity.

Thanks a lot for the great tips — they helped clean this up nicely!

@ondrejmirtes
@owenvoke

@pelmered
Copy link

Nice cleanup!

Sign up for free to 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.

6 participants