Browse Source

Disabling declaration of class which name conflicts with import.

PHAR_1_2
Dmitry Stogov 19 years ago
parent
commit
c4b5d9e572
  1. 6
      Zend/tests/ns_022.inc
  2. 10
      Zend/tests/ns_022.phpt
  3. 24
      Zend/tests/ns_025.phpt
  4. 30
      Zend/tests/ns_026.phpt
  5. 11
      Zend/tests/ns_027.inc
  6. 24
      Zend/tests/ns_027.phpt
  7. 15
      Zend/tests/ns_028.inc
  8. 26
      Zend/tests/ns_028.phpt
  9. 12
      Zend/tests/ns_029.phpt
  10. 12
      Zend/tests/ns_030.phpt
  11. 9
      Zend/zend_compile.c

6
Zend/tests/ns_022.inc

@ -0,0 +1,6 @@
<?php
class Test {
static function foo() {
echo __CLASS__,"::",__FUNCTION__,"\n";
}
}

10
Zend/tests/ns_022.phpt

@ -6,18 +6,14 @@ namespace a::b::c;
import a::b::c as test;
class Test {
static function foo() {
echo __CLASS__,"::",__FUNCTION__,"\n";
}
}
require "ns_022.inc";
function foo() {
echo __FUNCTION__,"\n";
}
test::foo();
test::test::foo();
::test::foo();
--EXPECT--
a::b::c::foo
a::b::c::Test::foo
Test::foo

24
Zend/tests/ns_025.phpt

@ -0,0 +1,24 @@
--TEST--
025: Name ambiguity (class name & part of namespace name)
--FILE--
<?php
namespace Foo::Bar;
class Foo {
function __construct() {
echo __CLASS__,"\n";
}
static function Bar() {
echo __CLASS__,"\n";
}
}
$x = new Foo;
Foo::Bar();
$x = new Foo::Bar::Foo;
Foo::Bar::Foo::Bar();
--EXPECT--
Foo::Bar::Foo
Foo::Bar::Foo
Foo::Bar::Foo
Foo::Bar::Foo

30
Zend/tests/ns_026.phpt

@ -0,0 +1,30 @@
--TEST--
026: Name ambiguity (class name & namespace name)
--FILE--
<?php
namespace Foo;
class Foo {
function __construct() {
echo "Method - ".__CLASS__."::".__FUNCTION__."\n";
}
static function Bar() {
echo "Method - ".__CLASS__."::".__FUNCTION__."\n";
}
}
function Bar() {
echo "Func - ".__FUNCTION__."\n";
}
$x = new Foo;
Foo::Bar();
$x = new Foo::Foo;
Foo::Foo::Bar();
::Foo::Bar();
--EXPECT--
Method - Foo::Foo::__construct
Method - Foo::Foo::Bar
Method - Foo::Foo::__construct
Method - Foo::Foo::Bar
Func - Foo::Bar

11
Zend/tests/ns_027.inc

@ -0,0 +1,11 @@
<?php
namespace Foo::Bar;
class Foo {
function __construct() {
echo __CLASS__,"\n";
}
static function Bar() {
echo __CLASS__,"\n";
}
}

24
Zend/tests/ns_027.phpt

@ -0,0 +1,24 @@
--TEST--
027: Name ambiguity (class name & part of extertnal namespace name)
--FILE--
<?php
require "ns_027.inc";
class Foo {
function __construct() {
echo __CLASS__,"\n";
}
static function Bar() {
echo __CLASS__,"\n";
}
}
$x = new Foo;
Foo::Bar();
$x = new Foo::Bar::Foo;
Foo::Bar::Foo::Bar();
--EXPECT--
Foo
Foo
Foo::Bar::Foo
Foo::Bar::Foo

15
Zend/tests/ns_028.inc

@ -0,0 +1,15 @@
<?php
namespace Foo;
class Foo {
function __construct() {
echo "Method - ".__CLASS__."::".__FUNCTION__."\n";
}
static function Bar() {
echo "Method - ".__CLASS__."::".__FUNCTION__."\n";
}
}
function Bar() {
echo "Func - ".__FUNCTION__."\n";
}

26
Zend/tests/ns_028.phpt

@ -0,0 +1,26 @@
--TEST--
028: Name ambiguity (class name & external namespace name)
--FILE--
<?php
require "ns_028.inc";
class Foo {
function __construct() {
echo "Method - ".__CLASS__."::".__FUNCTION__."\n";
}
static function Bar() {
echo "Method - ".__CLASS__."::".__FUNCTION__."\n";
}
}
$x = new Foo;
Foo::Bar();
$x = new Foo::Foo;
Foo::Foo::Bar();
::Foo::Bar();
--EXPECT--
Method - Foo::__construct
Func - Foo::Bar
Method - Foo::Foo::__construct
Method - Foo::Foo::Bar
Func - Foo::Bar

12
Zend/tests/ns_029.phpt

@ -0,0 +1,12 @@
--TEST--
029: Name ambiguity (class name & import name)
--FILE--
<?php
import A::B as Foo;
class Foo {
}
new Foo();
--EXPECTF--
Fatal error: Class name 'Foo' coflicts with import name in %sns_029.php on line 4

12
Zend/tests/ns_030.phpt

@ -0,0 +1,12 @@
--TEST--
030: Name ambiguity (import name & class name)
--FILE--
<?php
class Foo {
}
import A::B as Foo;
new Foo();
--EXPECTF--
Fatal error: Import name 'Foo' coflicts with defined class in %sns_030.php on line 5

9
Zend/zend_compile.c

@ -3110,6 +3110,11 @@ void zend_do_begin_class_declaration(znode *class_token, znode *class_name, znod
zend_error(E_COMPILE_ERROR, "Cannot use '%R' as class name as it is reserved", Z_TYPE(class_name->u.constant), Z_UNIVAL(class_name->u.constant));
}
if (CG(current_import) &&
zend_u_hash_exists(CG(current_import), Z_TYPE(class_name->u.constant), lcname, lcname_len+1)) {
zend_error(E_COMPILE_ERROR, "Class name '%R' coflicts with import name", Z_TYPE(class_name->u.constant), Z_UNIVAL(class_name->u.constant));
}
if (CG(current_namespace)) {
znode tmp;
@ -4945,6 +4950,10 @@ void zend_do_import(znode *ns_name, znode *new_name TSRMLS_DC) /* {{{ */
zend_error(E_COMPILE_ERROR, "Cannot use '%R' as import name", Z_TYPE_P(name), Z_UNIVAL_P(name));
}
if (zend_u_hash_exists(CG(class_table), Z_TYPE_P(name), lcname, lcname_len+1)) {
zend_error(E_COMPILE_ERROR, "Import name '%R' coflicts with defined class", Z_TYPE_P(name), Z_UNIVAL_P(name));
}
if (zend_u_hash_add(CG(current_import), Z_TYPE_P(name), lcname, lcname_len+1, &ns, sizeof(zval*), NULL) != SUCCESS) {
zend_error(E_COMPILE_ERROR, "Cannot reuse import name");
}

Loading…
Cancel
Save