/** * @param Expr\Array_ $expr * @param Context $context * @return bool */ public function pass(Expr\Array_ $expr, Context $context) { if ($expr->getAttribute('kind') == Expr\Array_::KIND_LONG) { $context->notice('array.short-syntax', 'Please use [] (short syntax) for array definition.', $expr); return true; } return false; }
public function pExpr_Array(Expr\Array_ $node) { $syntax = $node->getAttribute('kind', $this->options['shortArraySyntax'] ? Expr\Array_::KIND_SHORT : Expr\Array_::KIND_LONG); if ($syntax === Expr\Array_::KIND_SHORT) { return '[' . $this->pCommaSeparated($node->items) . ']'; } else { return 'array(' . $this->pCommaSeparated($node->items) . ')'; } }
/** * print an associative array */ public function pExpr_Array(\PhpParser\Node\Expr\Array_ $node) { $multiLine = FALSE; $startLine = $node->getAttribute('startLine'); $endLine = $node->getAttribute('endLine'); if ($startLine != $endLine) { $multiLine = TRUE; } $printedNodes = ''; foreach ($node->items as $itemNode) { $glueToken = ", "; if ($itemNode->getAttribute('startLine') != $startLine) { $glueToken = ',' . LF; $startLine = $itemNode->getAttribute('startLine'); } if (!empty($printedNodes)) { $printedNodes .= $glueToken . $this->p($itemNode); } else { $printedNodes .= $this->p($itemNode); } } if ($multiLine) { $multiLinedItems = $this->indentToken . preg_replace('~\\n(?!$|' . $this->noIndentToken . ')~', LF . $this->indentToken, $printedNodes); return 'array(' . LF . $multiLinedItems . LF . ')'; } else { return parent::pExpr_Array($node); } }
/** * @param Expr\Array_ $parent * @param Expr\ArrayItem[] $nodes * @return string */ public function pArrayList(Expr\Array_ $parent, array $nodes) { $lineNumbers = [$parent->getAttribute('startLine', null)]; foreach ($nodes as $node) { $lineNumbers[] = $node->getAttribute('startLine', null); } //all the same line if (count(array_unique($lineNumbers)) == 1) { return $this->pCommaSeparated($nodes); } $output = "\n"; foreach ($nodes as $key => $node) { $output .= sprintf(" %s,\n", $this->p($node)); } return $output; }