|
| 1 | +<?php declare(strict_types = 1); |
| 2 | + |
| 3 | +namespace PHPStan\Rules\Api; |
| 4 | + |
| 5 | +use PhpParser\Node; |
| 6 | +use PHPStan\Analyser\Scope; |
| 7 | +use PHPStan\Reflection\ReflectionProvider; |
| 8 | +use PHPStan\Rules\Rule; |
| 9 | +use PHPStan\Rules\RuleErrorBuilder; |
| 10 | +use ReflectionClass; |
| 11 | +use ReflectionClassConstant; |
| 12 | +use ReflectionEnum; |
| 13 | +use ReflectionEnumBackedCase; |
| 14 | +use ReflectionExtension; |
| 15 | +use ReflectionFiber; |
| 16 | +use ReflectionFunction; |
| 17 | +use ReflectionGenerator; |
| 18 | +use ReflectionMethod; |
| 19 | +use ReflectionObject; |
| 20 | +use ReflectionParameter; |
| 21 | +use ReflectionProperty; |
| 22 | +use ReflectionZendExtension; |
| 23 | +use function array_keys; |
| 24 | +use function in_array; |
| 25 | +use function sprintf; |
| 26 | +use function strpos; |
| 27 | + |
| 28 | +/** |
| 29 | + * @implements Rule<Node\Expr\New_> |
| 30 | + */ |
| 31 | +class RuntimeReflectionInstantiationRule implements Rule |
| 32 | +{ |
| 33 | + |
| 34 | + public function __construct(private ReflectionProvider $reflectionProvider) |
| 35 | + { |
| 36 | + } |
| 37 | + |
| 38 | + public function getNodeType(): string |
| 39 | + { |
| 40 | + return Node\Expr\New_::class; |
| 41 | + } |
| 42 | + |
| 43 | + public function processNode(Node $node, Scope $scope): array |
| 44 | + { |
| 45 | + if (!$node->class instanceof Node\Name) { |
| 46 | + return []; |
| 47 | + } |
| 48 | + |
| 49 | + $className = $scope->resolveName($node->class); |
| 50 | + if (!$this->reflectionProvider->hasClass($className)) { |
| 51 | + return []; |
| 52 | + } |
| 53 | + |
| 54 | + $classReflection = $this->reflectionProvider->getClass($className); |
| 55 | + if (!in_array($classReflection->getName(), [ |
| 56 | + ReflectionMethod::class, |
| 57 | + ReflectionClass::class, |
| 58 | + ReflectionClassConstant::class, |
| 59 | + ReflectionEnum::class, |
| 60 | + ReflectionEnumBackedCase::class, |
| 61 | + ReflectionZendExtension::class, |
| 62 | + ReflectionExtension::class, |
| 63 | + ReflectionFunction::class, |
| 64 | + ReflectionObject::class, |
| 65 | + ReflectionParameter::class, |
| 66 | + ReflectionProperty::class, |
| 67 | + ReflectionGenerator::class, |
| 68 | + ReflectionFiber::class, |
| 69 | + ], true)) { |
| 70 | + return []; |
| 71 | + } |
| 72 | + |
| 73 | + if (!$scope->isInClass()) { |
| 74 | + return []; |
| 75 | + } |
| 76 | + |
| 77 | + $scopeClassReflection = $scope->getClassReflection(); |
| 78 | + $hasPhpStanInterface = false; |
| 79 | + foreach (array_keys($scopeClassReflection->getInterfaces()) as $interfaceName) { |
| 80 | + if (strpos($interfaceName, 'PHPStan\\') !== 0) { |
| 81 | + continue; |
| 82 | + } |
| 83 | + |
| 84 | + $hasPhpStanInterface = true; |
| 85 | + } |
| 86 | + |
| 87 | + if (!$hasPhpStanInterface) { |
| 88 | + return []; |
| 89 | + } |
| 90 | + |
| 91 | + return [ |
| 92 | + RuleErrorBuilder::message( |
| 93 | + sprintf('Creating new %s is a runtime reflection concept that might not work in PHPStan because it uses fully static reflection engine. Use objects retrieved from ReflectionProvider instead.', $classReflection->getName()), |
| 94 | + )->build(), |
| 95 | + ]; |
| 96 | + } |
| 97 | + |
| 98 | +} |
0 commit comments