mirror of
https://github.com/nikic/PHP-Parser.git
synced 2025-07-09 16:36:31 +02:00
Compare commits
1 Commits
Author | SHA1 | Date | |
---|---|---|---|
c4bbc8e236 |
@ -444,7 +444,7 @@ abstract class ParserAbstract
|
|||||||
private function getNamespacingStyle(array $stmts) {
|
private function getNamespacingStyle(array $stmts) {
|
||||||
$style = null;
|
$style = null;
|
||||||
$hasNotAllowedStmts = false;
|
$hasNotAllowedStmts = false;
|
||||||
foreach ($stmts as $stmt) {
|
foreach ($stmts as $i => $stmt) {
|
||||||
if ($stmt instanceof Node\Stmt\Namespace_) {
|
if ($stmt instanceof Node\Stmt\Namespace_) {
|
||||||
$currentStyle = null === $stmt->stmts ? 'semicolon' : 'brace';
|
$currentStyle = null === $stmt->stmts ? 'semicolon' : 'brace';
|
||||||
if (null === $style) {
|
if (null === $style) {
|
||||||
@ -455,9 +455,21 @@ abstract class ParserAbstract
|
|||||||
} elseif ($style !== $currentStyle) {
|
} elseif ($style !== $currentStyle) {
|
||||||
throw new Error('Cannot mix bracketed namespace declarations with unbracketed namespace declarations', $stmt->getLine());
|
throw new Error('Cannot mix bracketed namespace declarations with unbracketed namespace declarations', $stmt->getLine());
|
||||||
}
|
}
|
||||||
} elseif (!$stmt instanceof Node\Stmt\Declare_ && !$stmt instanceof Node\Stmt\HaltCompiler) {
|
continue;
|
||||||
$hasNotAllowedStmts = true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* declare() and __halt_compiler() can be used before a namespace declaration */
|
||||||
|
if ($stmt instanceof Node\Stmt\Declare_ || $stmt instanceof Node\Stmt\HaltCompiler) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* There may be a hashbang line at the very start of the file */
|
||||||
|
if ($i == 0 && $stmt instanceof Node\Stmt\InlineHTML && preg_match('/\A#!.*\r?\n\z/', $stmt->value)) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Everything else if forbidden before namespace declarations */
|
||||||
|
$hasNotAllowedStmts = true;
|
||||||
}
|
}
|
||||||
return $style;
|
return $style;
|
||||||
}
|
}
|
||||||
|
22
test/code/parser/stmt/namespace/nsAfterHashbang.test
Normal file
22
test/code/parser/stmt/namespace/nsAfterHashbang.test
Normal file
@ -0,0 +1,22 @@
|
|||||||
|
Hashbang followed by namespace declaration
|
||||||
|
-----
|
||||||
|
#!/usr/bin/env php
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace A;
|
||||||
|
-----
|
||||||
|
array(
|
||||||
|
0: Stmt_InlineHTML(
|
||||||
|
value: #!/usr/bin/env php
|
||||||
|
|
||||||
|
)
|
||||||
|
1: Stmt_Namespace(
|
||||||
|
name: Name(
|
||||||
|
parts: array(
|
||||||
|
0: A
|
||||||
|
)
|
||||||
|
)
|
||||||
|
stmts: array(
|
||||||
|
)
|
||||||
|
)
|
||||||
|
)
|
Reference in New Issue
Block a user