Skip to content
Browse files

Pretty print namespaces in semicolon-style if possible

  • Loading branch information...
1 parent db18906 commit 81d20bf10e33b9fca0ddf082322a74df1f71c9f8 @nikic committed Jan 15, 2013
View
3 CHANGELOG.md
@@ -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)
--------------------------
View
8 lib/PHPParser/PrettyPrinter/Default.php
@@ -465,8 +465,12 @@ public function pExpr_Yield(PHPParser_Node_Expr_Yield $node) {
// Declarations
public function pStmt_Namespace(PHPParser_Node_Stmt_Namespace $node) {
- return 'namespace' . (null !== $node->name ? ' ' . $this->p($node->name) : '')
- . ' {' . "\n" . $this->pStmts($node->stmts) . "\n" . '}';
+ 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) {
View
22 lib/PHPParser/PrettyPrinterAbstract.php
@@ -63,9 +63,10 @@
);
protected $noIndentToken;
+ protected $canUseSemicolonNamespaces;
public function __construct() {
- $this->noIndentToken = uniqid('_NO_INDENT_');
+ $this->noIndentToken = uniqid('_NO_INDENT_');
}
/**
@@ -76,6 +77,8 @@ public function __construct() {
* @return string Pretty printed nodes
*/
public function prettyPrint(array $nodes) {
+ $this->preprocessNodes($nodes);
+
return str_replace("\n" . $this->noIndentToken, "\n", $this->pStmts($nodes, false));
}
@@ -91,6 +94,21 @@ public function prettyPrintExpr(PHPParser_Node_Expr $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.
*
* @param PHPParser_Node[] $nodes Array of nodes
@@ -201,7 +219,7 @@ protected function pCommaSeparated(array $nodes) {
}
/**
- * 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
*
View
80 test/PHPParser/Tests/NodeVisitor/NameResolverTest.php
@@ -90,53 +90,53 @@ public function testResolveNames() {
public function testResolveLocations() {
$code = <<<EOC
<?php
-namespace NS {
- class A extends B implements C {
- use A;
- }
+namespace NS;
- interface A extends C {
- public function a(A \$a);
- }
+class A extends B implements C {
+ use A;
+}
- A::b();
- A::\$b;
- A::B;
- new A;
- \$a instanceof A;
+interface A extends C {
+ public function a(A \$a);
+}
- namespace\a();
- namespace\A;
+A::b();
+A::\$b;
+A::B;
+new A;
+\$a instanceof A;
- try {
- \$someThing;
- } catch (A \$a) {
- \$someThingElse;
- }
+namespace\a();
+namespace\A;
+
+try {
+ \$someThing;
+} catch (A \$a) {
+ \$someThingElse;
}
EOC;
$expectedCode = <<<EOC
-namespace NS {
- class A extends \\NS\\B implements \\NS\\C
- {
- use \\NS\\A;
- }
- interface A extends \\NS\\C
- {
- public function a(\\NS\\A \$a);
- }
- \\NS\\A::b();
- \\NS\\A::\$b;
- \\NS\\A::B;
- new \\NS\\A();
- \$a instanceof \\NS\\A;
- \\NS\\a();
- \\NS\\A;
- try {
- \$someThing;
- } catch (\\NS\\A \$a) {
- \$someThingElse;
- }
+namespace NS;
+
+class A extends \\NS\\B implements \\NS\\C
+{
+ use \\NS\\A;
+}
+interface A extends \\NS\\C
+{
+ public function a(\\NS\\A \$a);
+}
+\\NS\\A::b();
+\\NS\\A::\$b;
+\\NS\\A::B;
+new \\NS\\A();
+\$a instanceof \\NS\\A;
+\\NS\\a();
+\\NS\\A;
+try {
+ \$someThing;
+} catch (\\NS\\A \$a) {
+ \$someThingElse;
}
EOC;
View
6 test/code/prettyPrinter/comments.test
@@ -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 */
View
58 test/code/prettyPrinter/namespaces.test
@@ -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()
+ {
+
+ }
+}

0 comments on commit 81d20bf

Please sign in to comment.
Something went wrong with that request. Please try again.