From 9dc93aafaab90eb885a59412237b323047c1819a Mon Sep 17 00:00:00 2001 From: Andreev Sergey Date: Sat, 22 Apr 2017 19:37:53 +0700 Subject: [PATCH] Add ClassMethod::isMagic() method --- lib/PhpParser/Node/Stmt/ClassMethod.php | 27 +++++++++++++++++ test/PhpParser/Node/Stmt/ClassMethodTest.php | 31 ++++++++++++++++++++ 2 files changed, 58 insertions(+) diff --git a/lib/PhpParser/Node/Stmt/ClassMethod.php b/lib/PhpParser/Node/Stmt/ClassMethod.php index 1043e77..981a354 100644 --- a/lib/PhpParser/Node/Stmt/ClassMethod.php +++ b/lib/PhpParser/Node/Stmt/ClassMethod.php @@ -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)]); + } } diff --git a/test/PhpParser/Node/Stmt/ClassMethodTest.php b/test/PhpParser/Node/Stmt/ClassMethodTest.php index fa8aed8..e472c7e 100644 --- a/test/PhpParser/Node/Stmt/ClassMethodTest.php +++ b/test/PhpParser/Node/Stmt/ClassMethodTest.php @@ -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'), + ); + } }