Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix scope in enum match arm body #3786

Open
wants to merge 1 commit into
base: 2.1.x
Choose a base branch
from
Open
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
46 changes: 29 additions & 17 deletions src/Analyser/NodeScopeResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -3641,6 +3641,7 @@ static function (Node $node, Scope $scope) use ($nodeCallback): void {

$condNodes = [];
$conditionCases = [];
$conditionExprs = [];
foreach ($arm->conds as $cond) {
if (!$cond instanceof Expr\ClassConstFetch) {
continue 2;
Expand Down Expand Up @@ -3698,6 +3699,7 @@ static function (Node $node, Scope $scope) use ($nodeCallback): void {
$armConditionScope,
$cond->getStartLine(),
);
$conditionExprs[] = $cond;

unset($unusedIndexedEnumCases[$loweredFetchedClassName][$caseName]);
}
Expand All @@ -3711,10 +3713,11 @@ static function (Node $node, Scope $scope) use ($nodeCallback): void {
$conditionCaseType = new UnionType($conditionCases);
}

$filteringExpr = $this->getFilteringExprForMatchArm($expr, $conditionExprs);
$matchArmBodyScope = $matchScope->addTypeToExpression(
$expr->cond,
$conditionCaseType,
);
)->filterByTruthyValue($filteringExpr);
$matchArmBody = new MatchExpressionArmBody($matchArmBodyScope, $arm->body);
$armNodes[$i] = new MatchExpressionArm($matchArmBody, $condNodes, $arm->getStartLine());

Expand Down Expand Up @@ -3790,22 +3793,7 @@ static function (Node $node, Scope $scope) use ($nodeCallback): void {
$filteringExprs[] = $armCond;
}

if (count($filteringExprs) === 1) {
$filteringExpr = new BinaryOp\Identical($expr->cond, $filteringExprs[0]);
} else {
$items = [];
foreach ($filteringExprs as $filteringExpr) {
$items[] = new Node\ArrayItem($filteringExpr);
}
$filteringExpr = new FuncCall(
new Name\FullyQualified('in_array'),
[
new Arg($expr->cond),
new Arg(new Array_($items)),
new Arg(new ConstFetch(new Name\FullyQualified('true'))),
],
);
}
$filteringExpr = $this->getFilteringExprForMatchArm($expr, $filteringExprs);

$bodyScope = $this->processExprNode($stmt, $filteringExpr, $matchScope, static function (): void {
}, $deepContext)->getTruthyScope();
Expand Down Expand Up @@ -6598,4 +6586,28 @@ private function getNextUnreachableStatements(array $nodes, bool $earlyBinding):
return $stmts;
}

/**
* @param array<Expr> $conditions
*/
public function getFilteringExprForMatchArm(Expr\Match_ $expr, array $conditions): BinaryOp\Identical|FuncCall
{
if (count($conditions) === 1) {
return new BinaryOp\Identical($expr->cond, $conditions[0]);
}

$items = [];
foreach ($conditions as $filteringExpr) {
$items[] = new Node\ArrayItem($filteringExpr);
}

return new FuncCall(
new Name\FullyQualified('in_array'),
[
new Arg($expr->cond),
new Arg(new Array_($items)),
new Arg(new ConstFetch(new Name\FullyQualified('true'))),
],
);
}

}
1 change: 1 addition & 0 deletions tests/PHPStan/Analyser/NodeScopeResolverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ private static function findTestFiles(): iterable
define('TEST_ARRAY_CONSTANT', [true, false, null]);
define('TEST_ENUM_CONSTANT', Foo::ONE);
yield __DIR__ . '/data/new-in-initializers-runtime.php';
yield __DIR__ . '/data/scope-in-enum-match-arm-body.php';
}

yield __DIR__ . '/../Rules/Comparison/data/bug-6473.php';
Expand Down
26 changes: 26 additions & 0 deletions tests/PHPStan/Analyser/data/scope-in-enum-match-arm-body.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

namespace ScopeInEnumMatchArmBody;

use function PHPStan\Testing\assertType;

enum Foo: int
{
case ALLOW_NULLABLE_INT = 1;
case ALLOW_ONLY_INT = 2;

public function bar(?int $nullable): void
{
if ($nullable === null && $this === self::ALLOW_ONLY_INT) {
throw new \LogicException('Cannot be null');
}

match ($this) {
self::ALLOW_ONLY_INT => assertType('int', $nullable),
self::ALLOW_NULLABLE_INT => assertType('int|null', $nullable),
};
}
}



Loading