function ParserAbstract::handleNamespaces
Moves statements of semicolon-style namespaces into $ns->stmts and checks various error conditions.
Parameters
Node\Stmt[] $stmts:
Return value
Node\Stmt[]
File
-
vendor/
nikic/ php-parser/ lib/ PhpParser/ ParserAbstract.php, line 561
Class
Namespace
PhpParserCode
protected function handleNamespaces(array $stmts) : array {
$hasErrored = false;
$style = $this->getNamespacingStyle($stmts);
if (null === $style) {
// not namespaced, nothing to do
return $stmts;
}
if ('brace' === $style) {
// For braced namespaces we only have to check that there are no invalid statements between the namespaces
$afterFirstNamespace = false;
foreach ($stmts as $stmt) {
if ($stmt instanceof Node\Stmt\Namespace_) {
$afterFirstNamespace = true;
}
elseif (!$stmt instanceof Node\Stmt\HaltCompiler && !$stmt instanceof Node\Stmt\Nop && $afterFirstNamespace && !$hasErrored) {
$this->emitError(new Error('No code may exist outside of namespace {}', $stmt->getAttributes()));
$hasErrored = true;
// Avoid one error for every statement
}
}
return $stmts;
}
else {
// For semicolon namespaces we have to move the statements after a namespace declaration into ->stmts
$resultStmts = [];
$targetStmts =& $resultStmts;
$lastNs = null;
foreach ($stmts as $stmt) {
if ($stmt instanceof Node\Stmt\Namespace_) {
if ($lastNs !== null) {
$this->fixupNamespaceAttributes($lastNs);
}
if ($stmt->stmts === null) {
$stmt->stmts = [];
$targetStmts =& $stmt->stmts;
$resultStmts[] = $stmt;
}
else {
// This handles the invalid case of mixed style namespaces
$resultStmts[] = $stmt;
$targetStmts =& $resultStmts;
}
$lastNs = $stmt;
}
elseif ($stmt instanceof Node\Stmt\HaltCompiler) {
// __halt_compiler() is not moved into the namespace
$resultStmts[] = $stmt;
}
else {
$targetStmts[] = $stmt;
}
}
if ($lastNs !== null) {
$this->fixupNamespaceAttributes($lastNs);
}
return $resultStmts;
}
}