mirror of
https://github.com/danog/PHP-Parser.git
synced 2024-11-27 04:14:44 +01:00
Pretty print namespaces in semicolon-style if possible
This commit is contained in:
parent
db18906dfc
commit
81d20bf10e
@ -11,6 +11,9 @@ Version 0.9.4-dev
|
||||
very loosely applies the Zend Coding Standard. The class `PHPParser_PrettyPrinter_Zend` extends
|
||||
`PHPParser_PrettyPrinter_Default` to maintain backwards compatibility.
|
||||
|
||||
* The pretty printer now prints namespaces in semicolon-style if possible (i.e. if the file does not contain a global
|
||||
namespace declaration).
|
||||
|
||||
Version 0.9.3 (22.11.2012)
|
||||
--------------------------
|
||||
|
||||
|
@ -465,9 +465,13 @@ class PHPParser_PrettyPrinter_Default extends PHPParser_PrettyPrinterAbstract
|
||||
// Declarations
|
||||
|
||||
public function pStmt_Namespace(PHPParser_Node_Stmt_Namespace $node) {
|
||||
if ($this->canUseSemicolonNamespaces) {
|
||||
return 'namespace ' . $this->p($node->name) . ';' . "\n\n" . $this->pStmts($node->stmts, false);
|
||||
} else {
|
||||
return 'namespace' . (null !== $node->name ? ' ' . $this->p($node->name) : '')
|
||||
. ' {' . "\n" . $this->pStmts($node->stmts) . "\n" . '}';
|
||||
}
|
||||
}
|
||||
|
||||
public function pStmt_Use(PHPParser_Node_Stmt_Use $node) {
|
||||
return 'use ' . $this->pCommaSeparated($node->uses) . ';';
|
||||
|
@ -63,6 +63,7 @@ abstract class PHPParser_PrettyPrinterAbstract
|
||||
);
|
||||
|
||||
protected $noIndentToken;
|
||||
protected $canUseSemicolonNamespaces;
|
||||
|
||||
public function __construct() {
|
||||
$this->noIndentToken = uniqid('_NO_INDENT_');
|
||||
@ -76,6 +77,8 @@ abstract class PHPParser_PrettyPrinterAbstract
|
||||
* @return string Pretty printed nodes
|
||||
*/
|
||||
public function prettyPrint(array $nodes) {
|
||||
$this->preprocessNodes($nodes);
|
||||
|
||||
return str_replace("\n" . $this->noIndentToken, "\n", $this->pStmts($nodes, false));
|
||||
}
|
||||
|
||||
@ -90,6 +93,21 @@ abstract class PHPParser_PrettyPrinterAbstract
|
||||
return str_replace("\n" . $this->noIndentToken, "\n", $this->p($node));
|
||||
}
|
||||
|
||||
/**
|
||||
* Preprocesses the top-level nodes to initialize pretty printer state.
|
||||
*
|
||||
* @param PHPParser_Node[] $nodes Array of nodes
|
||||
*/
|
||||
protected function preprocessNodes(array $nodes) {
|
||||
/* We can use semicolon-namespaces unless there is a global namespace declaration */
|
||||
$this->canUseSemicolonNamespaces = true;
|
||||
foreach ($nodes as $node) {
|
||||
if ($node instanceof PHPParser_Node_Stmt_Namespace && null === $node->name) {
|
||||
$this->canUseSemicolonNamespaces = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Pretty prints an array of nodes (statements) and indents them optionally.
|
||||
*
|
||||
@ -201,7 +219,7 @@ abstract class PHPParser_PrettyPrinterAbstract
|
||||
}
|
||||
|
||||
/**
|
||||
* Signifies the pretty printer that a string shall not be indented.
|
||||
* Signals the pretty printer that a string shall not be indented.
|
||||
*
|
||||
* @param string $string Not to be indented string
|
||||
*
|
||||
|
@ -90,7 +90,8 @@ EOC;
|
||||
public function testResolveLocations() {
|
||||
$code = <<<EOC
|
||||
<?php
|
||||
namespace NS {
|
||||
namespace NS;
|
||||
|
||||
class A extends B implements C {
|
||||
use A;
|
||||
}
|
||||
@ -113,10 +114,10 @@ namespace NS {
|
||||
} catch (A \$a) {
|
||||
\$someThingElse;
|
||||
}
|
||||
}
|
||||
EOC;
|
||||
$expectedCode = <<<EOC
|
||||
namespace NS {
|
||||
namespace NS;
|
||||
|
||||
class A extends \\NS\\B implements \\NS\\C
|
||||
{
|
||||
use \\NS\\A;
|
||||
@ -137,7 +138,6 @@ namespace NS {
|
||||
} catch (\\NS\\A \$a) {
|
||||
\$someThingElse;
|
||||
}
|
||||
}
|
||||
EOC;
|
||||
|
||||
$parser = new PHPParser_Parser(new PHPParser_Lexer_Emulative);
|
||||
|
@ -2,7 +2,8 @@ Comments
|
||||
-----
|
||||
<?php
|
||||
|
||||
namespace JustForIndentation {
|
||||
function justForIndentation()
|
||||
{
|
||||
// Some text
|
||||
# Some text
|
||||
/* Some text */
|
||||
@ -28,7 +29,8 @@ namespace JustForIndentation {
|
||||
}
|
||||
|
||||
-----
|
||||
namespace JustForIndentation {
|
||||
function justForIndentation()
|
||||
{
|
||||
// Some text
|
||||
# Some text
|
||||
/* Some text */
|
||||
|
58
test/code/prettyPrinter/namespaces.test
Normal file
58
test/code/prettyPrinter/namespaces.test
Normal file
@ -0,0 +1,58 @@
|
||||
Namespaces
|
||||
-----
|
||||
<?php
|
||||
|
||||
namespace Foo;
|
||||
|
||||
function foo()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
namespace Bar;
|
||||
|
||||
function bar()
|
||||
{
|
||||
|
||||
}
|
||||
-----
|
||||
namespace Foo;
|
||||
|
||||
function foo()
|
||||
{
|
||||
|
||||
}
|
||||
namespace Bar;
|
||||
|
||||
function bar()
|
||||
{
|
||||
|
||||
}
|
||||
-----
|
||||
<?php
|
||||
|
||||
namespace Foo {
|
||||
function foo()
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
namespace {
|
||||
function glob() {
|
||||
|
||||
}
|
||||
}
|
||||
-----
|
||||
namespace Foo {
|
||||
function foo()
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
namespace {
|
||||
function glob()
|
||||
{
|
||||
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user