mirror of
https://github.com/phabelio/PHP-Parser.git
synced 2024-11-26 20:14:46 +01:00
Add prettyPrintFile() method
This commit is contained in:
parent
5fca55702b
commit
08f0cde6f9
@ -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)
|
||||
--------------------------
|
||||
|
||||
|
@ -676,10 +676,7 @@ class PHPParser_PrettyPrinter_Default extends PHPParser_PrettyPrinterAbstract
|
||||
}
|
||||
|
||||
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) {
|
||||
|
@ -70,16 +70,16 @@ abstract class PHPParser_PrettyPrinterAbstract
|
||||
}
|
||||
|
||||
/**
|
||||
* 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));
|
||||
}
|
||||
|
||||
/**
|
||||
@ -93,6 +93,26 @@ abstract class PHPParser_PrettyPrinterAbstract
|
||||
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.
|
||||
*
|
||||
|
@ -4,23 +4,39 @@ require_once dirname(__FILE__) . '/CodeTestAbstract.php';
|
||||
|
||||
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
test/code/prettyPrinter/inlineHTMLandPHPtest.file-test
Normal file
52
test/code/prettyPrinter/inlineHTMLandPHPtest.file-test
Normal file
@ -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
test/code/prettyPrinter/onlyInlineHTML.file-test
Normal file
11
test/code/prettyPrinter/onlyInlineHTML.file-test
Normal file
@ -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
test/code/prettyPrinter/onlyPHP.file-test
Normal file
11
test/code/prettyPrinter/onlyPHP.file-test
Normal file
@ -0,0 +1,11 @@
|
||||
File containing only PHP
|
||||
-----
|
||||
<?php
|
||||
|
||||
echo 'Foo Bar';
|
||||
echo 'Bar Foo';
|
||||
-----
|
||||
<?php
|
||||
|
||||
echo 'Foo Bar';
|
||||
echo 'Bar Foo';
|
Loading…
Reference in New Issue
Block a user