From 073da14c4497bda9496abd36324fb39cc6ae2c3f Mon Sep 17 00:00:00 2001 From: Markus Staab Date: Sun, 28 Sep 2025 10:13:42 +0200 Subject: [PATCH 1/2] Remove instanceof Type checks --- src/Analyser/MutatingScope.php | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/src/Analyser/MutatingScope.php b/src/Analyser/MutatingScope.php index 6a073dd1a3..af1ab31238 100644 --- a/src/Analyser/MutatingScope.php +++ b/src/Analyser/MutatingScope.php @@ -4310,7 +4310,7 @@ public function specifyExpressionType(Expr $expr, Type $type, Type $nativeType, $scope = $this; if ($expr instanceof Expr\ArrayDimFetch && $expr->dim !== null) { $dimType = $scope->getType($expr->dim)->toArrayKey(); - if ($dimType instanceof ConstantIntegerType || $dimType instanceof ConstantStringType) { + if ($dimType->isInteger()->yes() || $dimType->isString()->yes()) { $exprVarType = $scope->getType($expr->var); if (!$exprVarType instanceof MixedType && !$exprVarType->isArray()->no()) { $types = [ @@ -4318,16 +4318,21 @@ public function specifyExpressionType(Expr $expr, Type $type, Type $nativeType, new ObjectType(ArrayAccess::class), new NullType(), ]; - if ($dimType instanceof ConstantIntegerType) { + if ($dimType->isInteger()->yes()) { $types[] = new StringType(); } + $offsetValueType = TypeCombinator::intersect($exprVarType, TypeCombinator::union(...$types)); + + if ($dimType instanceof ConstantIntegerType || $dimType instanceof ConstantStringType) { + $offsetValueType = TypeCombinator::intersect( + $offsetValueType, + new HasOffsetValueType($dimType, $type), + ); + } $scope = $scope->specifyExpressionType( $expr->var, - TypeCombinator::intersect( - TypeCombinator::intersect($exprVarType, TypeCombinator::union(...$types)), - new HasOffsetValueType($dimType, $type), - ), + $offsetValueType, $scope->getNativeType($expr->var), $certainty, ); From 2621cdc0eb166d1bd2fa8e9ecdedd58a1afe6338 Mon Sep 17 00:00:00 2001 From: Markus Staab Date: Sun, 28 Sep 2025 10:29:05 +0200 Subject: [PATCH 2/2] Simplify UnusedFunctionParametersCheck --- src/Rules/UnusedFunctionParametersCheck.php | 16 ++++++---------- 1 file changed, 6 insertions(+), 10 deletions(-) diff --git a/src/Rules/UnusedFunctionParametersCheck.php b/src/Rules/UnusedFunctionParametersCheck.php index 10ec0753e1..9e36e01387 100644 --- a/src/Rules/UnusedFunctionParametersCheck.php +++ b/src/Rules/UnusedFunctionParametersCheck.php @@ -9,8 +9,6 @@ use PHPStan\DependencyInjection\AutowiredService; use PHPStan\Reflection\ReflectionProvider; use PHPStan\ShouldNotHappenException; -use function array_combine; -use function array_map; use function array_merge; use function in_array; use function is_array; @@ -43,20 +41,18 @@ public function getUnusedParameters( string $identifier, ): array { - $parameterNames = array_map(static function (Variable $variable): string { + $unusedParameters = []; + foreach ($parameterVars as $variable) { if (!is_string($variable->name)) { throw new ShouldNotHappenException(); } - return $variable->name; - }, $parameterVars); - $unusedParameters = array_combine($parameterNames, $parameterVars); - foreach ($this->getUsedVariables($scope, $statements) as $variableName) { - if (!isset($unusedParameters[$variableName])) { - continue; - } + $unusedParameters[$variable->name] = $variable; + } + foreach ($this->getUsedVariables($scope, $statements) as $variableName) { unset($unusedParameters[$variableName]); } + $errors = []; foreach ($unusedParameters as $name => $variable) { $errorBuilder = RuleErrorBuilder::message(sprintf($unusedParameterMessage, $name))->identifier($identifier);