Skip to content

Commit

Permalink
Fix gettype in match
Browse files Browse the repository at this point in the history
  • Loading branch information
ondrejmirtes committed Jun 19, 2023
1 parent aa66b84 commit caa0a5b
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/Analyser/TypeSpecifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,9 @@ public function specifyTypesInCondition(
}

if ($types !== null) {
if ($leftExpr !== $unwrappedLeftExpr) {
$types = $types->unionWith($this->create($leftExpr, $rightType, $context, false, $scope, $rootExpr));
}
return $types;
}
}
Expand Down
9 changes: 9 additions & 0 deletions tests/PHPStan/Rules/Comparison/MatchExpressionRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -498,4 +498,13 @@ public function testBug9457(): void
$this->analyse([__DIR__ . '/data/bug-9457.php'], []);
}

public function testBug8614(): void
{
if (PHP_VERSION_ID < 80100) {
$this->markTestSkipped('Test requires PHP 8.1.');
}

$this->analyse([__DIR__ . '/data/bug-8614.php'], []);
}

}
14 changes: 14 additions & 0 deletions tests/PHPStan/Rules/Comparison/data/bug-8614.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php // lint >= 8.1

namespace Bug8614;

/**
* @param int|float|bool|string|object|mixed[] $value
*/
function stringify(int|float|bool|string|object|array $value): string
{
return match (gettype($value)) {
'integer', 'double', 'boolean', 'string' => (string) $value,
'object', 'array' => var_export($value, true),
};
}

0 comments on commit caa0a5b

Please sign in to comment.