1
0
mirror of https://github.com/danog/PHP-Parser.git synced 2025-01-22 05:41:23 +01:00

Add ClassMethod::isMagic() method

This commit is contained in:
Andreev Sergey 2017-04-22 19:37:53 +07:00 committed by Nikita Popov
parent ba85da88a0
commit 9dc93aafaa
2 changed files with 58 additions and 0 deletions

View File

@ -20,6 +20,24 @@ class ClassMethod extends Node\Stmt implements FunctionLike
/** @var Node\Stmt[] Statements */
public $stmts;
private static $magicNames = array(
'__construct' => true,
'__destruct' => true,
'__call' => true,
'__callstatic' => true,
'__get' => true,
'__set' => true,
'__isset' => true,
'__unset' => true,
'__sleep' => true,
'__wakeup' => true,
'__tostring' => true,
'__set_state' => true,
'__clone' => true,
'__invoke' => true,
'__debuginfo' => true,
);
/**
* Constructs a class method node.
*
@ -117,4 +135,13 @@ class ClassMethod extends Node\Stmt implements FunctionLike
public function isStatic() {
return (bool) ($this->flags & Class_::MODIFIER_STATIC);
}
/**
* Whether the method is magic.
*
* @return bool
*/
public function isMagic() {
return isset(self::$magicNames[strtolower($this->name)]);
}
}

View File

@ -24,6 +24,7 @@ class ClassMethodTest extends \PHPUnit_Framework_TestCase
$this->assertFalse($node->isAbstract());
$this->assertFalse($node->isFinal());
$this->assertFalse($node->isStatic());
$this->assertFalse($node->isMagic());
}
public function provideModifiers() {
@ -60,4 +61,34 @@ class ClassMethodTest extends \PHPUnit_Framework_TestCase
array('static'),
);
}
/**
* @dataProvider provideMagics
*
* @param string $name Node name
*/
public function testMagic($name) {
$node = new ClassMethod($name);
$this->assertTrue($node->isMagic(), 'Method should be magic');
}
public function provideMagics() {
return array(
array('__construct'),
array('__DESTRUCT'),
array('__caLL'),
array('__callstatic'),
array('__get'),
array('__set'),
array('__isset'),
array('__unset'),
array('__sleep'),
array('__wakeup'),
array('__tostring'),
array('__set_state'),
array('__clone'),
array('__invoke'),
array('__debuginfo'),
);
}
}