Skip to content

Commit 3b72271

Browse files
authored
Cleanup instanceof Type checks
1 parent 79326d6 commit 3b72271

File tree

2 files changed

+17
-16
lines changed

2 files changed

+17
-16
lines changed

src/Analyser/MutatingScope.php

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4310,24 +4310,29 @@ public function specifyExpressionType(Expr $expr, Type $type, Type $nativeType,
43104310
$scope = $this;
43114311
if ($expr instanceof Expr\ArrayDimFetch && $expr->dim !== null) {
43124312
$dimType = $scope->getType($expr->dim)->toArrayKey();
4313-
if ($dimType instanceof ConstantIntegerType || $dimType instanceof ConstantStringType) {
4313+
if ($dimType->isInteger()->yes() || $dimType->isString()->yes()) {
43144314
$exprVarType = $scope->getType($expr->var);
43154315
if (!$exprVarType instanceof MixedType && !$exprVarType->isArray()->no()) {
43164316
$types = [
43174317
new ArrayType(new MixedType(), new MixedType()),
43184318
new ObjectType(ArrayAccess::class),
43194319
new NullType(),
43204320
];
4321-
if ($dimType instanceof ConstantIntegerType) {
4321+
if ($dimType->isInteger()->yes()) {
43224322
$types[] = new StringType();
43234323
}
4324+
$offsetValueType = TypeCombinator::intersect($exprVarType, TypeCombinator::union(...$types));
4325+
4326+
if ($dimType instanceof ConstantIntegerType || $dimType instanceof ConstantStringType) {
4327+
$offsetValueType = TypeCombinator::intersect(
4328+
$offsetValueType,
4329+
new HasOffsetValueType($dimType, $type),
4330+
);
4331+
}
43244332

43254333
$scope = $scope->specifyExpressionType(
43264334
$expr->var,
4327-
TypeCombinator::intersect(
4328-
TypeCombinator::intersect($exprVarType, TypeCombinator::union(...$types)),
4329-
new HasOffsetValueType($dimType, $type),
4330-
),
4335+
$offsetValueType,
43314336
$scope->getNativeType($expr->var),
43324337
$certainty,
43334338
);

src/Rules/UnusedFunctionParametersCheck.php

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,6 @@
99
use PHPStan\DependencyInjection\AutowiredService;
1010
use PHPStan\Reflection\ReflectionProvider;
1111
use PHPStan\ShouldNotHappenException;
12-
use function array_combine;
13-
use function array_map;
1412
use function array_merge;
1513
use function in_array;
1614
use function is_array;
@@ -43,20 +41,18 @@ public function getUnusedParameters(
4341
string $identifier,
4442
): array
4543
{
46-
$parameterNames = array_map(static function (Variable $variable): string {
44+
$unusedParameters = [];
45+
foreach ($parameterVars as $variable) {
4746
if (!is_string($variable->name)) {
4847
throw new ShouldNotHappenException();
4948
}
50-
return $variable->name;
51-
}, $parameterVars);
52-
$unusedParameters = array_combine($parameterNames, $parameterVars);
53-
foreach ($this->getUsedVariables($scope, $statements) as $variableName) {
54-
if (!isset($unusedParameters[$variableName])) {
55-
continue;
56-
}
5749

50+
$unusedParameters[$variable->name] = $variable;
51+
}
52+
foreach ($this->getUsedVariables($scope, $statements) as $variableName) {
5853
unset($unusedParameters[$variableName]);
5954
}
55+
6056
$errors = [];
6157
foreach ($unusedParameters as $name => $variable) {
6258
$errorBuilder = RuleErrorBuilder::message(sprintf($unusedParameterMessage, $name))->identifier($identifier);

0 commit comments

Comments
 (0)