/** * @param \PHPStan\Analyser\Scope $initialScope * @param \PHPStan\Analyser\StatementList[] $statementsLists * @param bool $isSwitchCase * @return Scope */ private function lookForAssignsInBranches(Scope $initialScope, array $statementsLists, bool $isSwitchCase = false) : Scope { $intersectedScope = null; $previousBranchScope = null; foreach ($statementsLists as $i => $statementList) { $statements = $statementList->getStatements(); $branchScope = $statementList->getScope(); if ($statements === null) { continue; } $mergeWithPrevious = $isSwitchCase; foreach ($statements as $statement) { $branchScope = $this->lookForAssigns($branchScope, $statement); $hasStatementEarlyTermination = $this->hasStatementEarlyTermination($statement, $branchScope); if ($hasStatementEarlyTermination && !$isSwitchCase) { continue 2; } } if ($intersectedScope === null) { $intersectedScope = $initialScope->addVariables($branchScope); } elseif ($mergeWithPrevious) { if ($previousBranchScope !== null) { $intersectedScope = $branchScope->addVariables($previousBranchScope); } } else { $intersectedScope = $branchScope->intersectVariables($intersectedScope); } $previousBranchScope = $branchScope; } return $intersectedScope !== null ? $intersectedScope : $initialScope; }