function Renderer::renderStatementBlock
Renders a node as a block statement
Parameters
Syntax\Node\Node $parent Parent node:
Syntax\Node\Node|array $node Node or array of : nodes to render
bool $forceBrackets Overrides brackets: inserting rules
bool $mandatorySeparator True if a starting: separator is mandatory
bool $addSemicolons Semicolons are: inserted automatically if this parameter is not false
bool $incIndent If false indentation: level won't be incremented
Return value
string
1 call to Renderer::renderStatementBlock()
- Renderer::renderNode in vendor/
mck89/ peast/ lib/ Peast/ Renderer.php - Renders a node
File
-
vendor/
mck89/ peast/ lib/ Peast/ Renderer.php, line 898
Class
- Renderer
- Nodes renderer class
Namespace
PeastCode
protected function renderStatementBlock($parent, $node, $forceBrackets = null, $mandatorySeparator = false, $addSemicolons = true, $incIndent = true) {
$code = "";
//If node is an array with only one element, handle it as a single node
if (is_array($node) && count($node) === 1) {
$node = $node[0];
}
//Special handling of BlockStatement and ClassBody nodes by rendering
//their child nodes
$origNode = null;
if (!is_array($node) && in_array($node->getType(), array(
"BlockStatement",
"ClassBody",
))) {
$origNode = $node;
$node = $node->getBody();
}
//If $forceBrackets is not null use its value to override curly brackets
//insertion rules
if ($forceBrackets !== null) {
$hasBrackets = $forceBrackets;
}
else {
//Insert curly brackets if needed
$hasBrackets = $this->needsBrackets($parent, $node);
}
$currentIndentation = $this->getIndentation();
//If the node must be wrapped in curly braces a separator defined by formatter
//must be inserted
if ($hasBrackets) {
if ($this->renderOpts->nlbc) {
$code .= $this->renderOpts->nl . $currentIndentation;
}
else {
$code .= $this->renderOpts->sao;
}
}
elseif ($parent->getType() === "SwitchCase") {
$code .= $this->renderOpts->nl;
}
$emptyBody = is_array($node) && !count($node);
if ($this->renderOpts->com && $origNode) {
$code .= $this->renderComments($origNode, true, !$emptyBody);
}
//Insert open curly bracket if required
if ($hasBrackets) {
$code .= "{" . $this->renderOpts->nl;
}
elseif ($mandatorySeparator) {
//If bracket is not inserted but a separator is still required
//a space is added
$code .= " ";
}
//Increase indentation level
if ($incIndent) {
$this->renderOpts->indLevel++;
}
$subIndentation = $this->getIndentation();
//Render the node or the array of nodes
if (is_array($node)) {
if (!$emptyBody) {
$code .= $subIndentation . $this->joinNodes($node, $this->renderOpts->nl . $subIndentation, $addSemicolons);
}
}
else {
$code .= $subIndentation . $this->renderNode($node, $addSemicolons && $this->requiresSemicolon($node));
}
if ($this->renderOpts->com) {
//Strip last new line and indentations added by comments rendering
if (!$emptyBody) {
$code = $this->trimEmptyLine($code);
}
if ($origNode) {
$code .= $this->renderComments($origNode, false, !$emptyBody);
if (!$emptyBody) {
$code = $this->trimEmptyLine($code);
}
}
}
//Reset the indentation level
if ($incIndent) {
$this->renderOpts->indLevel--;
}
//Insert closing curly bracket if required
if ($hasBrackets) {
//Add a new line if something was rendered
if (!$emptyBody) {
$code .= $this->renderOpts->nl;
}
$code .= $currentIndentation . "}";
}
return $code;
}