|
| 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\DependencyInjection\AutowiredParameter; |
| 10 | +use PHPStan\DependencyInjection\Conditional; |
| 11 | +use PHPStan\DependencyInjection\RegisteredRule; |
| 12 | +use PHPStan\Php\PhpVersion; |
| 13 | +use PHPStan\Reflection\ParametersAcceptorSelector; |
| 14 | +use PHPStan\Reflection\ReflectionProvider; |
| 15 | +use PHPStan\Rules\Rule; |
| 16 | +use PHPStan\Rules\RuleErrorBuilder; |
| 17 | +use PHPStan\Type\Type; |
| 18 | +use PHPStan\Type\VerbosityLevel; |
| 19 | +use function array_key_exists; |
| 20 | +use function count; |
| 21 | +use function sprintf; |
| 22 | + |
| 23 | +/** |
| 24 | + * Reports replace functions called with an array of replacements that is shorter |
| 25 | + * than the array of search values. PHP silently replaces the remaining search values |
| 26 | + * with an empty string, which is usually a mistake. |
| 27 | + * |
| 28 | + * @implements Rule<Node\Expr\FuncCall> |
| 29 | + */ |
| 30 | +#[Conditional( |
| 31 | + attribute: new RegisteredRule(level: 5), |
| 32 | + if: '%featureToggles.replaceCountMismatch%', |
| 33 | +)] |
| 34 | +final class ReplaceCountMismatchRule implements Rule |
| 35 | +{ |
| 36 | + |
| 37 | + private const FUNCTIONS = [ |
| 38 | + 'str_replace' => 'strReplace', |
| 39 | + 'str_ireplace' => 'strReplace', |
| 40 | + 'preg_replace' => 'pregReplace', |
| 41 | + 'preg_filter' => 'pregReplace', |
| 42 | + ]; |
| 43 | + |
| 44 | + public function __construct( |
| 45 | + private ReflectionProvider $reflectionProvider, |
| 46 | + private PhpVersion $phpVersion, |
| 47 | + #[AutowiredParameter] |
| 48 | + private bool $treatPhpDocTypesAsCertain, |
| 49 | + #[AutowiredParameter(ref: '%tips.treatPhpDocTypesAsCertain%')] |
| 50 | + private bool $treatPhpDocTypesAsCertainTip, |
| 51 | + ) |
| 52 | + { |
| 53 | + } |
| 54 | + |
| 55 | + public function getNodeType(): string |
| 56 | + { |
| 57 | + return FuncCall::class; |
| 58 | + } |
| 59 | + |
| 60 | + public function processNode(Node $node, Scope $scope): array |
| 61 | + { |
| 62 | + if (!($node->name instanceof Node\Name)) { |
| 63 | + return []; |
| 64 | + } |
| 65 | + |
| 66 | + if (!$this->reflectionProvider->hasFunction($node->name, $scope)) { |
| 67 | + return []; |
| 68 | + } |
| 69 | + |
| 70 | + $functionReflection = $this->reflectionProvider->getFunction($node->name, $scope); |
| 71 | + $functionName = $functionReflection->getName(); |
| 72 | + if (!array_key_exists($functionName, self::FUNCTIONS)) { |
| 73 | + return []; |
| 74 | + } |
| 75 | + |
| 76 | + $parametersAcceptor = ParametersAcceptorSelector::selectFromArgs( |
| 77 | + $scope, |
| 78 | + $node->getArgs(), |
| 79 | + $functionReflection->getVariants(), |
| 80 | + $functionReflection->getNamedArgumentsVariants(), |
| 81 | + ); |
| 82 | + |
| 83 | + $normalizedFuncCall = ArgumentsNormalizer::reorderFuncArguments($parametersAcceptor, $node); |
| 84 | + if ($normalizedFuncCall === null) { |
| 85 | + return []; |
| 86 | + } |
| 87 | + |
| 88 | + $args = $normalizedFuncCall->getArgs(); |
| 89 | + if (!array_key_exists(0, $args) || !array_key_exists(1, $args)) { |
| 90 | + return []; |
| 91 | + } |
| 92 | + |
| 93 | + $parameters = $parametersAcceptor->getParameters(); |
| 94 | + if (count($parameters) < 2) { |
| 95 | + return []; |
| 96 | + } |
| 97 | + |
| 98 | + if ($this->treatPhpDocTypesAsCertain) { |
| 99 | + $searchType = $scope->getType($args[0]->value); |
| 100 | + $replaceType = $scope->getType($args[1]->value); |
| 101 | + } else { |
| 102 | + $searchType = $scope->getNativeType($args[0]->value); |
| 103 | + $replaceType = $scope->getNativeType($args[1]->value); |
| 104 | + } |
| 105 | + |
| 106 | + if (!$this->hasFewerReplacements($searchType, $replaceType)) { |
| 107 | + return []; |
| 108 | + } |
| 109 | + |
| 110 | + $errorBuilder = RuleErrorBuilder::message(sprintf( |
| 111 | + 'Parameter #2 $%s of function %s has fewer elements (%s) than parameter #1 $%s (%s), the remaining values will be replaced with an empty string.', |
| 112 | + $parameters[1]->getName(), |
| 113 | + $functionName, |
| 114 | + $replaceType->getArraySize()->describe(VerbosityLevel::value()), |
| 115 | + $parameters[0]->getName(), |
| 116 | + $searchType->getArraySize()->describe(VerbosityLevel::value()), |
| 117 | + ))->identifier(sprintf('%s.replaceCount', self::FUNCTIONS[$functionName])); |
| 118 | + |
| 119 | + if ($this->treatPhpDocTypesAsCertain && $this->treatPhpDocTypesAsCertainTip) { |
| 120 | + $nativeSearchType = $scope->getNativeType($args[0]->value); |
| 121 | + $nativeReplaceType = $scope->getNativeType($args[1]->value); |
| 122 | + if (!$this->hasFewerReplacements($nativeSearchType, $nativeReplaceType)) { |
| 123 | + $errorBuilder->treatPhpDocTypesAsCertainTip(); |
| 124 | + } |
| 125 | + } |
| 126 | + |
| 127 | + return [ |
| 128 | + $errorBuilder->build(), |
| 129 | + ]; |
| 130 | + } |
| 131 | + |
| 132 | + private function hasFewerReplacements(Type $searchType, Type $replaceType): bool |
| 133 | + { |
| 134 | + if (!$searchType->isArray()->yes() || !$replaceType->isArray()->yes()) { |
| 135 | + return false; |
| 136 | + } |
| 137 | + |
| 138 | + // an explicitly empty array of replacements is a deliberate way to remove all search values |
| 139 | + if ($replaceType->isIterableAtLeastOnce()->no()) { |
| 140 | + return false; |
| 141 | + } |
| 142 | + |
| 143 | + return $replaceType->getArraySize()->isSmallerThan($searchType->getArraySize(), $this->phpVersion)->yes(); |
| 144 | + } |
| 145 | + |
| 146 | +} |
0 commit comments