Skip to content

Commit

Permalink
[PHP 8.0] Create match right from next Throws (#2803)
Browse files Browse the repository at this point in the history
  • Loading branch information
TomasVotruba committed Aug 19, 2022
1 parent 4bc378b commit 7f7bb19
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 54 deletions.
14 changes: 14 additions & 0 deletions rules/Php80/NodeAnalyzer/MatchSwitchAnalyzer.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,20 @@ public function haveCondAndExprsMatchPotential(array $condAndExprs): bool
return count($assignVariableNames) <= 1;
}

/**
* @param CondAndExpr[] $condAndExprs
*/
public function hasCondsAndExprDefaultValue(array $condAndExprs): bool
{
foreach ($condAndExprs as $condAndExpr) {
if ($condAndExpr->getCondExprs() === null) {
return true;
}
}

return false;
}

public function hasDefaultValue(Match_ $match): bool
{
foreach ($match->arms as $matchArm) {
Expand Down
76 changes: 22 additions & 54 deletions rules/Php80/NodeFactory/MatchFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,49 +30,38 @@ public function __construct(
*/
public function createFromCondAndExprs(Expr $condExpr, array $condAndExprs, ?Stmt $nextStmt): ?MatchResult
{
$matchArms = $this->matchArmsFactory->createFromCondAndExprs($condAndExprs);
$match = new Match_($condExpr, $matchArms);
$shouldRemoteNextStmt = false;

// implicit return default after switch
if ($nextStmt instanceof Return_ && $nextStmt->expr instanceof Expr) {
return $this->processImplicitReturnAfterSwitch($match, $condAndExprs, $nextStmt->expr);
}
// is default value missing? maybe it can be found in next stmt
if (! $this->matchSwitchAnalyzer->hasCondsAndExprDefaultValue($condAndExprs)) {

if ($nextStmt instanceof ThrowsStmt) {
return $this->processImplicitThrowsAfterSwitch($match, $condAndExprs, $nextStmt->expr);
}
// 1. is followed by throws stmts?
if ($nextStmt instanceof ThrowsStmt) {
$throw = new Throw_($nextStmt->expr);
$condAndExprs[] = new CondAndExpr([], $throw, MatchKind::RETURN);

return new MatchResult($match, false);
}
$shouldRemoteNextStmt = true;
}

/**
* @param CondAndExpr[] $condAndExprs
*/
private function processImplicitReturnAfterSwitch(
Match_ $match,
array $condAndExprs,
Expr $returnExpr
): ?MatchResult {
if ($this->matchSwitchAnalyzer->hasDefaultValue($match)) {
return new MatchResult($match, false);
}
// 2. is followed by return expr

$expr = $this->resolveAssignVar($condAndExprs);
if ($expr instanceof ArrayDimFetch) {
return null;
}
// implicit return default after switch
if ($nextStmt instanceof Return_ && $nextStmt->expr instanceof Expr) {
// @todo this should be improved
$expr = $this->resolveAssignVar($condAndExprs);
if ($expr instanceof ArrayDimFetch) {
return null;
}

$shouldRemoveNextStmt = false;
if (! $expr instanceof Expr) {
$shouldRemoveNextStmt = true;
$shouldRemoteNextStmt = ! $expr instanceof Expr;
$condAndExprs[] = new CondAndExpr([], $nextStmt->expr, MatchKind::RETURN);
}
}

$condAndExprs[] = new CondAndExpr([], $returnExpr, MatchKind::RETURN);

$matchArms = $this->matchArmsFactory->createFromCondAndExprs($condAndExprs);
$wrappedMatch = new Match_($match->cond, $matchArms);
$match = new Match_($condExpr, $matchArms);

return new MatchResult($wrappedMatch, $shouldRemoveNextStmt);
return new MatchResult($match, $shouldRemoteNextStmt);
}

/**
Expand All @@ -91,25 +80,4 @@ private function resolveAssignVar(array $condAndExprs): ?Expr

return null;
}

/**
* @param CondAndExpr[] $condAndExprs
*/
private function processImplicitThrowsAfterSwitch(
Match_ $match,
array $condAndExprs,
Expr $throwExpr
): MatchResult {
if ($this->matchSwitchAnalyzer->hasDefaultValue($match)) {
return new MatchResult($match, false);
}

$throw = new Throw_($throwExpr);
$condAndExprs[] = new CondAndExpr([], $throw, MatchKind::RETURN);

$matchArms = $this->matchArmsFactory->createFromCondAndExprs($condAndExprs);
$wrappedMatch = new Match_($match->cond, $matchArms);

return new MatchResult($wrappedMatch, true);
}
}

0 comments on commit 7f7bb19

Please sign in to comment.