Skip to content

Commit

Permalink
Add prettyPrintFile() method
Browse files Browse the repository at this point in the history
  • Loading branch information
nikic committed Apr 15, 2013
1 parent 5fca557 commit 08f0cde
Show file tree
Hide file tree
Showing 7 changed files with 127 additions and 16 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ Version 0.9.4-dev
* The pretty printer now prints namespaces in semicolon-style if possible (i.e. if the file does not contain a global
namespace declaration).

* Added `prettyPrintFile(array $stmts)` method which will pretty print a file of statements including the opening
`<?php` tag if it is required. Use of this method will also eliminate the unnecessary `<?php ?>` at the start and end
of files using inline HTML.

Version 0.9.3 (22.11.2012)
--------------------------

Expand Down
5 changes: 1 addition & 4 deletions lib/PHPParser/PrettyPrinter/Default.php
Original file line number Diff line number Diff line change
Expand Up @@ -676,10 +676,7 @@ public function pStmt_Unset(PHPParser_Node_Stmt_Unset $node) {
}

public function pStmt_InlineHTML(PHPParser_Node_Stmt_InlineHTML $node) {
return '?>' . $this->pNoIndent(
("\n" === $node->value[0] || "\r" === $node->value[0] ? "\n" : '')
. $node->value
) . '<?php ';
return '?>' . $this->pNoIndent("\n" . $node->value) . '<?php ';
}

public function pStmt_HaltCompiler(PHPParser_Node_Stmt_HaltCompiler $node) {
Expand Down
32 changes: 26 additions & 6 deletions lib/PHPParser/PrettyPrinterAbstract.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,16 +70,16 @@ public function __construct() {
}

/**
* Pretty prints an array of nodes (statements).
* Pretty prints an array of statements.
*
* @param PHPParser_Node[] $nodes Array of nodes
* @param PHPParser_Node[] $stmts Array of statements
*
* @return string Pretty printed nodes
* @return string Pretty printed statements
*/
public function prettyPrint(array $nodes) {
$this->preprocessNodes($nodes);
public function prettyPrint(array $stmts) {
$this->preprocessNodes($stmts);

return str_replace("\n" . $this->noIndentToken, "\n", $this->pStmts($nodes, false));
return str_replace("\n" . $this->noIndentToken, "\n", $this->pStmts($stmts, false));
}

/**
Expand All @@ -93,6 +93,26 @@ public function prettyPrintExpr(PHPParser_Node_Expr $node) {
return str_replace("\n" . $this->noIndentToken, "\n", $this->p($node));
}

/**
* Pretty prints a file of statements (includes the opening <?php tag if it is required).
*
* @param PHPParser_Node[] $stmts Array of statements
*
* @return string Pretty printed statements
*/
public function prettyPrintFile(array $stmts) {
$p = trim($this->prettyPrint($stmts));

$p = preg_replace('/^\?>\n?/', '', $p, -1, $count);
$p = preg_replace('/<\?php$/', '', $p);

if (!$count) {
$p = "<?php\n\n" . $p;
}

return $p;
}

/**
* Preprocesses the top-level nodes to initialize pretty printer state.
*
Expand Down
28 changes: 22 additions & 6 deletions test/PHPParser/Tests/PrettyPrinterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,39 @@

class PHPParser_Tests_PrettyPrinterTest extends PHPParser_Tests_CodeTestAbstract
{
/**
* @dataProvider provideTestPrettyPrint
* @covers PHPParser_PrettyPrinter_Zend<extended>
*/
public function testPrettyPrint($name, $code, $dump) {
protected function doTestPrettyPrintMethod($method, $name, $code, $dump) {
$parser = new PHPParser_Parser(new PHPParser_Lexer_Emulative);
$prettyPrinter = new PHPParser_PrettyPrinter_Default;

$stmts = $parser->parse($code);
$this->assertEquals(
$this->canonicalize($dump),
$this->canonicalize($prettyPrinter->prettyPrint($stmts)),
$this->canonicalize($prettyPrinter->$method($stmts)),
$name
);
}

/**
* @dataProvider provideTestPrettyPrint
* @covers PHPParser_PrettyPrinter_Default<extended>
*/
public function testPrettyPrint($name, $code, $dump) {
$this->doTestPrettyPrintMethod('prettyPrint', $name, $code, $dump);
}

/**
* @dataProvider provideTestPrettyPrintFile
* @covers PHPParser_PrettyPrinter_Default<extended>
*/
public function testPrettyPrintFile($name, $code, $dump) {
$this->doTestPrettyPrintMethod('prettyPrintFile', $name, $code, $dump);
}

public function provideTestPrettyPrint() {
return $this->getTests(dirname(__FILE__) . '/../../code/prettyPrinter', 'test');
}

public function provideTestPrettyPrintFile() {
return $this->getTests(dirname(__FILE__) . '/../../code/prettyPrinter', 'file-test');
}
}
52 changes: 52 additions & 0 deletions test/code/prettyPrinter/inlineHTMLandPHPtest.file-test
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
File containing both inline HTML and PHP
-----
HTML
<?php
echo 'PHP';
-----
HTML
<?php
echo 'PHP';
-----
<?php
echo 'PHP';
?>
HTML
-----
<?php

echo 'PHP';
?>
HTML
-----
HTML
<?php
echo 'PHP';
?>
HTML
-----
HTML
<?php
echo 'PHP';
?>
HTML
-----
HTML
<?php
echo 'PHP';
?>
HTML
<?php
echo 'PHP';
?>
HTML
-----
HTML
<?php
echo 'PHP';
?>
HTML
<?php
echo 'PHP';
?>
HTML
11 changes: 11 additions & 0 deletions test/code/prettyPrinter/onlyInlineHTML.file-test
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
File containing only inline HTML
-----
Hallo World
Foo Bar
Bar Foo
World Hallo
-----
Hallo World
Foo Bar
Bar Foo
World Hallo
11 changes: 11 additions & 0 deletions test/code/prettyPrinter/onlyPHP.file-test
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
File containing only PHP
-----
<?php

echo 'Foo Bar';
echo 'Bar Foo';
-----
<?php

echo 'Foo Bar';
echo 'Bar Foo';

0 comments on commit 08f0cde

Please sign in to comment.