|
| 1 | +<?php declare(strict_types = 1); |
| 2 | + |
| 3 | +namespace PHPStan\Build; |
| 4 | + |
| 5 | +use PhpParser\Node; |
| 6 | +use PHPStan\Analyser\Scope; |
| 7 | +use PHPStan\Node\InClassNode; |
| 8 | +use PHPStan\Php\PhpVersion; |
| 9 | +use PHPStan\Rules\Rule; |
| 10 | +use PHPStan\Rules\RuleErrorBuilder; |
| 11 | +use PHPStan\Type\DynamicFunctionReturnTypeExtension; |
| 12 | +use PHPStan\Type\DynamicFunctionThrowTypeExtension; |
| 13 | +use PHPStan\Type\DynamicMethodReturnTypeExtension; |
| 14 | +use PHPStan\Type\DynamicMethodThrowTypeExtension; |
| 15 | +use PHPStan\Type\DynamicStaticMethodReturnTypeExtension; |
| 16 | +use PHPStan\Type\DynamicStaticMethodThrowTypeExtension; |
| 17 | +use PHPStan\Type\ExpressionTypeResolverExtension; |
| 18 | +use PHPStan\Type\FunctionParameterClosureThisExtension; |
| 19 | +use PHPStan\Type\FunctionParameterClosureTypeExtension; |
| 20 | +use PHPStan\Type\FunctionParameterOutTypeExtension; |
| 21 | +use PHPStan\Type\FunctionTypeSpecifyingExtension; |
| 22 | +use PHPStan\Type\MethodParameterClosureThisExtension; |
| 23 | +use PHPStan\Type\MethodParameterClosureTypeExtension; |
| 24 | +use PHPStan\Type\MethodParameterOutTypeExtension; |
| 25 | +use PHPStan\Type\MethodTypeSpecifyingExtension; |
| 26 | +use PHPStan\Type\ObjectType; |
| 27 | +use PHPStan\Type\StaticMethodParameterClosureThisExtension; |
| 28 | +use PHPStan\Type\StaticMethodParameterClosureTypeExtension; |
| 29 | +use PHPStan\Type\StaticMethodParameterOutTypeExtension; |
| 30 | +use PHPStan\Type\StaticMethodTypeSpecifyingExtension; |
| 31 | +use PHPStan\Type\TypeCombinator; |
| 32 | +use function sprintf; |
| 33 | + |
| 34 | +/** |
| 35 | + * Extensions that receive a Scope must ask Scope::getPhpVersion() about the analysed |
| 36 | + * PHP version so that PHP_VERSION_ID checks in the analysed code narrow their answers. |
| 37 | + * |
| 38 | + * @implements Rule<InClassNode> |
| 39 | + */ |
| 40 | +final class NoPhpVersionInjectionInScopeAwareExtensionRule implements Rule |
| 41 | +{ |
| 42 | + |
| 43 | + private const SCOPE_AWARE_EXTENSIONS = [ |
| 44 | + DynamicFunctionReturnTypeExtension::class, |
| 45 | + DynamicMethodReturnTypeExtension::class, |
| 46 | + DynamicStaticMethodReturnTypeExtension::class, |
| 47 | + DynamicFunctionThrowTypeExtension::class, |
| 48 | + DynamicMethodThrowTypeExtension::class, |
| 49 | + DynamicStaticMethodThrowTypeExtension::class, |
| 50 | + FunctionTypeSpecifyingExtension::class, |
| 51 | + MethodTypeSpecifyingExtension::class, |
| 52 | + StaticMethodTypeSpecifyingExtension::class, |
| 53 | + FunctionParameterClosureTypeExtension::class, |
| 54 | + MethodParameterClosureTypeExtension::class, |
| 55 | + StaticMethodParameterClosureTypeExtension::class, |
| 56 | + FunctionParameterClosureThisExtension::class, |
| 57 | + MethodParameterClosureThisExtension::class, |
| 58 | + StaticMethodParameterClosureThisExtension::class, |
| 59 | + FunctionParameterOutTypeExtension::class, |
| 60 | + MethodParameterOutTypeExtension::class, |
| 61 | + StaticMethodParameterOutTypeExtension::class, |
| 62 | + ExpressionTypeResolverExtension::class, |
| 63 | + ]; |
| 64 | + |
| 65 | + public function getNodeType(): string |
| 66 | + { |
| 67 | + return InClassNode::class; |
| 68 | + } |
| 69 | + |
| 70 | + public function processNode(Node $node, Scope $scope): array |
| 71 | + { |
| 72 | + $classReflection = $node->getClassReflection(); |
| 73 | + $implementedExtension = null; |
| 74 | + foreach (self::SCOPE_AWARE_EXTENSIONS as $extension) { |
| 75 | + if ($classReflection->implementsInterface($extension)) { |
| 76 | + $implementedExtension = $extension; |
| 77 | + break; |
| 78 | + } |
| 79 | + } |
| 80 | + if ($implementedExtension === null) { |
| 81 | + return []; |
| 82 | + } |
| 83 | + |
| 84 | + if (!$classReflection->hasConstructor()) { |
| 85 | + return []; |
| 86 | + } |
| 87 | + |
| 88 | + $phpVersionType = new ObjectType(PhpVersion::class); |
| 89 | + $errors = []; |
| 90 | + foreach ($classReflection->getConstructor()->getOnlyVariant()->getParameters() as $parameter) { |
| 91 | + if (!$phpVersionType->isSuperTypeOf(TypeCombinator::removeNull($parameter->getType()))->yes()) { |
| 92 | + continue; |
| 93 | + } |
| 94 | + |
| 95 | + $errors[] = RuleErrorBuilder::message(sprintf( |
| 96 | + '%s implements %s and should not inject %s via constructor parameter $%s. Use Scope::getPhpVersion() instead.', |
| 97 | + $classReflection->getDisplayName(), |
| 98 | + $implementedExtension, |
| 99 | + PhpVersion::class, |
| 100 | + $parameter->getName(), |
| 101 | + )) |
| 102 | + ->identifier('phpstan.phpVersionInjection') |
| 103 | + ->build(); |
| 104 | + } |
| 105 | + |
| 106 | + return $errors; |
| 107 | + } |
| 108 | + |
| 109 | +} |
0 commit comments