2012-05-11 16:18:14 +02:00
|
|
|
<?php
|
|
|
|
|
2014-02-06 14:44:16 +01:00
|
|
|
namespace PhpParser;
|
2012-05-11 16:18:14 +02:00
|
|
|
|
2016-02-28 20:28:32 +01:00
|
|
|
use PhpParser\Comment;
|
2015-01-18 19:57:09 +01:00
|
|
|
use PhpParser\Node\Expr;
|
2016-12-11 16:31:59 +01:00
|
|
|
use PhpParser\Node\Scalar\DNumber;
|
2016-04-02 15:22:24 +02:00
|
|
|
use PhpParser\Node\Scalar\Encapsed;
|
|
|
|
use PhpParser\Node\Scalar\EncapsedStringPart;
|
2016-12-11 16:31:59 +01:00
|
|
|
use PhpParser\Node\Scalar\LNumber;
|
2015-03-20 21:47:20 +01:00
|
|
|
use PhpParser\Node\Scalar\String_;
|
2015-01-18 19:57:09 +01:00
|
|
|
use PhpParser\Node\Stmt;
|
|
|
|
use PhpParser\PrettyPrinter\Standard;
|
|
|
|
|
2014-02-06 14:44:16 +01:00
|
|
|
require_once __DIR__ . '/CodeTestAbstract.php';
|
|
|
|
|
|
|
|
class PrettyPrinterTest extends CodeTestAbstract
|
2012-05-11 16:18:14 +02:00
|
|
|
{
|
2015-09-21 09:12:59 +02:00
|
|
|
protected function doTestPrettyPrintMethod($method, $name, $code, $expected, $modeLine) {
|
2015-06-13 19:46:01 +02:00
|
|
|
$lexer = new Lexer\Emulative;
|
|
|
|
$parser5 = new Parser\Php5($lexer);
|
2016-12-23 14:11:31 +01:00
|
|
|
|
|
|
|
// Use these options on one of the parsers ... don't want to write extra tests for them
|
|
|
|
$parser7 = new Parser\Php7($lexer, [
|
|
|
|
'useIdentifierNodes' => true,
|
|
|
|
'useConsistentVariableNodes' => true,
|
|
|
|
]);
|
2015-09-21 09:12:59 +02:00
|
|
|
|
|
|
|
list($version, $options) = $this->parseModeLine($modeLine);
|
|
|
|
$prettyPrinter = new Standard($options);
|
2012-05-11 16:18:14 +02:00
|
|
|
|
2015-06-13 19:46:01 +02:00
|
|
|
try {
|
2015-06-20 11:43:16 +02:00
|
|
|
$output5 = canonicalize($prettyPrinter->$method($parser5->parse($code)));
|
2015-06-13 19:46:01 +02:00
|
|
|
} catch (Error $e) {
|
|
|
|
$output5 = null;
|
2015-10-02 11:14:31 +02:00
|
|
|
if ('php7' !== $version) {
|
|
|
|
throw $e;
|
|
|
|
}
|
2015-06-13 19:46:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
2015-06-20 11:43:16 +02:00
|
|
|
$output7 = canonicalize($prettyPrinter->$method($parser7->parse($code)));
|
2015-06-13 19:46:01 +02:00
|
|
|
} catch (Error $e) {
|
|
|
|
$output7 = null;
|
2015-10-02 11:14:31 +02:00
|
|
|
if ('php5' !== $version) {
|
|
|
|
throw $e;
|
|
|
|
}
|
2015-06-13 19:46:01 +02:00
|
|
|
}
|
|
|
|
|
2015-09-21 09:12:59 +02:00
|
|
|
if ('php5' === $version) {
|
2015-06-13 19:46:01 +02:00
|
|
|
$this->assertSame($expected, $output5, $name);
|
|
|
|
$this->assertNotSame($expected, $output7, $name);
|
2015-09-21 09:12:59 +02:00
|
|
|
} else if ('php7' === $version) {
|
2015-06-13 19:46:01 +02:00
|
|
|
$this->assertSame($expected, $output7, $name);
|
|
|
|
$this->assertNotSame($expected, $output5, $name);
|
|
|
|
} else {
|
|
|
|
$this->assertSame($expected, $output5, $name);
|
|
|
|
$this->assertSame($expected, $output7, $name);
|
|
|
|
}
|
2012-05-11 16:18:14 +02:00
|
|
|
}
|
|
|
|
|
2013-04-15 20:53:23 +02:00
|
|
|
/**
|
|
|
|
* @dataProvider provideTestPrettyPrint
|
2016-12-22 21:13:42 +01:00
|
|
|
* @covers \PhpParser\PrettyPrinter\Standard<extended>
|
2013-04-15 20:53:23 +02:00
|
|
|
*/
|
2015-06-13 19:46:01 +02:00
|
|
|
public function testPrettyPrint($name, $code, $expected, $mode) {
|
|
|
|
$this->doTestPrettyPrintMethod('prettyPrint', $name, $code, $expected, $mode);
|
2013-04-15 20:53:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider provideTestPrettyPrintFile
|
2016-12-22 21:13:42 +01:00
|
|
|
* @covers \PhpParser\PrettyPrinter\Standard<extended>
|
2013-04-15 20:53:23 +02:00
|
|
|
*/
|
2015-06-13 19:46:01 +02:00
|
|
|
public function testPrettyPrintFile($name, $code, $expected, $mode) {
|
|
|
|
$this->doTestPrettyPrintMethod('prettyPrintFile', $name, $code, $expected, $mode);
|
2013-04-15 20:53:23 +02:00
|
|
|
}
|
|
|
|
|
2012-05-11 16:18:14 +02:00
|
|
|
public function provideTestPrettyPrint() {
|
2014-02-06 14:44:16 +01:00
|
|
|
return $this->getTests(__DIR__ . '/../code/prettyPrinter', 'test');
|
2012-05-11 16:18:14 +02:00
|
|
|
}
|
2013-04-15 20:53:23 +02:00
|
|
|
|
|
|
|
public function provideTestPrettyPrintFile() {
|
2014-02-06 14:44:16 +01:00
|
|
|
return $this->getTests(__DIR__ . '/../code/prettyPrinter', 'file-test');
|
2013-04-15 20:53:23 +02:00
|
|
|
}
|
2015-01-18 19:57:09 +01:00
|
|
|
|
|
|
|
public function testPrettyPrintExpr() {
|
|
|
|
$prettyPrinter = new Standard;
|
|
|
|
$expr = new Expr\BinaryOp\Mul(
|
|
|
|
new Expr\BinaryOp\Plus(new Expr\Variable('a'), new Expr\Variable('b')),
|
|
|
|
new Expr\Variable('c')
|
|
|
|
);
|
|
|
|
$this->assertEquals('($a + $b) * $c', $prettyPrinter->prettyPrintExpr($expr));
|
|
|
|
|
|
|
|
$expr = new Expr\Closure(array(
|
2015-03-20 21:47:20 +01:00
|
|
|
'stmts' => array(new Stmt\Return_(new String_("a\nb")))
|
2015-01-18 19:57:09 +01:00
|
|
|
));
|
|
|
|
$this->assertEquals("function () {\n return 'a\nb';\n}", $prettyPrinter->prettyPrintExpr($expr));
|
|
|
|
}
|
2015-09-21 09:12:59 +02:00
|
|
|
|
2016-02-28 20:28:32 +01:00
|
|
|
public function testCommentBeforeInlineHTML() {
|
|
|
|
$prettyPrinter = new PrettyPrinter\Standard;
|
|
|
|
$comment = new Comment\Doc("/**\n * This is a comment\n */");
|
|
|
|
$stmts = [new Stmt\InlineHTML('Hello World!', ['comments' => [$comment]])];
|
|
|
|
$expected = "<?php\n\n/**\n * This is a comment\n */\n?>\nHello World!";
|
|
|
|
$this->assertSame($expected, $prettyPrinter->prettyPrintFile($stmts));
|
|
|
|
}
|
|
|
|
|
2015-09-21 09:12:59 +02:00
|
|
|
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];
|
|
|
|
}
|
2016-03-09 21:30:39 +01:00
|
|
|
|
|
|
|
public function testArraySyntaxDefault() {
|
|
|
|
$prettyPrinter = new Standard(['shortArraySyntax' => true]);
|
|
|
|
$expr = new Expr\Array_([
|
|
|
|
new Expr\ArrayItem(new String_('val'), new String_('key'))
|
|
|
|
]);
|
|
|
|
$expected = "['key' => 'val']";
|
|
|
|
$this->assertSame($expected, $prettyPrinter->prettyPrintExpr($expr));
|
|
|
|
}
|
2016-04-02 15:22:24 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider provideTestKindAttributes
|
|
|
|
*/
|
|
|
|
public function testKindAttributes($node, $expected) {
|
|
|
|
$prttyPrinter = new PrettyPrinter\Standard;
|
|
|
|
$result = $prttyPrinter->prettyPrintExpr($node);
|
|
|
|
$this->assertSame($expected, $result);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function provideTestKindAttributes() {
|
|
|
|
$nowdoc = ['kind' => String_::KIND_NOWDOC, 'docLabel' => 'STR'];
|
|
|
|
$heredoc = ['kind' => String_::KIND_HEREDOC, 'docLabel' => 'STR'];
|
|
|
|
return [
|
|
|
|
// Defaults to single quoted
|
|
|
|
[new String_('foo'), "'foo'"],
|
|
|
|
// Explicit single/double quoted
|
|
|
|
[new String_('foo', ['kind' => String_::KIND_SINGLE_QUOTED]), "'foo'"],
|
|
|
|
[new String_('foo', ['kind' => String_::KIND_DOUBLE_QUOTED]), '"foo"'],
|
|
|
|
// Fallback from doc string if no label
|
|
|
|
[new String_('foo', ['kind' => String_::KIND_NOWDOC]), "'foo'"],
|
|
|
|
[new String_('foo', ['kind' => String_::KIND_HEREDOC]), '"foo"'],
|
|
|
|
// Fallback if string contains label
|
|
|
|
[new String_("A\nB\nC", ['kind' => String_::KIND_NOWDOC, 'docLabel' => 'A']), "'A\nB\nC'"],
|
|
|
|
[new String_("A\nB\nC", ['kind' => String_::KIND_NOWDOC, 'docLabel' => 'B']), "'A\nB\nC'"],
|
|
|
|
[new String_("A\nB\nC", ['kind' => String_::KIND_NOWDOC, 'docLabel' => 'C']), "'A\nB\nC'"],
|
|
|
|
[new String_("STR;", ['kind' => String_::KIND_NOWDOC, 'docLabel' => 'STR']), "'STR;'"],
|
|
|
|
// Doc string if label not contained (or not in ending position)
|
|
|
|
[new String_("foo", $nowdoc), "<<<'STR'\nfoo\nSTR\n"],
|
|
|
|
[new String_("foo", $heredoc), "<<<STR\nfoo\nSTR\n"],
|
|
|
|
[new String_("STRx", $nowdoc), "<<<'STR'\nSTRx\nSTR\n"],
|
|
|
|
[new String_("xSTR", $nowdoc), "<<<'STR'\nxSTR\nSTR\n"],
|
|
|
|
// Empty doc string variations (encapsed variant does not occur naturally)
|
|
|
|
[new String_("", $nowdoc), "<<<'STR'\nSTR\n"],
|
|
|
|
[new String_("", $heredoc), "<<<STR\nSTR\n"],
|
|
|
|
[new Encapsed([new EncapsedStringPart('')], $heredoc), "<<<STR\nSTR\n"],
|
|
|
|
// Encapsed doc string variations
|
|
|
|
[new Encapsed([new EncapsedStringPart('foo')], $heredoc), "<<<STR\nfoo\nSTR\n"],
|
|
|
|
[new Encapsed([new EncapsedStringPart('foo'), new Expr\Variable('y')], $heredoc), "<<<STR\nfoo{\$y}\nSTR\n"],
|
|
|
|
[new Encapsed([new EncapsedStringPart("\nSTR"), new Expr\Variable('y')], $heredoc), "<<<STR\n\nSTR{\$y}\nSTR\n"],
|
|
|
|
[new Encapsed([new EncapsedStringPart("\nSTR"), new Expr\Variable('y')], $heredoc), "<<<STR\n\nSTR{\$y}\nSTR\n"],
|
|
|
|
[new Encapsed([new Expr\Variable('y'), new EncapsedStringPart("STR\n")], $heredoc), "<<<STR\n{\$y}STR\n\nSTR\n"],
|
|
|
|
// Encapsed doc string fallback
|
|
|
|
[new Encapsed([new Expr\Variable('y'), new EncapsedStringPart("\nSTR")], $heredoc), '"{$y}\\nSTR"'],
|
|
|
|
[new Encapsed([new EncapsedStringPart("STR\n"), new Expr\Variable('y')], $heredoc), '"STR\\n{$y}"'],
|
|
|
|
[new Encapsed([new EncapsedStringPart("STR")], $heredoc), '"STR"'],
|
|
|
|
];
|
|
|
|
}
|
2016-12-11 16:31:59 +01:00
|
|
|
|
|
|
|
/** @dataProvider provideTestUnnaturalLiterals */
|
|
|
|
public function testUnnaturalLiterals($node, $expected) {
|
|
|
|
$prttyPrinter = new PrettyPrinter\Standard;
|
|
|
|
$result = $prttyPrinter->prettyPrintExpr($node);
|
|
|
|
$this->assertSame($expected, $result);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function provideTestUnnaturalLiterals() {
|
|
|
|
return [
|
|
|
|
[new LNumber(-1), '-1'],
|
|
|
|
[new LNumber(-PHP_INT_MAX - 1), '(-' . PHP_INT_MAX . '-1)'],
|
|
|
|
[new LNumber(-1, ['kind' => LNumber::KIND_BIN]), '-0b1'],
|
|
|
|
[new LNumber(-1, ['kind' => LNumber::KIND_OCT]), '-01'],
|
|
|
|
[new LNumber(-1, ['kind' => LNumber::KIND_HEX]), '-0x1'],
|
|
|
|
[new DNumber(\INF), '\INF'],
|
|
|
|
[new DNumber(-\INF), '-\INF'],
|
|
|
|
[new DNumber(-\NAN), '\NAN'],
|
|
|
|
];
|
|
|
|
}
|
2016-12-23 14:11:31 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider provideTestFormatPreservingPrint
|
|
|
|
* @covers \PhpParser\PrettyPrinter\Standard<extended>
|
|
|
|
*/
|
|
|
|
public function testFormatPreservingPrint($name, $code, $modification, $expected, $modeLine) {
|
|
|
|
$lexer = new Lexer\Emulative([
|
|
|
|
'usedAttributes' => [
|
|
|
|
'comments',
|
|
|
|
'startLine', 'endLine',
|
|
|
|
'startTokenPos', 'endTokenPos',
|
|
|
|
],
|
|
|
|
]);
|
|
|
|
|
|
|
|
$parser = new Parser\Php7($lexer, [
|
|
|
|
'useIdentifierNodes' => true,
|
|
|
|
'useConsistentVariableNodes' => true,
|
|
|
|
'useNopStatements' => false,
|
|
|
|
]);
|
|
|
|
|
|
|
|
$traverser = new NodeTraverser();
|
|
|
|
$traverser->addVisitor(new NodeVisitor\CloningVisitor());
|
|
|
|
|
|
|
|
$printer = new PrettyPrinter\Standard();
|
|
|
|
|
|
|
|
$oldStmts = $parser->parse($code);
|
|
|
|
$oldTokens = $lexer->getTokens();
|
|
|
|
|
|
|
|
$newStmts = $traverser->traverse($oldStmts);
|
|
|
|
|
|
|
|
/** @var callable $fn */
|
|
|
|
eval(<<<CODE
|
|
|
|
use PhpParser\Node;
|
|
|
|
use PhpParser\Node\Expr;
|
|
|
|
use PhpParser\Node\Scalar;
|
|
|
|
use PhpParser\Node\Stmt;
|
|
|
|
\$fn = function(&\$stmts) { $modification };
|
|
|
|
CODE
|
|
|
|
);
|
|
|
|
$fn($newStmts);
|
|
|
|
|
|
|
|
$newCode = $printer->printFormatPreserving($newStmts, $oldStmts, $oldTokens);
|
|
|
|
$this->assertSame(canonicalize($expected), canonicalize($newCode), $name);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function provideTestFormatPreservingPrint() {
|
|
|
|
return $this->getTests(__DIR__ . '/../code/formatPreservation', 'test', 3);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider provideTestRoundTripPrint
|
|
|
|
* @covers \PhpParser\PrettyPrinter\Standard<extended>
|
|
|
|
*/
|
|
|
|
public function testRoundTripPrint($name, $code, $expected, $modeLine) {
|
|
|
|
/**
|
|
|
|
* This test makes sure that the format-preserving pretty printer round-trips for all
|
|
|
|
* the pretty printer tests (i.e. returns the input if no changes occurred).
|
|
|
|
*/
|
|
|
|
if (false !== strpos($code, 'new class')) {
|
|
|
|
// Can't preserve formatting on anon classes for now
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
list($version) = $this->parseModeLine($modeLine);
|
|
|
|
|
|
|
|
$lexer = new Lexer\Emulative([
|
|
|
|
'usedAttributes' => [
|
|
|
|
'comments',
|
|
|
|
'startLine', 'endLine',
|
|
|
|
'startTokenPos', 'endTokenPos',
|
|
|
|
],
|
|
|
|
]);
|
|
|
|
|
|
|
|
$parserClass = $version === 'php5' ? Parser\Php5::class : Parser\Php7::class;
|
|
|
|
/** @var Parser $parser */
|
|
|
|
$parser = new $parserClass($lexer, [
|
|
|
|
'useIdentifierNodes' => true,
|
|
|
|
'useConsistentVariableNodes' => true,
|
|
|
|
'useNopStatements' => false,
|
|
|
|
]);
|
|
|
|
|
|
|
|
$traverser = new NodeTraverser();
|
|
|
|
$traverser->addVisitor(new NodeVisitor\CloningVisitor());
|
|
|
|
|
|
|
|
$printer = new PrettyPrinter\Standard();
|
|
|
|
|
|
|
|
try {
|
|
|
|
$oldStmts = $parser->parse($code);
|
|
|
|
} catch (Error $e) {
|
|
|
|
// Can't do a format-preserving print on a file with errors
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$oldTokens = $lexer->getTokens();
|
|
|
|
|
|
|
|
$newStmts = $traverser->traverse($oldStmts);
|
|
|
|
|
|
|
|
$newCode = $printer->printFormatPreserving($newStmts, $oldStmts, $oldTokens);
|
|
|
|
$this->assertSame(canonicalize($code), canonicalize($newCode), $name);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function provideTestRoundTripPrint() {
|
|
|
|
return array_merge(
|
|
|
|
$this->getTests(__DIR__ . '/../code/prettyPrinter', 'test'),
|
|
|
|
$this->getTests(__DIR__ . '/../code/parser', 'test')
|
|
|
|
);
|
|
|
|
}
|
2015-01-18 19:57:09 +01:00
|
|
|
}
|