Drop support for PHP 7 aliases

This commit is contained in:
Nikita Popov 2015-05-02 22:21:12 +02:00
parent 5f621c5adc
commit cc75dd3612
2 changed files with 0 additions and 50 deletions

View File

@ -10,9 +10,6 @@ class Autoloader
/** @var bool Whether the autoloader has been registered. */
private static $registered = false;
/** @var bool Whether we're running on PHP 7. */
private static $runningOnPhp7;
/**
* Registers PhpParser\Autoloader as an SPL autoloader.
*
@ -25,7 +22,6 @@ class Autoloader
spl_autoload_register(array(__CLASS__, 'autoload'), true, $prepend);
self::$registered = true;
self::$runningOnPhp7 = version_compare(PHP_VERSION, '7.0-dev', '>=');
}
/**
@ -35,43 +31,10 @@ class Autoloader
*/
static public function autoload($class) {
if (0 === strpos($class, 'PhpParser\\')) {
if (isset(self::$php7AliasesOldToNew[$class])) {
if (self::$runningOnPhp7) {
return;
}
// Load the new class, alias will be registered afterwards
$class = self::$php7AliasesOldToNew[$class];
}
$fileName = dirname(__DIR__) . '/' . strtr($class, '\\', '/') . '.php';
if (file_exists($fileName)) {
require $fileName;
}
if (isset(self::$php7AliasesNewToOld[$class])) {
// New class name was used, register alias for old one, otherwise
// it won't be usable in "instanceof" and other non-autoloading places.
if (!self::$runningOnPhp7) {
class_alias($class, self::$php7AliasesNewToOld[$class]);
}
}
}
}
private static $php7AliasesOldToNew = array(
'PhpParser\Node\Expr\Cast\Bool' => 'PhpParser\Node\Expr\Cast\Bool_',
'PhpParser\Node\Expr\Cast\Int' => 'PhpParser\Node\Expr\Cast\Int_',
'PhpParser\Node\Expr\Cast\Object' => 'PhpParser\Node\Expr\Cast\Object_',
'PhpParser\Node\Expr\Cast\String' => 'PhpParser\Node\Expr\Cast\String_',
'PhpParser\Node\Scalar\String' => 'PhpParser\Node\Scalar\String_',
);
private static $php7AliasesNewToOld = array(
'PhpParser\Node\Expr\Cast\Bool_' => 'PhpParser\Node\Expr\Cast\Bool',
'PhpParser\Node\Expr\Cast\Int_' => 'PhpParser\Node\Expr\Cast\Int',
'PhpParser\Node\Expr\Cast\Object_' => 'PhpParser\Node\Expr\Cast\Object',
'PhpParser\Node\Expr\Cast\String_' => 'PhpParser\Node\Expr\Cast\String',
'PhpParser\Node\Scalar\String_' => 'PhpParser\Node\Scalar\String',
);
}

View File

@ -8,19 +8,6 @@ use PhpParser\Node\Scalar;
/* The autoloader is already active at this point, so we only check effects here. */
class AutoloaderTest extends \PHPUnit_Framework_TestCase {
public function testPhp7ReservedNames() {
if (version_compare(PHP_VERSION, '7.0-dev', '>=')) {
$this->markTestSkipped('Cannot create aliases to reserved names on PHP 7');
}
$this->assertTrue(new Expr\Cast\Bool_(new Expr\Variable('foo')) instanceof Expr\Cast\Bool);
$this->assertTrue(new Expr\Cast\Int_(new Expr\Variable('foo')) instanceof Expr\Cast\Int);
$this->assertInstanceof('PhpParser\Node\Expr\Cast\Object_', new Expr\Cast\Object(new Expr\Variable('foo')));
$this->assertInstanceof('PhpParser\Node\Expr\Cast\String_', new Expr\Cast\String(new Expr\Variable('foo')));
$this->assertInstanceof('PhpParser\Node\Scalar\String_', new Scalar\String('foobar'));
}
public function testClassExists() {
$this->assertTrue(class_exists('PhpParser\NodeVisitorAbstract'));
$this->assertFalse(class_exists('PHPParser_NodeVisitor_NameResolver'));