|
| 1 | +<?php |
| 2 | +declare(strict_types = 1); |
| 3 | + |
| 4 | +namespace Spaze\PHPStan\Rules\Disallowed\RuleErrors; |
| 5 | + |
| 6 | +use PhpParser\Node\Expr; |
| 7 | +use PhpParser\Node\Expr\CallLike; |
| 8 | +use PhpParser\Node\Expr\FuncCall; |
| 9 | +use PhpParser\Node\Expr\MethodCall; |
| 10 | +use PhpParser\Node\Expr\StaticCall; |
| 11 | +use PhpParser\Node\Name; |
| 12 | +use PHPStan\Analyser\ArgumentsNormalizer; |
| 13 | +use PHPStan\Analyser\Scope; |
| 14 | +use PHPStan\Reflection\ExtendedMethodReflection; |
| 15 | +use PHPStan\Reflection\FunctionReflection; |
| 16 | +use PHPStan\Reflection\ParametersAcceptorSelector; |
| 17 | +use PHPStan\Reflection\ReflectionProvider; |
| 18 | +use PHPStan\Rules\IdentifierRuleError; |
| 19 | +use PHPStan\ShouldNotHappenException; |
| 20 | +use PHPStan\Type\TypeCombinator; |
| 21 | +use Spaze\PHPStan\Rules\Disallowed\DisallowedCall; |
| 22 | +use Spaze\PHPStan\Rules\Disallowed\DisallowedCallFactory; |
| 23 | +use Spaze\PHPStan\Rules\Disallowed\PHPStan1Compatibility; |
| 24 | +use Spaze\PHPStan\Rules\Disallowed\Type\TypeResolver; |
| 25 | + |
| 26 | +class DisallowedCallableParameterRuleErrors |
| 27 | +{ |
| 28 | + |
| 29 | + private TypeResolver $typeResolver; |
| 30 | + |
| 31 | + private DisallowedFunctionRuleErrors $disallowedFunctionRuleErrors; |
| 32 | + |
| 33 | + private DisallowedMethodRuleErrors $disallowedMethodRuleErrors; |
| 34 | + |
| 35 | + /** @var list<DisallowedCall> */ |
| 36 | + private array $disallowedFunctionCalls; |
| 37 | + |
| 38 | + /** @var list<DisallowedCall> */ |
| 39 | + private array $disallowedCalls; |
| 40 | + |
| 41 | + private ReflectionProvider $reflectionProvider; |
| 42 | + |
| 43 | + |
| 44 | + /** |
| 45 | + * @param TypeResolver $typeResolver |
| 46 | + * @param DisallowedFunctionRuleErrors $disallowedFunctionRuleErrors |
| 47 | + * @param DisallowedMethodRuleErrors $disallowedMethodRuleErrors |
| 48 | + * @param DisallowedCallFactory $disallowedCallFactory |
| 49 | + * @param ReflectionProvider $reflectionProvider |
| 50 | + * @param array $forbiddenFunctionCalls |
| 51 | + * @phpstan-param ForbiddenCallsConfig $forbiddenFunctionCalls |
| 52 | + * @param array $forbiddenMethodCalls |
| 53 | + * @phpstan-param ForbiddenCallsConfig $forbiddenMethodCalls |
| 54 | + * @param array $forbiddenStaticCalls |
| 55 | + * @phpstan-param ForbiddenCallsConfig $forbiddenStaticCalls |
| 56 | + * @noinspection PhpUndefinedClassInspection ForbiddenCallsConfig is a type alias defined in PHPStan config |
| 57 | + * @throws ShouldNotHappenException |
| 58 | + */ |
| 59 | + public function __construct( |
| 60 | + TypeResolver $typeResolver, |
| 61 | + DisallowedFunctionRuleErrors $disallowedFunctionRuleErrors, |
| 62 | + DisallowedMethodRuleErrors $disallowedMethodRuleErrors, |
| 63 | + DisallowedCallFactory $disallowedCallFactory, |
| 64 | + ReflectionProvider $reflectionProvider, |
| 65 | + array $forbiddenFunctionCalls, |
| 66 | + array $forbiddenMethodCalls, |
| 67 | + array $forbiddenStaticCalls |
| 68 | + ) { |
| 69 | + $this->typeResolver = $typeResolver; |
| 70 | + $this->disallowedFunctionRuleErrors = $disallowedFunctionRuleErrors; |
| 71 | + $this->disallowedMethodRuleErrors = $disallowedMethodRuleErrors; |
| 72 | + $this->disallowedFunctionCalls = $disallowedCallFactory->createFromConfig($forbiddenFunctionCalls); |
| 73 | + $this->disallowedCalls = $disallowedCallFactory->createFromConfig(array_merge($forbiddenMethodCalls, $forbiddenStaticCalls)); |
| 74 | + $this->reflectionProvider = $reflectionProvider; |
| 75 | + } |
| 76 | + |
| 77 | + |
| 78 | + /** |
| 79 | + * @param FuncCall $node |
| 80 | + * @param Scope $scope |
| 81 | + * @return list<IdentifierRuleError> |
| 82 | + * @throws ShouldNotHappenException |
| 83 | + */ |
| 84 | + public function getForFunction(FuncCall $node, Scope $scope): array |
| 85 | + { |
| 86 | + $ruleErrors = []; |
| 87 | + foreach ($this->typeResolver->getNamesFromCall($node, $scope) as $name) { |
| 88 | + if (!$this->reflectionProvider->hasFunction($name, $scope)) { |
| 89 | + continue; |
| 90 | + } |
| 91 | + $reflection = $this->reflectionProvider->getFunction($name, $scope); |
| 92 | + $errors = $this->getErrors($node, $scope, $reflection); |
| 93 | + if ($errors) { |
| 94 | + $ruleErrors = array_merge($ruleErrors, $errors); |
| 95 | + } |
| 96 | + } |
| 97 | + return $ruleErrors; |
| 98 | + } |
| 99 | + |
| 100 | + |
| 101 | + /** |
| 102 | + * @param Name|Expr $class |
| 103 | + * @param MethodCall|StaticCall $node |
| 104 | + * @param Scope $scope |
| 105 | + * @return list<IdentifierRuleError> |
| 106 | + * @throws ShouldNotHappenException |
| 107 | + */ |
| 108 | + public function getForMethod($class, CallLike $node, Scope $scope): array |
| 109 | + { |
| 110 | + $ruleErrors = []; |
| 111 | + $classType = $this->typeResolver->getType($class, $scope); |
| 112 | + if (PHPStan1Compatibility::isClassString($classType)->yes()) { |
| 113 | + $classType = $classType->getClassStringObjectType(); |
| 114 | + } |
| 115 | + foreach ($classType->getObjectTypeOrClassStringObjectType()->getObjectClassNames() as $className) { |
| 116 | + if (!$this->reflectionProvider->hasClass($className)) { |
| 117 | + continue; |
| 118 | + } |
| 119 | + $classReflection = $this->reflectionProvider->getClass($className); |
| 120 | + foreach ($this->typeResolver->getNamesFromCall($node, $scope) as $name) { |
| 121 | + if (!$classReflection->hasMethod($name->toString())) { |
| 122 | + continue; |
| 123 | + } |
| 124 | + $reflection = $classReflection->getMethod($name->toString(), $scope); |
| 125 | + $errors = $this->getErrors($node, $scope, $reflection); |
| 126 | + if ($errors) { |
| 127 | + $ruleErrors = array_merge($ruleErrors, $errors); |
| 128 | + } |
| 129 | + } |
| 130 | + } |
| 131 | + return $ruleErrors; |
| 132 | + } |
| 133 | + |
| 134 | + |
| 135 | + /** |
| 136 | + * @param Scope $scope |
| 137 | + * @param FuncCall|MethodCall|StaticCall $node |
| 138 | + * @param ExtendedMethodReflection|FunctionReflection $reflection |
| 139 | + * @return list<IdentifierRuleError> |
| 140 | + * @throws ShouldNotHappenException |
| 141 | + */ |
| 142 | + private function getErrors(CallLike $node, Scope $scope, $reflection): array |
| 143 | + { |
| 144 | + $ruleErrors = []; |
| 145 | + $parametersAcceptor = ParametersAcceptorSelector::selectFromArgs($scope, $node->getArgs(), $reflection->getVariants()); |
| 146 | + $reorderedArgs = ArgumentsNormalizer::reorderArgs($parametersAcceptor, $node->getArgs()) ?? $node->getArgs(); |
| 147 | + foreach ($parametersAcceptor->getParameters() as $key => $parameter) { |
| 148 | + if (!TypeCombinator::removeNull($parameter->getType())->isCallable()->yes() || !isset($reorderedArgs[$key])) { |
| 149 | + continue; |
| 150 | + } |
| 151 | + $callableType = $scope->getType($reorderedArgs[$key]->value); |
| 152 | + foreach ($callableType->getConstantStrings() as $constantString) { |
| 153 | + $errors = $this->disallowedFunctionRuleErrors->getByString($constantString->getValue(), $scope, $this->disallowedFunctionCalls); |
| 154 | + if ($errors) { |
| 155 | + $ruleErrors = array_merge($ruleErrors, $errors); |
| 156 | + } |
| 157 | + } |
| 158 | + foreach ($callableType->getConstantArrays() as $constantArray) { |
| 159 | + foreach ($constantArray->findTypeAndMethodNames() as $typeAndMethodName) { |
| 160 | + if ($typeAndMethodName->isUnknown()) { |
| 161 | + continue; |
| 162 | + } |
| 163 | + $method = $typeAndMethodName->getMethod(); |
| 164 | + foreach ($typeAndMethodName->getType()->getObjectClassNames() as $class) { |
| 165 | + $errors = $this->disallowedMethodRuleErrors->getByString($class, $method, $scope, $this->disallowedCalls); |
| 166 | + if ($errors) { |
| 167 | + $ruleErrors = array_merge($ruleErrors, $errors); |
| 168 | + } |
| 169 | + } |
| 170 | + } |
| 171 | + } |
| 172 | + } |
| 173 | + return $ruleErrors; |
| 174 | + } |
| 175 | + |
| 176 | +} |
0 commit comments