|
| 1 | +<?php declare(strict_types = 1); |
| 2 | + |
| 3 | +namespace PHPStan\Rules\Variables; |
| 4 | + |
| 5 | +use PhpParser\Node\Expr; |
| 6 | +use PHPStan\Analyser\Scope; |
| 7 | +use PHPStan\DependencyInjection\AutowiredService; |
| 8 | +use PHPStan\Reflection\ExtendedMethodReflection; |
| 9 | +use PHPStan\Reflection\FunctionReflection; |
| 10 | +use PHPStan\Reflection\ParameterReflection; |
| 11 | +use PHPStan\Rules\IdentifierRuleError; |
| 12 | +use PHPStan\Rules\RuleErrorBuilder; |
| 13 | +use PHPStan\Rules\RuleLevelHelper; |
| 14 | +use PHPStan\Type\ErrorType; |
| 15 | +use PHPStan\Type\Type; |
| 16 | +use PHPStan\Type\VerbosityLevel; |
| 17 | +use function sprintf; |
| 18 | + |
| 19 | +/** |
| 20 | + * Compares what a by-ref parameter is left holding against the type its callers are promised. |
| 21 | + * |
| 22 | + * The promise is either an explicit `@param-out` or, in its absence, the parameter's own type. |
| 23 | + * Which one it is only shows in the error message, so callers report it via $isParamOutType. |
| 24 | + * |
| 25 | + * @internal |
| 26 | + */ |
| 27 | +#[AutowiredService] |
| 28 | +final class ParameterOutTypeCheck |
| 29 | +{ |
| 30 | + |
| 31 | + public function __construct( |
| 32 | + private RuleLevelHelper $ruleLevelHelper, |
| 33 | + ) |
| 34 | + { |
| 35 | + } |
| 36 | + |
| 37 | + /** |
| 38 | + * @param Type $outType Already passed through TypeUtils::resolveLateResolvableTypes() |
| 39 | + * @return list<IdentifierRuleError> |
| 40 | + */ |
| 41 | + public function check( |
| 42 | + Scope $scope, |
| 43 | + FunctionReflection|ExtendedMethodReflection $inFunction, |
| 44 | + ParameterReflection $parameter, |
| 45 | + Expr $checkedExpr, |
| 46 | + Type $outType, |
| 47 | + bool $isParamOutType, |
| 48 | + ): array |
| 49 | + { |
| 50 | + $typeResult = $this->ruleLevelHelper->findTypeToCheck( |
| 51 | + $scope, |
| 52 | + $checkedExpr, |
| 53 | + '', |
| 54 | + static fn (Type $type): bool => $outType->isSuperTypeOf($type)->yes(), |
| 55 | + ); |
| 56 | + if ($typeResult->getType() instanceof ErrorType) { |
| 57 | + return $typeResult->getUnknownClassErrors(); |
| 58 | + } |
| 59 | + |
| 60 | + $assignedExprType = $scope->getType($checkedExpr); |
| 61 | + if ($outType->isSuperTypeOf($assignedExprType)->yes()) { |
| 62 | + return []; |
| 63 | + } |
| 64 | + |
| 65 | + if ($inFunction instanceof ExtendedMethodReflection) { |
| 66 | + $functionDescription = sprintf('method %s::%s()', $inFunction->getDeclaringClass()->getDisplayName(), $inFunction->getName()); |
| 67 | + } else { |
| 68 | + $functionDescription = sprintf('function %s()', $inFunction->getName()); |
| 69 | + } |
| 70 | + |
| 71 | + $verbosityLevel = VerbosityLevel::getRecommendedLevelByType($outType, $assignedExprType); |
| 72 | + $errorBuilder = RuleErrorBuilder::message(sprintf( |
| 73 | + 'Parameter &$%s %s of %s expects %s, %s given.', |
| 74 | + $parameter->getName(), |
| 75 | + $isParamOutType ? '@param-out type' : 'by-ref type', |
| 76 | + $functionDescription, |
| 77 | + $outType->describe($verbosityLevel), |
| 78 | + $assignedExprType->describe($verbosityLevel), |
| 79 | + ))->identifier(sprintf('%s.type', $isParamOutType ? 'paramOut' : 'parameterByRef')); |
| 80 | + |
| 81 | + if (!$isParamOutType) { |
| 82 | + $errorBuilder->tip('You can change the parameter out type with @param-out PHPDoc tag.'); |
| 83 | + } |
| 84 | + |
| 85 | + return [ |
| 86 | + $errorBuilder->build(), |
| 87 | + ]; |
| 88 | + } |
| 89 | + |
| 90 | +} |
0 commit comments