|
| 1 | +<?php declare(strict_types = 1); |
| 2 | + |
| 3 | +namespace PHPStan\Rules\Functions; |
| 4 | + |
| 5 | +use PhpParser\Node; |
| 6 | +use PhpParser\Node\Expr\FuncCall; |
| 7 | +use PHPStan\Analyser\ArgumentsNormalizer; |
| 8 | +use PHPStan\Analyser\Scope; |
| 9 | +use PHPStan\Reflection\ParametersAcceptorSelector; |
| 10 | +use PHPStan\Reflection\ReflectionProvider; |
| 11 | +use PHPStan\Rules\Rule; |
| 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 array_key_exists; |
| 18 | +use function count; |
| 19 | +use function in_array; |
| 20 | +use function sprintf; |
| 21 | + |
| 22 | +/** |
| 23 | + * @implements Rule<Node\Expr\FuncCall> |
| 24 | + */ |
| 25 | +class ParameterCastableToStringFunctionRule implements Rule |
| 26 | +{ |
| 27 | + |
| 28 | + public function __construct( |
| 29 | + private ReflectionProvider $reflectionProvider, |
| 30 | + private RuleLevelHelper $ruleLevelHelper, |
| 31 | + ) |
| 32 | + { |
| 33 | + } |
| 34 | + |
| 35 | + public function getNodeType(): string |
| 36 | + { |
| 37 | + return FuncCall::class; |
| 38 | + } |
| 39 | + |
| 40 | + public function processNode(Node $node, Scope $scope): array |
| 41 | + { |
| 42 | + if (!($node->name instanceof Node\Name)) { |
| 43 | + return []; |
| 44 | + } |
| 45 | + |
| 46 | + if (!$this->reflectionProvider->hasFunction($node->name, $scope)) { |
| 47 | + return []; |
| 48 | + } |
| 49 | + |
| 50 | + $functionReflection = $this->reflectionProvider->getFunction($node->name, $scope); |
| 51 | + $functionName = $functionReflection->getName(); |
| 52 | + $implodeFunctions = ['implode', 'join']; |
| 53 | + $checkAllArgsFunctions = ['array_intersect', 'array_intersect_assoc', 'array_diff', 'array_diff_assoc']; |
| 54 | + $checkFirstArgFunctions = [ |
| 55 | + 'array_unique', |
| 56 | + 'array_combine', |
| 57 | + 'sort', |
| 58 | + 'rsort', |
| 59 | + 'asort', |
| 60 | + 'arsort', |
| 61 | + 'natcasesort', |
| 62 | + 'natsort', |
| 63 | + 'array_count_values', |
| 64 | + 'array_fill_keys', |
| 65 | + ]; |
| 66 | + |
| 67 | + if ( |
| 68 | + !in_array($functionName, $checkAllArgsFunctions, true) |
| 69 | + && !in_array($functionName, $checkFirstArgFunctions, true) |
| 70 | + && !in_array($functionName, $implodeFunctions, true) |
| 71 | + ) { |
| 72 | + return []; |
| 73 | + } |
| 74 | + |
| 75 | + $origArgs = $node->getArgs(); |
| 76 | + $parametersAcceptor = ParametersAcceptorSelector::selectFromArgs( |
| 77 | + $scope, |
| 78 | + $origArgs, |
| 79 | + $functionReflection->getVariants(), |
| 80 | + $functionReflection->getNamedArgumentsVariants(), |
| 81 | + ); |
| 82 | + |
| 83 | + $errorMessage = 'Parameter %s of function %s expects an array of values castable to string, %s given.'; |
| 84 | + $getNormalizedArgs = static function () use ($parametersAcceptor, $node): array { |
| 85 | + $normalizedFuncCall = ArgumentsNormalizer::reorderFuncArguments($parametersAcceptor, $node); |
| 86 | + |
| 87 | + if ($normalizedFuncCall === null) { |
| 88 | + return []; |
| 89 | + } |
| 90 | + |
| 91 | + return $normalizedFuncCall->getArgs(); |
| 92 | + }; |
| 93 | + if (in_array($functionName, $implodeFunctions, true)) { |
| 94 | + $normalizedArgs = $getNormalizedArgs(); |
| 95 | + $errorMessage = 'Parameter %s of function %s expects array<string>, %s given.'; |
| 96 | + if (count($normalizedArgs) === 1) { |
| 97 | + $argsToCheck = [0 => $normalizedArgs[0]]; |
| 98 | + } elseif (count($normalizedArgs) === 2) { |
| 99 | + $argsToCheck = [1 => $normalizedArgs[1]]; |
| 100 | + } else { |
| 101 | + return []; |
| 102 | + } |
| 103 | + } elseif (in_array($functionName, $checkAllArgsFunctions, true)) { |
| 104 | + $argsToCheck = $origArgs; |
| 105 | + } elseif (in_array($functionName, $checkFirstArgFunctions, true)) { |
| 106 | + $normalizedArgs = $getNormalizedArgs(); |
| 107 | + if ($normalizedArgs === []) { |
| 108 | + return []; |
| 109 | + } |
| 110 | + $argsToCheck = [0 => $normalizedArgs[0]]; |
| 111 | + } else { |
| 112 | + return []; |
| 113 | + } |
| 114 | + |
| 115 | + $origNamedArgs = []; |
| 116 | + foreach ($origArgs as $arg) { |
| 117 | + if ($arg->unpack || $arg->name === null) { |
| 118 | + continue; |
| 119 | + } |
| 120 | + |
| 121 | + $origNamedArgs[$arg->name->toString()] = $arg; |
| 122 | + } |
| 123 | + |
| 124 | + $errors = []; |
| 125 | + $functionParameters = $parametersAcceptor->getParameters(); |
| 126 | + |
| 127 | + foreach ($argsToCheck as $argIdx => $arg) { |
| 128 | + if ($arg->unpack) { |
| 129 | + continue; |
| 130 | + } |
| 131 | + |
| 132 | + $typeResult = $this->ruleLevelHelper->findTypeToCheck( |
| 133 | + $scope, |
| 134 | + $arg->value, |
| 135 | + '', |
| 136 | + static fn (Type $type): bool => !$type->getIterableValueType()->toString() instanceof ErrorType, |
| 137 | + ); |
| 138 | + |
| 139 | + if ($typeResult->getType() instanceof ErrorType |
| 140 | + || !$typeResult->getType()->getIterableValueType()->toString() instanceof ErrorType) { |
| 141 | + continue; |
| 142 | + } |
| 143 | + |
| 144 | + if (in_array($functionName, $implodeFunctions, true)) { |
| 145 | + // implode has weird variants, so $array has to be fixed. It's especially weird with named arguments. |
| 146 | + if (array_key_exists('array', $origNamedArgs)) { |
| 147 | + $argName = '$array'; |
| 148 | + } elseif (array_key_exists('separator', $origNamedArgs) && count($origArgs) === 1) { |
| 149 | + $argName = '$separator'; |
| 150 | + } else { |
| 151 | + $argName = sprintf('#%d $array', $argIdx + 1); |
| 152 | + } |
| 153 | + } elseif (array_key_exists($argIdx, $functionParameters)) { |
| 154 | + $paramName = $functionParameters[$argIdx]->getName(); |
| 155 | + $argName = array_key_exists($paramName, $origNamedArgs) |
| 156 | + ? sprintf('$%s', $paramName) |
| 157 | + : sprintf('#%d $%s', $argIdx + 1, $paramName); |
| 158 | + } else { |
| 159 | + $argName = sprintf('#%d', $argIdx + 1); |
| 160 | + } |
| 161 | + |
| 162 | + $errors[] = RuleErrorBuilder::message( |
| 163 | + sprintf($errorMessage, $argName, $functionName, $typeResult->getType()->describe(VerbosityLevel::typeOnly())), |
| 164 | + )->identifier('argument.type')->build(); |
| 165 | + } |
| 166 | + |
| 167 | + return $errors; |
| 168 | + } |
| 169 | + |
| 170 | +} |
0 commit comments