-
Notifications
You must be signed in to change notification settings - Fork 593
Resolve PHP version checks in remaining scope-aware extensions from Scope::getPhpVersion() and forbid injecting PhpVersion into them
#6563
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
Merged
staabm
merged 9 commits into
phpstan:2.3.x
from
phpstan-bot:create-pull-request/patch-dh7ttzc
Sep 23, 2026
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
3886d99
Resolve PHP version checks in remaining scope-aware extensions from `…
staabm cd07843
Add `PhpVersions::hasCorrectReflectionEnumAdapterReturnTypes()` for t…
phpstan-bot 71a3320
Keep injected `PhpVersion` in `PDOConnectReturnTypeExtension`
phpstan-bot f69c8ba
Check constructor parameters in `NoPhpVersionInjectionInScopeAwareExt…
phpstan-bot 7186c8f
Test nullable `PhpVersion` constructor parameter in `NoPhpVersionInje…
phpstan-bot 30f79b0
Reduce `MbSubstituteCharacterDynamicReturnTypeExtension` and `MbFunct…
phpstan-bot 32c2219
Move the `PDOConnectReturnTypeExtension` `PhpVersion` injection ignor…
phpstan-bot 6c662cb
Regenerate baseline
phpstan-bot ec89b7d
Keep `PASS` and `NONE` encodings when the analysed PHP version range …
phpstan-bot File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
109 changes: 109 additions & 0 deletions
109
build/PHPStan/Build/NoPhpVersionInjectionInScopeAwareExtensionRule.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,109 @@ | ||
| <?php declare(strict_types = 1); | ||
|
|
||
| namespace PHPStan\Build; | ||
|
|
||
| use PhpParser\Node; | ||
| use PHPStan\Analyser\Scope; | ||
| use PHPStan\Node\InClassNode; | ||
| use PHPStan\Php\PhpVersion; | ||
| use PHPStan\Rules\Rule; | ||
| use PHPStan\Rules\RuleErrorBuilder; | ||
| use PHPStan\Type\DynamicFunctionReturnTypeExtension; | ||
| use PHPStan\Type\DynamicFunctionThrowTypeExtension; | ||
| use PHPStan\Type\DynamicMethodReturnTypeExtension; | ||
| use PHPStan\Type\DynamicMethodThrowTypeExtension; | ||
| use PHPStan\Type\DynamicStaticMethodReturnTypeExtension; | ||
| use PHPStan\Type\DynamicStaticMethodThrowTypeExtension; | ||
| use PHPStan\Type\ExpressionTypeResolverExtension; | ||
| use PHPStan\Type\FunctionParameterClosureThisExtension; | ||
| use PHPStan\Type\FunctionParameterClosureTypeExtension; | ||
| use PHPStan\Type\FunctionParameterOutTypeExtension; | ||
| use PHPStan\Type\FunctionTypeSpecifyingExtension; | ||
| use PHPStan\Type\MethodParameterClosureThisExtension; | ||
| use PHPStan\Type\MethodParameterClosureTypeExtension; | ||
| use PHPStan\Type\MethodParameterOutTypeExtension; | ||
| use PHPStan\Type\MethodTypeSpecifyingExtension; | ||
| use PHPStan\Type\ObjectType; | ||
| use PHPStan\Type\StaticMethodParameterClosureThisExtension; | ||
| use PHPStan\Type\StaticMethodParameterClosureTypeExtension; | ||
| use PHPStan\Type\StaticMethodParameterOutTypeExtension; | ||
| use PHPStan\Type\StaticMethodTypeSpecifyingExtension; | ||
| use PHPStan\Type\TypeCombinator; | ||
| use function sprintf; | ||
|
|
||
| /** | ||
| * Extensions that receive a Scope must ask Scope::getPhpVersion() about the analysed | ||
| * PHP version so that PHP_VERSION_ID checks in the analysed code narrow their answers. | ||
| * | ||
| * @implements Rule<InClassNode> | ||
| */ | ||
| final class NoPhpVersionInjectionInScopeAwareExtensionRule implements Rule | ||
| { | ||
|
|
||
| private const SCOPE_AWARE_EXTENSIONS = [ | ||
| DynamicFunctionReturnTypeExtension::class, | ||
| DynamicMethodReturnTypeExtension::class, | ||
| DynamicStaticMethodReturnTypeExtension::class, | ||
| DynamicFunctionThrowTypeExtension::class, | ||
| DynamicMethodThrowTypeExtension::class, | ||
| DynamicStaticMethodThrowTypeExtension::class, | ||
| FunctionTypeSpecifyingExtension::class, | ||
| MethodTypeSpecifyingExtension::class, | ||
| StaticMethodTypeSpecifyingExtension::class, | ||
| FunctionParameterClosureTypeExtension::class, | ||
| MethodParameterClosureTypeExtension::class, | ||
| StaticMethodParameterClosureTypeExtension::class, | ||
| FunctionParameterClosureThisExtension::class, | ||
| MethodParameterClosureThisExtension::class, | ||
| StaticMethodParameterClosureThisExtension::class, | ||
| FunctionParameterOutTypeExtension::class, | ||
| MethodParameterOutTypeExtension::class, | ||
| StaticMethodParameterOutTypeExtension::class, | ||
| ExpressionTypeResolverExtension::class, | ||
| ]; | ||
|
|
||
| public function getNodeType(): string | ||
| { | ||
| return InClassNode::class; | ||
| } | ||
|
|
||
| public function processNode(Node $node, Scope $scope): array | ||
| { | ||
| $classReflection = $node->getClassReflection(); | ||
| $implementedExtension = null; | ||
| foreach (self::SCOPE_AWARE_EXTENSIONS as $extension) { | ||
| if ($classReflection->implementsInterface($extension)) { | ||
| $implementedExtension = $extension; | ||
| break; | ||
| } | ||
| } | ||
| if ($implementedExtension === null) { | ||
| return []; | ||
| } | ||
|
|
||
| if (!$classReflection->hasConstructor()) { | ||
| return []; | ||
| } | ||
|
|
||
| $phpVersionType = new ObjectType(PhpVersion::class); | ||
| $errors = []; | ||
| foreach ($classReflection->getConstructor()->getOnlyVariant()->getParameters() as $parameter) { | ||
| if (!$phpVersionType->isSuperTypeOf(TypeCombinator::removeNull($parameter->getType()))->yes()) { | ||
| continue; | ||
| } | ||
|
|
||
| $errors[] = RuleErrorBuilder::message(sprintf( | ||
| '%s implements %s and should not inject %s via constructor parameter $%s. Use Scope::getPhpVersion() instead.', | ||
| $classReflection->getDisplayName(), | ||
| $implementedExtension, | ||
| PhpVersion::class, | ||
| $parameter->getName(), | ||
| )) | ||
| ->identifier('phpstan.phpVersionInjection') | ||
| ->build(); | ||
| } | ||
|
|
||
| return $errors; | ||
| } | ||
|
|
||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.