Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions src/Analyser/TypeSpecifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -2568,7 +2568,7 @@ private function resolveNormalizedIdentical(Expr\BinaryOp\Identical $expr, Scope
$argType = $scope->getType($unwrappedLeftExpr->getArgs()[0]->value);
$isZero = (new ConstantIntegerType(0))->isSuperTypeOf($rightType);
if ($isZero->yes()) {
$funcTypes = $this->create($unwrappedLeftExpr, $rightType, $context, $scope)->setRootExpr($expr);
$funcTypes = $this->create($leftExpr, $rightType, $context, $scope)->setRootExpr($expr);

if ($context->truthy() && !$argType->isArray()->yes()) {
$newArgType = new UnionType([
Expand All @@ -2586,11 +2586,15 @@ private function resolveNormalizedIdentical(Expr\BinaryOp\Identical $expr, Scope

$specifiedTypes = $this->specifyTypesForCountFuncCall($unwrappedLeftExpr, $argType, $rightType, $context, $scope, $expr);
if ($specifiedTypes !== null) {
if ($leftExpr !== $unwrappedLeftExpr) {
$funcTypes = $this->create($leftExpr, $rightType, $context, $scope)->setRootExpr($expr);
return $specifiedTypes->unionWith($funcTypes);
}
return $specifiedTypes;
}

if ($context->truthy() && $argType->isArray()->yes()) {
$funcTypes = $this->create($unwrappedLeftExpr, $rightType, $context, $scope)->setRootExpr($expr);
$funcTypes = $this->create($leftExpr, $rightType, $context, $scope)->setRootExpr($expr);
if (IntegerRangeType::fromInterval(1, null)->isSuperTypeOf($rightType)->yes()) {
return $funcTypes->unionWith(
$this->create($unwrappedLeftExpr->getArgs()[0]->value, new NonEmptyArrayType(), $context, $scope)->setRootExpr($expr),
Expand All @@ -2616,7 +2620,7 @@ private function resolveNormalizedIdentical(Expr\BinaryOp\Identical $expr, Scope

$isZero = (new ConstantIntegerType(0))->isSuperTypeOf($rightType);
if ($isZero->yes()) {
$funcTypes = $this->create($unwrappedLeftExpr, $rightType, $context, $scope)->setRootExpr($expr);
$funcTypes = $this->create($leftExpr, $rightType, $context, $scope)->setRootExpr($expr);
return $funcTypes->unionWith(
$this->create($unwrappedLeftExpr->getArgs()[0]->value, new ConstantStringType(''), $context, $scope)->setRootExpr($expr),
);
Expand All @@ -2625,7 +2629,7 @@ private function resolveNormalizedIdentical(Expr\BinaryOp\Identical $expr, Scope
if ($context->truthy() && IntegerRangeType::fromInterval(1, null)->isSuperTypeOf($rightType)->yes()) {
$argType = $scope->getType($unwrappedLeftExpr->getArgs()[0]->value);
if ($argType->isString()->yes()) {
$funcTypes = $this->create($unwrappedLeftExpr, $rightType, $context, $scope)->setRootExpr($expr);
$funcTypes = $this->create($leftExpr, $rightType, $context, $scope)->setRootExpr($expr);

$accessory = new AccessoryNonEmptyStringType();
if (IntegerRangeType::fromInterval(2, null)->isSuperTypeOf($rightType)->yes()) {
Expand Down
6 changes: 6 additions & 0 deletions tests/PHPStan/Rules/Comparison/MatchExpressionRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -453,6 +453,12 @@ public function testBug13029(): void
$this->analyse([__DIR__ . '/data/bug-13029.php'], []);
}

#[RequiresPhp('>= 8.0')]
public function testBug12790(): void
{
$this->analyse([__DIR__ . '/data/bug-12790.php'], []);
}

#[RequiresPhp('>= 8.0')]
public function testBug11310(): void
{
Expand Down
24 changes: 24 additions & 0 deletions tests/PHPStan/Rules/Comparison/data/bug-12790.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php // lint >= 8.0

declare(strict_types = 1);

namespace Bug12790;

$r = [];
$r[] = 'a';
if (rand(0, 1)) {
$r[] = 'b';
}

echo match (count($r)) {
1 => 'one',
2 => 'two',
};

/** @param 'a'|'ab' $s */
function matchStrlen(string $s): string {
return match (strlen($s)) {
1 => 'one',
2 => 'two',
};
}
Loading