Skip to content

Commit

Permalink
Decorate reasons when comparing ConstantArrayType
Browse files Browse the repository at this point in the history
  • Loading branch information
ondrejmirtes committed Oct 18, 2024
1 parent 34bacd7 commit dc5d8f4
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 1 deletion.
13 changes: 13 additions & 0 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,11 @@ parameters:
count: 1
path: src/Analyser/NodeScopeResolver.php

-
message: "#^Doing instanceof PHPStan\\\\Type\\\\Constant\\\\ConstantBooleanType is error\\-prone and deprecated\\. Use Type\\:\\:isTrue\\(\\) or Type\\:\\:isFalse\\(\\) instead\\.$#"
count: 1
path: src/Analyser/RicherScopeGetTypeHelper.php

-
message: "#^Doing instanceof PHPStan\\\\Type\\\\ConstantScalarType is error\\-prone and deprecated\\. Use Type\\:\\:isConstantScalarValue\\(\\) or Type\\:\\:getConstantScalarTypes\\(\\) or Type\\:\\:getConstantScalarValues\\(\\) instead\\.$#"
count: 2
Expand Down Expand Up @@ -487,6 +492,14 @@ parameters:
count: 2
path: src/Rules/Comparison/NumberComparisonOperatorsConstantConditionRule.php

-
message: """
#^Call to deprecated method doNotTreatPhpDocTypesAsCertain\\(\\) of interface PHPStan\\\\Analyser\\\\Scope\\:
Use getNativeType\\(\\)$#
"""
count: 2
path: src/Rules/Comparison/StrictComparisonOfDifferentTypesRule.php

-
message: "#^Doing instanceof PHPStan\\\\Type\\\\Constant\\\\ConstantBooleanType is error\\-prone and deprecated\\. Use Type\\:\\:isTrue\\(\\) or Type\\:\\:isFalse\\(\\) instead\\.$#"
count: 2
Expand Down
4 changes: 4 additions & 0 deletions src/Analyser/RicherScopeGetTypeHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ public function getIdenticalResult(Scope $scope, Identical $expr): TypeResult
$leftType = $scope->getType($expr->left);
$rightType = $scope->getType($expr->right);

if (!$scope instanceof MutatingScope) {
return $this->initializerExprTypeResolver->resolveIdenticalType($leftType, $rightType);
}

if (
(
$expr->left instanceof Node\Expr\PropertyFetch
Expand Down
2 changes: 1 addition & 1 deletion src/Type/Constant/ConstantArrayType.php
Original file line number Diff line number Diff line change
Expand Up @@ -390,7 +390,7 @@ public function isSuperTypeOfWithReason(Type $type): IsSuperTypeOfResult

$isValueSuperType = $this->valueTypes[$i]->isSuperTypeOfWithReason($type->getOffsetValueType($keyType));
if ($isValueSuperType->no()) {
return $isValueSuperType;
return $isValueSuperType->decorateReasons(static fn (string $reason) => sprintf('Offset %s: %s', $keyType->describe(VerbosityLevel::value()), $reason));
}
$results[] = $isValueSuperType;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,11 @@ public function testStrictComparison(): void
1034,
'Type null has already been eliminated from mixed.',
],
[
'Strict comparison using !== between array{1, mixed, 3} and array{int, null, int} will always evaluate to true.',
1048,
'Offset 1: Type null has already been eliminated from mixed.',
],
],
);
}
Expand Down
14 changes: 14 additions & 0 deletions tests/PHPStan/Rules/Comparison/data/strict-comparison.php
Original file line number Diff line number Diff line change
Expand Up @@ -1036,4 +1036,18 @@ public function doFoo($m): void
}
}

public function doBar($m, int $i, int $j): void
{
if ($m === null) {
return;
}

$a = [1, $m, 3];
$b = [$i, null, $j];

if ($a !== $b) {

}
}

}

0 comments on commit dc5d8f4

Please sign in to comment.