Synchronized error messages with native php error messages

This commit is contained in:
Martin Hasoň
2014-01-23 13:33:02 +01:00
committed by nikic
parent 6f36a88993
commit 118f28344d
14 changed files with 61 additions and 44 deletions

View File

@@ -166,7 +166,7 @@ inner_statement:
statement { $$ = $1; } statement { $$ = $1; }
| function_declaration_statement { $$ = $1; } | function_declaration_statement { $$ = $1; }
| class_declaration_statement { $$ = $1; } | class_declaration_statement { $$ = $1; }
| T_HALT_COMPILER { error('__halt_compiler() can only be used from the outermost scope'); } | T_HALT_COMPILER { error('__HALT_COMPILER() can only be used from the outermost scope'); }
; ;
statement: statement:

View File

@@ -148,7 +148,7 @@ class PHPParser_Lexer
// this simplifies the situation, by not allowing any comments // this simplifies the situation, by not allowing any comments
// in between of the tokens. // in between of the tokens.
if (!preg_match('~\s*\(\s*\)\s*(?:;|\?>\r?\n?)~', $textAfter, $matches)) { if (!preg_match('~\s*\(\s*\)\s*(?:;|\?>\r?\n?)~', $textAfter, $matches)) {
throw new PHPParser_Error('__halt_compiler must be followed by "();"'); throw new PHPParser_Error('__HALT_COMPILER must be followed by "();"');
} }
// prevent the lexer from returning any further tokens // prevent the lexer from returning any further tokens
@@ -192,4 +192,4 @@ class PHPParser_Lexer
return $tokenMap; return $tokenMap;
} }
} }

View File

@@ -46,16 +46,16 @@ class PHPParser_Node_Stmt_Class extends PHPParser_Node_Stmt
$this->name = $name; $this->name = $name;
if (isset(self::$specialNames[(string) $this->name])) { if (isset(self::$specialNames[(string) $this->name])) {
throw new PHPParser_Error(sprintf('Cannot use "%s" as class name as it is reserved', $this->name)); throw new PHPParser_Error(sprintf('Cannot use \'%s\' as class name as it is reserved', $this->name));
} }
if (isset(self::$specialNames[(string) $this->extends])) { if (isset(self::$specialNames[(string) $this->extends])) {
throw new PHPParser_Error(sprintf('Cannot use "%s" as class name as it is reserved', $this->extends)); throw new PHPParser_Error(sprintf('Cannot use \'%s\' as class name as it is reserved', $this->extends));
} }
foreach ($this->implements as $interface) { foreach ($this->implements as $interface) {
if (isset(self::$specialNames[(string) $interface])) { if (isset(self::$specialNames[(string) $interface])) {
throw new PHPParser_Error(sprintf('Cannot use "%s" as interface name as it is reserved', $interface)); throw new PHPParser_Error(sprintf('Cannot use \'%s\' as interface name as it is reserved', $interface));
} }
} }
} }
@@ -96,7 +96,7 @@ class PHPParser_Node_Stmt_Class extends PHPParser_Node_Stmt
} }
if ($a & 48 && $b & 48) { if ($a & 48 && $b & 48) {
throw new PHPParser_Error('Cannot use the final and abstract modifier at the same time'); throw new PHPParser_Error('Cannot use the final modifier on an abstract class member');
} }
} }
} }

View File

@@ -33,10 +33,15 @@ class PHPParser_Node_Stmt_ClassMethod extends PHPParser_Node_Stmt
); );
$this->name = $name; $this->name = $name;
if (($this->type & PHPParser_Node_Stmt_Class::MODIFIER_STATIC) if ($this->type & PHPParser_Node_Stmt_Class::MODIFIER_STATIC) {
&& ('__construct' == $this->name || '__destruct' == $this->name || '__clone' == $this->name) switch (strtolower($this->name)) {
) { case '__construct':
throw new PHPParser_Error(sprintf('"%s" method cannot be static', $this->name)); throw new PHPParser_Error(sprintf('Constructor %s() cannot be static', $this->name));
case '__destruct':
throw new PHPParser_Error(sprintf('Destructor %s() cannot be static', $this->name));
case '__clone':
throw new PHPParser_Error(sprintf('Clone method %s() cannot be static', $this->name));
}
} }
} }
@@ -63,4 +68,4 @@ class PHPParser_Node_Stmt_ClassMethod extends PHPParser_Node_Stmt
public function isStatic() { public function isStatic() {
return (bool) ($this->type & PHPParser_Node_Stmt_Class::MODIFIER_STATIC); return (bool) ($this->type & PHPParser_Node_Stmt_Class::MODIFIER_STATIC);
} }
} }

View File

@@ -33,13 +33,13 @@ class PHPParser_Node_Stmt_Interface extends PHPParser_Node_Stmt
$this->name = $name; $this->name = $name;
if (isset(self::$specialNames[(string) $this->name])) { if (isset(self::$specialNames[(string) $this->name])) {
throw new PHPParser_Error(sprintf('Cannot use "%s" as interface name as it is reserved', $this->name)); throw new PHPParser_Error(sprintf('Cannot use \'%s\' as class name as it is reserved', $this->name));
} }
foreach ($this->extends as $interface) { foreach ($this->extends as $interface) {
if (isset(self::$specialNames[(string) $interface])) { if (isset(self::$specialNames[(string) $interface])) {
throw new PHPParser_Error(sprintf('Cannot use "%s" as interface name as it is reserved', $interface)); throw new PHPParser_Error(sprintf('Cannot use \'%s\' as interface name as it is reserved', $interface));
} }
} }
} }
} }

View File

@@ -29,7 +29,7 @@ class PHPParser_Node_Stmt_Namespace extends PHPParser_Node_Stmt
); );
if (isset(self::$specialNames[(string) $this->name])) { if (isset(self::$specialNames[(string) $this->name])) {
throw new PHPParser_Error(sprintf('Cannot use "%s" as namespace name as it is reserved', $this->name)); throw new PHPParser_Error(sprintf('Cannot use \'%s\' as namespace name', $this->name));
} }
if (null !== $this->stmts) { if (null !== $this->stmts) {
@@ -119,4 +119,4 @@ class PHPParser_Node_Stmt_Namespace extends PHPParser_Node_Stmt
return $newStmts; return $newStmts;
} }
} }
} }

View File

@@ -20,7 +20,7 @@ class PHPParser_Node_Stmt_UseUse extends PHPParser_Node_Stmt
if ('self' == $alias || 'parent' == $alias) { if ('self' == $alias || 'parent' == $alias) {
throw new PHPParser_Error(sprintf( throw new PHPParser_Error(sprintf(
'Cannot use "%s" as "%s" because "%2$s" is a special class name', 'Cannot use %s as %s because \'%2$s\' is a special class name',
$name, $alias $name, $alias
)); ));
} }
@@ -33,4 +33,4 @@ class PHPParser_Node_Stmt_UseUse extends PHPParser_Node_Stmt
$attributes $attributes
); );
} }
} }

View File

@@ -1226,7 +1226,7 @@ class PHPParser_Parser
} }
protected function yyn29($attributes) { protected function yyn29($attributes) {
throw new PHPParser_Error('__halt_compiler() can only be used from the outermost scope'); throw new PHPParser_Error('__HALT_COMPILER() can only be used from the outermost scope');
} }
protected function yyn30($attributes) { protected function yyn30($attributes) {

View File

@@ -22,8 +22,8 @@ Multiple final modifiers are not allowed on line 1
----- -----
<?php class A { abstract final a(); } <?php class A { abstract final a(); }
----- -----
Cannot use the final and abstract modifier at the same time on line 1 Cannot use the final modifier on an abstract class member on line 1
----- -----
<?php abstract final class A { } <?php abstract final class A { }
----- -----
Syntax error, unexpected T_FINAL, expecting T_CLASS on line 1 Syntax error, unexpected T_FINAL, expecting T_CLASS on line 1

View File

@@ -2,11 +2,11 @@ Invalid class name
----- -----
<?php class self {} <?php class self {}
----- -----
Cannot use "self" as class name as it is reserved on line 1 Cannot use 'self' as class name as it is reserved on line 1
----- -----
<?php class parent {} <?php class parent {}
----- -----
Cannot use "parent" as class name as it is reserved on line 1 Cannot use 'parent' as class name as it is reserved on line 1
----- -----
<?php class static {} <?php class static {}
----- -----
@@ -14,11 +14,11 @@ Syntax error, unexpected T_STATIC, expecting T_STRING on line 1
----- -----
<?php class A extends self {} <?php class A extends self {}
----- -----
Cannot use "self" as class name as it is reserved on line 1 Cannot use 'self' as class name as it is reserved on line 1
----- -----
<?php class A extends parent {} <?php class A extends parent {}
----- -----
Cannot use "parent" as class name as it is reserved on line 1 Cannot use 'parent' as class name as it is reserved on line 1
----- -----
<?php class A extends static {} <?php class A extends static {}
----- -----
@@ -26,11 +26,11 @@ Syntax error, unexpected T_STATIC, expecting T_STRING or T_NAMESPACE or T_NS_SEP
----- -----
<?php class A implements self {} <?php class A implements self {}
----- -----
Cannot use "self" as interface name as it is reserved on line 1 Cannot use 'self' as interface name as it is reserved on line 1
----- -----
<?php class A implements parent {} <?php class A implements parent {}
----- -----
Cannot use "parent" as interface name as it is reserved on line 1 Cannot use 'parent' as interface name as it is reserved on line 1
----- -----
<?php class A implements static {} <?php class A implements static {}
----- -----
@@ -38,11 +38,11 @@ Syntax error, unexpected T_STATIC, expecting T_STRING or T_NAMESPACE or T_NS_SEP
----- -----
<?php interface self {} <?php interface self {}
----- -----
Cannot use "self" as interface name as it is reserved on line 1 Cannot use 'self' as class name as it is reserved on line 1
----- -----
<?php interface parent {} <?php interface parent {}
----- -----
Cannot use "parent" as interface name as it is reserved on line 1 Cannot use 'parent' as class name as it is reserved on line 1
----- -----
<?php interface static {} <?php interface static {}
----- -----
@@ -50,12 +50,12 @@ Syntax error, unexpected T_STATIC, expecting T_STRING on line 1
----- -----
<?php interface A extends self {} <?php interface A extends self {}
----- -----
Cannot use "self" as interface name as it is reserved on line 1 Cannot use 'self' as interface name as it is reserved on line 1
----- -----
<?php interface A extends parent {} <?php interface A extends parent {}
----- -----
Cannot use "parent" as interface name as it is reserved on line 1 Cannot use 'parent' as interface name as it is reserved on line 1
----- -----
<?php interface A extends static {} <?php interface A extends static {}
----- -----
Syntax error, unexpected T_STATIC, expecting T_STRING or T_NAMESPACE or T_NS_SEPARATOR on line 1 Syntax error, unexpected T_STATIC, expecting T_STRING or T_NAMESPACE or T_NS_SEPARATOR on line 1

View File

@@ -2,12 +2,24 @@ Some special methods cannot be static
----- -----
<?php class A { static function __construct() {} } <?php class A { static function __construct() {} }
----- -----
"__construct" method cannot be static on line 1 Constructor __construct() cannot be static on line 1
----- -----
<?php class A { static function __destruct() {} } <?php class A { static function __destruct() {} }
----- -----
"__destruct" method cannot be static on line 1 Destructor __destruct() cannot be static on line 1
----- -----
<?php class A { static function __clone() {} } <?php class A { static function __clone() {} }
----- -----
"__clone" method cannot be static on line 1 Clone method __clone() cannot be static on line 1
-----
<?php class A { static function __CONSTRUCT() {} }
-----
Constructor __CONSTRUCT() cannot be static on line 1
-----
<?php class A { static function __Destruct() {} }
-----
Destructor __Destruct() cannot be static on line 1
-----
<?php class A { static function __cLoNe() {} }
-----
Clone method __cLoNe() cannot be static on line 1

View File

@@ -3,4 +3,4 @@ Invalid __halt_compiler() syntax
<?php <?php
__halt_compiler() __halt_compiler()
----- -----
__halt_compiler must be followed by "();" on line 2 __HALT_COMPILER must be followed by "();" on line 2

View File

@@ -5,4 +5,4 @@ if (true) {
__halt_compiler(); __halt_compiler();
} }
----- -----
__halt_compiler() can only be used from the outermost scope on line 3 __HALT_COMPILER() can only be used from the outermost scope on line 3

View File

@@ -2,11 +2,11 @@ Invalid namespace names
----- -----
<?php namespace self; <?php namespace self;
----- -----
Cannot use "self" as namespace name as it is reserved on line 1 Cannot use 'self' as namespace name on line 1
----- -----
<?php namespace parent; <?php namespace parent;
----- -----
Cannot use "parent" as namespace name as it is reserved on line 1 Cannot use 'parent' as namespace name on line 1
----- -----
<?php namespace static; <?php namespace static;
----- -----
@@ -14,12 +14,12 @@ Syntax error, unexpected T_STATIC, expecting T_STRING or T_NS_SEPARATOR or '{' o
----- -----
<?php use A as self; <?php use A as self;
----- -----
Cannot use "A" as "self" because "self" is a special class name on line 1 Cannot use A as self because 'self' is a special class name on line 1
----- -----
<?php use B as parent; <?php use B as parent;
----- -----
Cannot use "B" as "parent" because "parent" is a special class name on line 1 Cannot use B as parent because 'parent' is a special class name on line 1
----- -----
<?php use C as static; <?php use C as static;
----- -----
Syntax error, unexpected T_STATIC, expecting T_STRING on line 1 Syntax error, unexpected T_STATIC, expecting T_STRING on line 1