1
0
mirror of https://github.com/danog/PHP-Parser.git synced 2025-01-22 13:51:12 +01:00

Add option for short array syntax in pretty printer

This commit is contained in:
Nikita Popov 2015-09-21 15:12:59 +08:00
parent fcf23101dd
commit 39a039fa42
4 changed files with 45 additions and 8 deletions

View File

@ -422,7 +422,11 @@ class Standard extends PrettyPrinterAbstract
} }
public function pExpr_Array(Expr\Array_ $node) { 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) { public function pExpr_ArrayItem(Expr\ArrayItem $node) {

View File

@ -76,9 +76,21 @@ abstract class PrettyPrinterAbstract
protected $noIndentToken; protected $noIndentToken;
protected $canUseSemicolonNamespaces; 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(); $this->noIndentToken = '_NO_INDENT_' . mt_rand();
$defaultOptions = ['shortArraySyntax' => false];
$this->options = $options + $defaultOptions;
} }
/** /**

View File

@ -11,30 +11,32 @@ require_once __DIR__ . '/CodeTestAbstract.php';
class PrettyPrinterTest extends CodeTestAbstract class PrettyPrinterTest extends CodeTestAbstract
{ {
protected function doTestPrettyPrintMethod($method, $name, $code, $expected, $mode) { protected function doTestPrettyPrintMethod($method, $name, $code, $expected, $modeLine) {
$lexer = new Lexer\Emulative; $lexer = new Lexer\Emulative;
$parser5 = new Parser\Php5($lexer); $parser5 = new Parser\Php5($lexer);
$parser7 = new Parser\Php7($lexer); $parser7 = new Parser\Php7($lexer);
$prettyPrinter = new Standard;
list($version, $options) = $this->parseModeLine($modeLine);
$prettyPrinter = new Standard($options);
try { try {
$output5 = canonicalize($prettyPrinter->$method($parser5->parse($code))); $output5 = canonicalize($prettyPrinter->$method($parser5->parse($code)));
} catch (Error $e) { } catch (Error $e) {
$output5 = null; $output5 = null;
$this->assertEquals('php7', $mode); $this->assertEquals('php7', $version);
} }
try { try {
$output7 = canonicalize($prettyPrinter->$method($parser7->parse($code))); $output7 = canonicalize($prettyPrinter->$method($parser7->parse($code)));
} catch (Error $e) { } catch (Error $e) {
$output7 = null; $output7 = null;
$this->assertEquals('php5', $mode); $this->assertEquals('php5', $version);
} }
if ('php5' === $mode) { if ('php5' === $version) {
$this->assertSame($expected, $output5, $name); $this->assertSame($expected, $output5, $name);
$this->assertNotSame($expected, $output7, $name); $this->assertNotSame($expected, $output7, $name);
} else if ('php7' === $mode) { } else if ('php7' === $version) {
$this->assertSame($expected, $output7, $name); $this->assertSame($expected, $output7, $name);
$this->assertNotSame($expected, $output5, $name); $this->assertNotSame($expected, $output5, $name);
} else { } else {
@ -80,4 +82,11 @@ class PrettyPrinterTest extends CodeTestAbstract
)); ));
$this->assertEquals("function () {\n return 'a\nb';\n}", $prettyPrinter->prettyPrintExpr($expr)); $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];
}
} }

View 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'];