mirror of
https://github.com/phabelio/PHP-Parser.git
synced 2024-11-27 04:24:43 +01:00
Add option for short array syntax in pretty printer
This commit is contained in:
parent
fcf23101dd
commit
39a039fa42
@ -422,7 +422,11 @@ class Standard extends PrettyPrinterAbstract
|
||||
}
|
||||
|
||||
public function pExpr_Array(Expr\Array_ $node) {
|
||||
return 'array(' . $this->pCommaSeparated($node->items) . ')';
|
||||
if ($this->options['shortArraySyntax']) {
|
||||
return '[' . $this->pCommaSeparated($node->items) . ']';
|
||||
} else {
|
||||
return 'array(' . $this->pCommaSeparated($node->items) . ')';
|
||||
}
|
||||
}
|
||||
|
||||
public function pExpr_ArrayItem(Expr\ArrayItem $node) {
|
||||
|
@ -76,9 +76,21 @@ abstract class PrettyPrinterAbstract
|
||||
|
||||
protected $noIndentToken;
|
||||
protected $canUseSemicolonNamespaces;
|
||||
protected $options;
|
||||
|
||||
public function __construct() {
|
||||
/**
|
||||
* Creates a pretty printer instance using the given options.
|
||||
*
|
||||
* Supported options:
|
||||
* * bool $shortArraySyntax = false: Whether to use [] instead of array()
|
||||
*
|
||||
* @param array $options Dictionary of formatting options
|
||||
*/
|
||||
public function __construct(array $options = []) {
|
||||
$this->noIndentToken = '_NO_INDENT_' . mt_rand();
|
||||
|
||||
$defaultOptions = ['shortArraySyntax' => false];
|
||||
$this->options = $options + $defaultOptions;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -11,30 +11,32 @@ require_once __DIR__ . '/CodeTestAbstract.php';
|
||||
|
||||
class PrettyPrinterTest extends CodeTestAbstract
|
||||
{
|
||||
protected function doTestPrettyPrintMethod($method, $name, $code, $expected, $mode) {
|
||||
protected function doTestPrettyPrintMethod($method, $name, $code, $expected, $modeLine) {
|
||||
$lexer = new Lexer\Emulative;
|
||||
$parser5 = new Parser\Php5($lexer);
|
||||
$parser7 = new Parser\Php7($lexer);
|
||||
$prettyPrinter = new Standard;
|
||||
|
||||
list($version, $options) = $this->parseModeLine($modeLine);
|
||||
$prettyPrinter = new Standard($options);
|
||||
|
||||
try {
|
||||
$output5 = canonicalize($prettyPrinter->$method($parser5->parse($code)));
|
||||
} catch (Error $e) {
|
||||
$output5 = null;
|
||||
$this->assertEquals('php7', $mode);
|
||||
$this->assertEquals('php7', $version);
|
||||
}
|
||||
|
||||
try {
|
||||
$output7 = canonicalize($prettyPrinter->$method($parser7->parse($code)));
|
||||
} catch (Error $e) {
|
||||
$output7 = null;
|
||||
$this->assertEquals('php5', $mode);
|
||||
$this->assertEquals('php5', $version);
|
||||
}
|
||||
|
||||
if ('php5' === $mode) {
|
||||
if ('php5' === $version) {
|
||||
$this->assertSame($expected, $output5, $name);
|
||||
$this->assertNotSame($expected, $output7, $name);
|
||||
} else if ('php7' === $mode) {
|
||||
} else if ('php7' === $version) {
|
||||
$this->assertSame($expected, $output7, $name);
|
||||
$this->assertNotSame($expected, $output5, $name);
|
||||
} else {
|
||||
@ -80,4 +82,11 @@ class PrettyPrinterTest extends CodeTestAbstract
|
||||
));
|
||||
$this->assertEquals("function () {\n return 'a\nb';\n}", $prettyPrinter->prettyPrintExpr($expr));
|
||||
}
|
||||
|
||||
private function parseModeLine($modeLine) {
|
||||
$parts = explode(' ', $modeLine, 2);
|
||||
$version = isset($parts[0]) ? $parts[0] : 'both';
|
||||
$options = isset($parts[1]) ? json_decode($parts[1], true) : [];
|
||||
return [$version, $options];
|
||||
}
|
||||
}
|
||||
|
12
test/code/prettyPrinter/shortArraySyntax.test
Normal file
12
test/code/prettyPrinter/shortArraySyntax.test
Normal file
@ -0,0 +1,12 @@
|
||||
Short array syntax
|
||||
-----
|
||||
<?php
|
||||
|
||||
[];
|
||||
array(1, 2, 3);
|
||||
['a' => 'b', 'c' => 'd'];
|
||||
-----
|
||||
!!both {"shortArraySyntax": true}
|
||||
[];
|
||||
[1, 2, 3];
|
||||
['a' => 'b', 'c' => 'd'];
|
Loading…
Reference in New Issue
Block a user