Skip to content

Commit

Permalink
ReflectionClass tested, documented.
Browse files Browse the repository at this point in the history
  • Loading branch information
KrisJordan committed Sep 7, 2009
1 parent 8fdc34e commit f6bcf99
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 5 deletions.
4 changes: 2 additions & 2 deletions recess/recess/lang/Object.class.php
Expand Up @@ -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();
}

/**
Expand Down
34 changes: 31 additions & 3 deletions recess/recess/lang/ReflectionClass.class.php
@@ -1,6 +1,10 @@
<?php
namespace recess\lang;

use recess\lang\Object;
use recess\lang\ReflectionMethod;
use recess\lang\ReflectionProperty;

/**
* Recess PHP Framework reflection for class which introduces annotations.
* Annotations follow the following syntax:
Expand All @@ -13,7 +17,6 @@
* This class is instantiated if it exists (else throws UnknownAnnotationException) and its init
* method is passed the value array following the annotation's name.
*
* @todo Harden the regular expressions.
*
* @author Kris Jordan <krisjordan@gmail.com>
* @copyright 2008, 2009 Kris Jordan
Expand All @@ -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();
Expand All @@ -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();
Expand All @@ -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();
Expand All @@ -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;
Expand Down
2 changes: 2 additions & 0 deletions recess/test/recess/lang/RecessLangAllTests.php
Expand Up @@ -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
Expand All @@ -13,6 +14,7 @@ public static function suite()

$suite->addTestSuite('AnnotationTest');
$suite->addTestSuite('AttachedMethodTest');
$suite->addTestSuite('ReflectionClassTest');
$suite->addTestSuite('ReflectionMethodTest');
$suite->addTestSuite('ObjectTest');

Expand Down
50 changes: 50 additions & 0 deletions recess/test/recess/lang/ReflectionClassTest.php
@@ -0,0 +1,50 @@
<?php
use recess\lang\ReflectionClass;
use recess\lang\ReflectionProperty;
use recess\lang\Object;

require_once __DIR__ . '/DummyAnnotation.class.php';
use made\up\space\DummyAnnotation;

class ReflectionClassTest extends PHPUnit_Framework_TestCase {

function testGetAnnotations() {
DummyAnnotation::load();
$klass = new ReflectionClass('ReflectionClassObject');
$annotations = $klass->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;
}

0 comments on commit f6bcf99

Please sign in to comment.