|
| 1 | +<?php declare(strict_types = 1); |
| 2 | + |
| 3 | +namespace PHPStan\Rules\Functions; |
| 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 PHPStan\Rules\RuleLevelHelper; |
| 11 | +use PHPStan\Type\BooleanType; |
| 12 | +use PHPStan\Type\ErrorType; |
| 13 | +use PHPStan\Type\FloatType; |
| 14 | +use PHPStan\Type\IntegerType; |
| 15 | +use PHPStan\Type\NullType; |
| 16 | +use PHPStan\Type\StringAlwaysAcceptingObjectWithToStringType; |
| 17 | +use PHPStan\Type\Type; |
| 18 | +use PHPStan\Type\TypeCombinator; |
| 19 | +use PHPStan\Type\VerbosityLevel; |
| 20 | +use function array_key_exists; |
| 21 | +use function count; |
| 22 | +use function sprintf; |
| 23 | + |
| 24 | +/** |
| 25 | + * @implements Rule<Node\Expr\FuncCall> |
| 26 | + */ |
| 27 | +final class PrintfParameterTypeRule implements Rule |
| 28 | +{ |
| 29 | + |
| 30 | + private const FORMAT_ARGUMENT_POSITIONS = [ |
| 31 | + 'printf' => 0, |
| 32 | + 'sprintf' => 0, |
| 33 | + 'fprintf' => 1, |
| 34 | + ]; |
| 35 | + private const MINIMUM_NUMBER_OF_ARGUMENTS = [ |
| 36 | + 'printf' => 1, |
| 37 | + 'sprintf' => 1, |
| 38 | + 'fprintf' => 2, |
| 39 | + ]; |
| 40 | + |
| 41 | + public function __construct( |
| 42 | + private PrintfHelper $printfHelper, |
| 43 | + private ReflectionProvider $reflectionProvider, |
| 44 | + private RuleLevelHelper $ruleLevelHelper, |
| 45 | + ) |
| 46 | + { |
| 47 | + } |
| 48 | + |
| 49 | + public function getNodeType(): string |
| 50 | + { |
| 51 | + return Node\Expr\FuncCall::class; |
| 52 | + } |
| 53 | + |
| 54 | + public function processNode(Node $node, Scope $scope): array |
| 55 | + { |
| 56 | + if (!($node->name instanceof Node\Name)) { |
| 57 | + return []; |
| 58 | + } |
| 59 | + |
| 60 | + if (!$this->reflectionProvider->hasFunction($node->name, $scope)) { |
| 61 | + return []; |
| 62 | + } |
| 63 | + |
| 64 | + $functionReflection = $this->reflectionProvider->getFunction($node->name, $scope); |
| 65 | + $name = $functionReflection->getName(); |
| 66 | + if (!array_key_exists($name, self::FORMAT_ARGUMENT_POSITIONS)) { |
| 67 | + return []; |
| 68 | + } |
| 69 | + |
| 70 | + $formatArgumentPosition = self::FORMAT_ARGUMENT_POSITIONS[$name]; |
| 71 | + |
| 72 | + $args = $node->getArgs(); |
| 73 | + foreach ($args as $arg) { |
| 74 | + if ($arg->unpack) { |
| 75 | + return []; |
| 76 | + } |
| 77 | + } |
| 78 | + $argsCount = count($args); |
| 79 | + if ($argsCount < self::MINIMUM_NUMBER_OF_ARGUMENTS[$name]) { |
| 80 | + return []; // caught by CallToFunctionParametersRule |
| 81 | + } |
| 82 | + |
| 83 | + $formatArgType = $scope->getType($args[$formatArgumentPosition]->value); |
| 84 | + $formatArgTypeStrings = $formatArgType->getConstantStrings(); |
| 85 | + |
| 86 | + // Let's start simple for now. |
| 87 | + if (count($formatArgTypeStrings) !== 1) { |
| 88 | + return []; |
| 89 | + } |
| 90 | + |
| 91 | + $formatString = $formatArgTypeStrings[0]; |
| 92 | + $format = $formatString->getValue(); |
| 93 | + $placeholderMap = $this->printfHelper->getPrintfPlaceholders($format); |
| 94 | + $errors = []; |
| 95 | + $typeAllowedByCallToFunctionParametersRule = TypeCombinator::union( |
| 96 | + new StringAlwaysAcceptingObjectWithToStringType(), |
| 97 | + new IntegerType(), |
| 98 | + new FloatType(), |
| 99 | + new BooleanType(), |
| 100 | + new NullType(), |
| 101 | + ); |
| 102 | + // Type on the left can go to the type on the right, but not vice versa. |
| 103 | + $allowedTypeNameMap = [ |
| 104 | + 'strict-int' => 'int', |
| 105 | + 'int' => 'castable to int', |
| 106 | + 'float' => 'castable to float', |
| 107 | + // These are here just for completeness. They won't be used because, these types are already enforced by |
| 108 | + // CallToFunctionParametersRule. |
| 109 | + 'string' => 'castable to string', |
| 110 | + 'mixed' => 'castable to string', |
| 111 | + ]; |
| 112 | + |
| 113 | + for ($i = $formatArgumentPosition + 1, $j = 0; $i < $argsCount; $i++, $j++) { |
| 114 | + // Some arguments may be skipped entirely. |
| 115 | + foreach ($placeholderMap[$j] ?? [] as $placeholder) { |
| 116 | + $argType = $this->ruleLevelHelper->findTypeToCheck( |
| 117 | + $scope, |
| 118 | + $args[$i]->value, |
| 119 | + '', |
| 120 | + static fn (Type $t) => $placeholder->doesArgumentTypeMatchPlaceholder($t), |
| 121 | + )->getType(); |
| 122 | + |
| 123 | + if ($argType instanceof ErrorType || $placeholder->doesArgumentTypeMatchPlaceholder($argType)) { |
| 124 | + continue; |
| 125 | + } |
| 126 | + |
| 127 | + // This is already reported by CallToFunctionParametersRule |
| 128 | + if ( |
| 129 | + !$this->ruleLevelHelper->accepts( |
| 130 | + $typeAllowedByCallToFunctionParametersRule, |
| 131 | + $argType, |
| 132 | + $scope->isDeclareStrictTypes(), |
| 133 | + )->result |
| 134 | + ) { |
| 135 | + continue; |
| 136 | + } |
| 137 | + |
| 138 | + $errors[] = RuleErrorBuilder::message( |
| 139 | + sprintf( |
| 140 | + 'Parameter #%d of function %s is expected to be %s by placeholder #%d (%s), %s given.', |
| 141 | + $i + 1, |
| 142 | + $name, |
| 143 | + $allowedTypeNameMap[$placeholder->acceptingType], |
| 144 | + $placeholder->placeholderNumber, |
| 145 | + $placeholder->label, |
| 146 | + $argType->describe(VerbosityLevel::typeOnly()), |
| 147 | + ), |
| 148 | + )->identifier('argument.type')->build(); |
| 149 | + } |
| 150 | + } |
| 151 | + |
| 152 | + return $errors; |
| 153 | + } |
| 154 | + |
| 155 | +} |
0 commit comments