Add Scalar\MagicConst->getName()

Return magic constant name, e.g. __CLASS__.

Resolves #95.
This commit is contained in:
nikic 2014-02-21 18:16:18 +01:00
parent 1c8481bff6
commit 70077039b4
10 changed files with 56 additions and 0 deletions

View File

@ -14,4 +14,11 @@ abstract class MagicConst extends Scalar
public function __construct(array $attributes = array()) {
parent::__construct(array(), $attributes);
}
/**
* Get name of magic constant.
*
* @return string Name of magic constant
*/
abstract public function getName();
}

View File

@ -6,4 +6,7 @@ use PhpParser\Node\Scalar\MagicConst;
class Class_ extends MagicConst
{
public function getName() {
return '__CLASS__';
}
}

View File

@ -6,4 +6,7 @@ use PhpParser\Node\Scalar\MagicConst;
class Dir extends MagicConst
{
public function getName() {
return '__DIR__';
}
}

View File

@ -6,4 +6,7 @@ use PhpParser\Node\Scalar\MagicConst;
class File extends MagicConst
{
public function getName() {
return '__FILE__';
}
}

View File

@ -6,4 +6,7 @@ use PhpParser\Node\Scalar\MagicConst;
class Function_ extends MagicConst
{
public function getName() {
return '__FUNCTION__';
}
}

View File

@ -6,4 +6,7 @@ use PhpParser\Node\Scalar\MagicConst;
class Line extends MagicConst
{
public function getName() {
return '__LINE__';
}
}

View File

@ -6,4 +6,7 @@ use PhpParser\Node\Scalar\MagicConst;
class Method extends MagicConst
{
public function getName() {
return '__METHOD__';
}
}

View File

@ -6,4 +6,7 @@ use PhpParser\Node\Scalar\MagicConst;
class Namespace_ extends MagicConst
{
public function getName() {
return '__NAMESPACE__';
}
}

View File

@ -6,4 +6,7 @@ use PhpParser\Node\Scalar\MagicConst;
class Trait_ extends MagicConst
{
public function getName() {
return '__TRAIT__';
}
}

View File

@ -0,0 +1,25 @@
<?php
namespace PhpParser\Node\Scalar;
class MagicConstTest extends \PHPUnit_Framework_TestCase {
/**
* @dataProvider provideTestGetName
*/
public function testGetName(MagicConst $magicConst, $name) {
$this->assertSame($name, $magicConst->getName());
}
public function provideTestGetName() {
return array(
array(new MagicConst\Class_, '__CLASS__'),
array(new MagicConst\Dir, '__DIR__'),
array(new MagicConst\File, '__FILE__'),
array(new MagicConst\Function_, '__FUNCTION__'),
array(new MagicConst\Line, '__LINE__'),
array(new MagicConst\Method, '__METHOD__'),
array(new MagicConst\Namespace_, '__NAMESPACE__'),
array(new MagicConst\Trait_, '__TRAIT__'),
);
}
}