From f6bcf99035dda5e95b872c3ada1c4c61ac536ab0 Mon Sep 17 00:00:00 2001 From: KrisJordan Date: Mon, 7 Sep 2009 18:11:39 -0400 Subject: [PATCH] ReflectionClass tested, documented. --- recess/recess/lang/Object.class.php | 4 +- recess/recess/lang/ReflectionClass.class.php | 34 +++++++++++-- .../test/recess/lang/RecessLangAllTests.php | 2 + .../test/recess/lang/ReflectionClassTest.php | 50 +++++++++++++++++++ 4 files changed, 85 insertions(+), 5 deletions(-) create mode 100644 recess/test/recess/lang/ReflectionClassTest.php diff --git a/recess/recess/lang/Object.class.php b/recess/recess/lang/Object.class.php index 683395a..a71e371 100644 --- a/recess/recess/lang/Object.class.php +++ b/recess/recess/lang/Object.class.php @@ -154,8 +154,8 @@ final static function getClassDescriptor($classNameOrInstance = false) { * @param variant $classNameOrInstance - String class name or instance of a Recess Class * @return array */ - final static function getAttachedMethods() { - return static::getClassDescriptor(get_called_class())->getAttachedMethods(); + final static function getAttachedMethods($classname = false) { + return static::getClassDescriptor($classname ?: get_called_class())->getAttachedMethods(); } /** diff --git a/recess/recess/lang/ReflectionClass.class.php b/recess/recess/lang/ReflectionClass.class.php index 00e3f7d..d39a49c 100644 --- a/recess/recess/lang/ReflectionClass.class.php +++ b/recess/recess/lang/ReflectionClass.class.php @@ -1,6 +1,10 @@ * @copyright 2008, 2009 Kris Jordan @@ -22,6 +25,15 @@ * @link http://www.recessframework.org/ */ class ReflectionClass extends \ReflectionClass { + + /** + * Returns an array of ReflectionProperties. Optional parameter + * $getAttachedParams (defaults to true) specifies whether or not + * to include dynamically attached methods in the return value. + * + * @param $filter + * @return array of ReflectionProperty's + */ function getProperties($filter = null) { $rawProperties = parent::getProperties(); $properties = array(); @@ -30,6 +42,15 @@ function getProperties($filter = null) { } return $properties; } + + /** + * Returns an array of ReflectionMethods. Optional parameter + * $getAttachedParams (defaults to true) specifies whether or not + * to include dynamically attached methods in the return value. + * + * @param bool $getAttachedMethods Return dynamically attached methods? + * @return array of ReflectionMethod's + */ function getMethods($getAttachedMethods = true){ $rawMethods = parent::getMethods(); $methods = array(); @@ -38,12 +59,19 @@ function getMethods($getAttachedMethods = true){ $methods[] = $method; } - if($getAttachedMethods && is_subclass_of($this->name, 'Object')) { + if($getAttachedMethods && is_subclass_of($this->name, 'recess\lang\Object')) { $methods = array_merge($methods, Object::getAttachedMethods($this->name)); } return $methods; } + + /** + * Returns an array of parsed annotations. Will throw an ErrorException + * if annotations cannot be parsed or have not been loaded. + * + * @return array of Annotations + */ function getAnnotations() { $docstring = $this->getDocComment(); if($docstring == '') return array(); @@ -52,7 +80,7 @@ function getAnnotations() { try { $returns = Annotation::parse($docstring); } catch(\Exception $e) { - throw new ErrorException('In class "' . $this->name . '".' . $e->getMessage(),0,0,$this->getFileName(),$this->getStartLine(),array()); + throw new \ErrorException('In class "' . $this->name . '".' . $e->getMessage(),0,0,$this->getFileName(),$this->getStartLine()); } } return $returns; diff --git a/recess/test/recess/lang/RecessLangAllTests.php b/recess/test/recess/lang/RecessLangAllTests.php index 67f691b..1970c86 100644 --- a/recess/test/recess/lang/RecessLangAllTests.php +++ b/recess/test/recess/lang/RecessLangAllTests.php @@ -3,6 +3,7 @@ require_once 'recess/lang/AnnotationTest.php'; require_once 'recess/lang/AttachedMethodTest.php'; require_once 'recess/lang/ReflectionMethodTest.php'; +require_once 'recess/lang/ReflectionClassTest.php'; require_once 'recess/lang/ObjectTest.php'; class RecessLangAllTests @@ -13,6 +14,7 @@ public static function suite() $suite->addTestSuite('AnnotationTest'); $suite->addTestSuite('AttachedMethodTest'); + $suite->addTestSuite('ReflectionClassTest'); $suite->addTestSuite('ReflectionMethodTest'); $suite->addTestSuite('ObjectTest'); diff --git a/recess/test/recess/lang/ReflectionClassTest.php b/recess/test/recess/lang/ReflectionClassTest.php new file mode 100644 index 0000000..c7c25aa --- /dev/null +++ b/recess/test/recess/lang/ReflectionClassTest.php @@ -0,0 +1,50 @@ +getAnnotations(); + $this->assertEquals(1, count($annotations)); + $this->assertEquals(array(new DummyAnnotation), $annotations); + } + + function testGetProperties() { + DummyAnnotation::load(); + $childClass = new ReflectionClass('ReflectionClassObject'); + $parentClass = new ReflectionClass('recess\lang\Object'); + $this->assertEquals(1, count($childClass->getProperties())-count($parentClass->getProperties())); + } + + function testGetMethods() { + DummyAnnotation::load(); + $childClass = new ReflectionClass('ReflectionClassObject'); + $parentClass = new ReflectionClass('recess\lang\Object'); + $this->assertEquals(1, count($childClass->getMethods())-count($parentClass->getMethods())); + } + + function testGetAttachedMethods() { + DummyAnnotation::load(); + ReflectionClassObject::attachMethod('aMethod',function($self){return true;}); + $childClass = new ReflectionClass('ReflectionClassObject'); + $parentClass = new ReflectionClass('recess\lang\Object'); + $this->assertEquals(2, count($childClass->getMethods())-count($parentClass->getMethods())); + } + +} + +/** !Dummy */ +class ReflectionClassObject extends Object { + /** !Dummy */ + function fooMethod() {} + + /** !Dummy */ + public $fooProperty; +} \ No newline at end of file