Skip to content

Commit 3c7d9c8

Browse files
committed
Match expression isset() fix
1 parent 1ffed9b commit 3c7d9c8

3 files changed

Lines changed: 46 additions & 29 deletions

File tree

src/Analyser/MutatingScope.php

Lines changed: 29 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -703,35 +703,6 @@ private function resolveType(Expr $node): Type
703703
return new ConstantBooleanType(!$result);
704704
}
705705

706-
if ($node instanceof Expr\Isset_) {
707-
$issetResult = true;
708-
foreach ($node->vars as $var) {
709-
$result = $this->issetCheck($var, static function (Type $type): ?bool {
710-
$isNull = (new NullType())->isSuperTypeOf($type);
711-
if ($isNull->maybe()) {
712-
return null;
713-
}
714-
715-
return !$isNull->yes();
716-
});
717-
if ($result !== null) {
718-
if (!$result) {
719-
return new ConstantBooleanType($result);
720-
}
721-
722-
continue;
723-
}
724-
725-
$issetResult = $result;
726-
}
727-
728-
if ($issetResult === null) {
729-
return new BooleanType();
730-
}
731-
732-
return new ConstantBooleanType($issetResult);
733-
}
734-
735706
if ($node instanceof Node\Expr\BooleanNot) {
736707
if ($this->treatPhpDocTypesAsCertain) {
737708
$exprBooleanType = $this->getType($node->expr)->toBoolean();
@@ -2020,6 +1991,35 @@ private function resolveType(Expr $node): Type
20201991
return $this->moreSpecificTypes[$exprString]->getType();
20211992
}
20221993

1994+
if ($node instanceof Expr\Isset_) {
1995+
$issetResult = true;
1996+
foreach ($node->vars as $var) {
1997+
$result = $this->issetCheck($var, static function (Type $type): ?bool {
1998+
$isNull = (new NullType())->isSuperTypeOf($type);
1999+
if ($isNull->maybe()) {
2000+
return null;
2001+
}
2002+
2003+
return !$isNull->yes();
2004+
});
2005+
if ($result !== null) {
2006+
if (!$result) {
2007+
return new ConstantBooleanType($result);
2008+
}
2009+
2010+
continue;
2011+
}
2012+
2013+
$issetResult = $result;
2014+
}
2015+
2016+
if ($issetResult === null) {
2017+
return new BooleanType();
2018+
}
2019+
2020+
return new ConstantBooleanType($issetResult);
2021+
}
2022+
20232023
if ($node instanceof Expr\AssignOp\Coalesce) {
20242024
return $this->getType(new BinaryOp\Coalesce($node->var, $node->expr, $node->getAttributes()));
20252025
}

tests/PHPStan/Rules/Comparison/MatchExpressionRuleTest.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,4 +185,13 @@ public function testBug6115(): void
185185
]);
186186
}
187187

188+
public function testBug7095(): void
189+
{
190+
if (PHP_VERSION_ID < 80000) {
191+
$this->markTestSkipped('Test requires PHP 8.0.');
192+
}
193+
194+
$this->analyse([__DIR__ . '/data/bug-7095.php'], []);
195+
}
196+
188197
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php // lint >= 8.0
2+
3+
namespace Bug7095;
4+
5+
match (isset($foo)) {
6+
true => 'a',
7+
false => 'b',
8+
};

0 commit comments

Comments
 (0)