Skip to content

Commit

Permalink
Remove deprecated Error constructor
Browse files Browse the repository at this point in the history
And use this chance to provide accurate position information for
namespace errors.
  • Loading branch information
nikic committed Sep 11, 2022
1 parent 40c89cf commit 032b102
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 16 deletions.
13 changes: 4 additions & 9 deletions lib/PhpParser/Error.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,12 @@ class Error extends \RuntimeException {
/**
* Creates an Exception signifying a parse error.
*
* @param string $message Error message
* @param array|int $attributes Attributes of node/token where error occurred
* (or start line of error -- deprecated)
* @param string $message Error message
* @param array<string, mixed> $attributes Attributes of node/token where error occurred
*/
public function __construct(string $message, $attributes = []) {
public function __construct(string $message, array $attributes = []) {
$this->rawMessage = $message;
if (is_array($attributes)) {
$this->attributes = $attributes;
} else {
$this->attributes = ['startLine' => $attributes];
}
$this->attributes = $attributes;
$this->updateMessage();
}

Expand Down
19 changes: 17 additions & 2 deletions lib/PhpParser/ParserAbstract.php
Original file line number Diff line number Diff line change
Expand Up @@ -579,6 +579,21 @@ private function fixupNamespaceAttributes(Node\Stmt\Namespace_ $stmt): void {
}
}

private function getNamespaceErrorAttributes(Namespace_ $node): array {
$attrs = $node->getAttributes();
// Adjust end attributes to only cover the "namespace" keyword, not the whole namespace.
if (isset($attrs['startLine'])) {
$attrs['endLine'] = $attrs['startLine'];
}
if (isset($attrs['startTokenPos'])) {
$attrs['endTokenPos'] = $attrs['startTokenPos'];
}
if (isset($attrs['startFilePos'])) {
$attrs['endFilePos'] = $attrs['startFilePos'] + \strlen('namespace') - 1;
}
return $attrs;
}

/**
* Determine namespacing style (semicolon or brace)
*
Expand All @@ -597,13 +612,13 @@ private function getNamespacingStyle(array $stmts): ?string {
if ($hasNotAllowedStmts) {
$this->emitError(new Error(
'Namespace declaration statement has to be the very first statement in the script',
$stmt->getLine() // Avoid marking the entire namespace as an error
$this->getNamespaceErrorAttributes($stmt)
));
}
} elseif ($style !== $currentStyle) {
$this->emitError(new Error(
'Cannot mix bracketed namespace declarations with unbracketed namespace declarations',
$stmt->getLine() // Avoid marking the entire namespace as an error
$this->getNamespaceErrorAttributes($stmt)
));
// Treat like semicolon style for namespace normalization
return 'semicolon';
Expand Down
2 changes: 1 addition & 1 deletion test/PhpParser/ErrorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public function provideTestColumnInfo() {
}

public function testNoColumnInfo() {
$error = new Error('Some error', 3);
$error = new Error('Some error', ['startLine' => 3]);

$this->assertFalse($error->hasColumnInfo());
try {
Expand Down
6 changes: 3 additions & 3 deletions test/code/parser/stmt/namespace/mix.test
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ namespace B {
}
echo 3;
-----
Cannot mix bracketed namespace declarations with unbracketed namespace declarations on line 4
Cannot mix bracketed namespace declarations with unbracketed namespace declarations from 4:1 to 4:9
array(
0: Stmt_Namespace(
name: Name(
Expand Down Expand Up @@ -59,7 +59,7 @@ echo 2;
namespace B;
echo 3;
-----
Cannot mix bracketed namespace declarations with unbracketed namespace declarations on line 6
Cannot mix bracketed namespace declarations with unbracketed namespace declarations from 6:1 to 6:9
array(
0: Stmt_Namespace(
name: Name(
Expand Down Expand Up @@ -100,4 +100,4 @@ array(
)
)
)
)
)
2 changes: 1 addition & 1 deletion test/code/parser/stmt/namespace/outsideStmtInvalid.test
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ echo 1;
echo 2;
namespace A;
-----
Namespace declaration statement has to be the very first statement in the script on line 4
Namespace declaration statement has to be the very first statement in the script from 4:1 to 4:9
array(
0: Stmt_Echo(
exprs: array(
Expand Down

0 comments on commit 032b102

Please sign in to comment.