Changeset 1081

Show
Ignore:
Timestamp:
11/28/07 14:20:13 (7 months ago)
Author:
mikey
Message:

implemented refactoring #111 for net::stubbles::reflection::stubReflectionProperty

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/src/main/php/net/stubbles/reflection/stubReflectionProperty.php

    r1016 r1081  
    2121{ 
    2222    /** 
    23      * docbloc comment for this property 
    24      * 
    25      * @var  string 
    26      */ 
    27     protected $docComment; 
    28  
    29     /** 
    3023     * Name of the class 
    3124     * 
     
    3326     */ 
    3427    protected $className; 
    35  
     28    /** 
     29     * reflection instance for class declaring this property 
     30     * 
     31     * @var  stubBaseReflectionClass 
     32     */ 
     33    protected $refClass; 
    3634    /** 
    3735     * Name of the property 
     
    4442     * constructor 
    4543     * 
    46      * @param  string  $className     name of class to reflect 
    47      * @param  string  $propertyName  name of property to reflect 
     44     * @param  string|stubBaseReflectionClass  $class        name of class to reflect 
     45     * @param  string                          $propertyName  name of property to reflect 
    4846     */ 
    49     public function __construct($className, $propertyName) 
     47    public function __construct($class, $propertyName) 
    5048    { 
    51         parent::__construct($className, $propertyName); 
    52         $this->className = $className; 
     49        if ($class instanceof stubBaseReflectionClass) { 
     50            $this->refClass  = $class; 
     51            $this->className = $class->getName(); 
     52        } else { 
     53            $this->className = $class; 
     54        } 
     55         
    5356        $this->propertyName = $propertyName; 
    54         $this->docComment = $this->getDocComment(); 
     57        parent::__construct($this->className, $propertyName); 
    5558    } 
    5659 
     
    6366    public function hasAnnotation($annotationName) 
    6467    { 
    65         return stubAnnotationFactory::has($this->docComment, $annotationName, stubAnnotation::TARGET_PROPERTY, $this->className.'::'.$this->propertyName, $this->getDeclaringClass()->getFileName()); 
     68        return stubAnnotationFactory::has($this->getDocComment(), $annotationName, stubAnnotation::TARGET_PROPERTY, $this->className . '::' . $this->propertyName, $this->getDeclaringClass()->getFileName()); 
    6669    } 
    6770 
     
    7578    public function getAnnotation($annotationName) 
    7679    { 
    77         return stubAnnotationFactory::create($this->docComment, $annotationName, stubAnnotation::TARGET_PROPERTY, $this->className.'::'.$this->propertyName, $this->getDeclaringClass()->getFileName()); 
     80        return stubAnnotationFactory::create($this->getDocComment(), $annotationName, stubAnnotation::TARGET_PROPERTY, $this->className . '::' . $this->propertyName, $this->getDeclaringClass()->getFileName()); 
    7881    } 
    7982 
     
    114117     * returns the class that declares this parameter 
    115118     * 
    116      * @return  stubReflectionClass 
     119     * @return  stubBaseReflectionClass 
    117120     */ 
    118121    public function getDeclaringClass() 
    119122    { 
    120         $refClass     = parent::getDeclaringClass(); 
     123        $refClass = parent::getDeclaringClass(); 
     124        if ($refClass->getName() === $this->className) { 
     125            if (null === $this->refClass) { 
     126                $this->refClass = new stubReflectionClass($refClass->getName()); 
     127            } 
     128             
     129            return $this->refClass; 
     130        } 
     131         
    121132        $stubRefClass = new stubReflectionClass($refClass->getName()); 
    122133        return $stubRefClass; 
  • trunk/src/test/php/net/stubbles/reflection/stubReflectionPropertyTestCase.php

    r697 r1081  
    11<?php 
    22/** 
    3  * Test for stubReflectionProperty. 
     3 * Test for net::stubbles::reflection::stubReflectionProperty. 
    44 * 
    55 * @author      Frank Kleine <mikey@stubbles.net> 
     
    88 */ 
    99stubClassLoader::load('net.stubbles.reflection.stubReflectionProperty'); 
    10 class stubTestProperty 
     10/** 
     11 * class for testing purposes 
     12 * 
     13 * @package     stubbles 
     14 * @subpackage  reflection_test 
     15 */ 
     16class stubTestProperty1 
    1117{ 
    1218    public $property; 
     
    1420} 
    1521/** 
    16  * Test for stubReflectionProperty. 
     22 * another class for testing purposes 
     23 * 
     24 * @package     stubbles 
     25 * @subpackage  reflection_test 
     26 */ 
     27class stubTestProperty2 extends stubTestProperty1 { } 
     28/** 
     29 * Test for net::stubbles::reflection::stubReflectionProperty. 
    1730 * 
    1831 * @package     stubbles 
     
    3346    public function setUp() 
    3447    { 
    35         $this->stubRefProperty = new stubReflectionProperty('stubTestProperty', 'property'); 
     48        $this->stubRefProperty = new stubReflectionProperty('stubTestProperty1', 'property'); 
    3649    } 
    3750 
     
    4255    { 
    4356        $this->assertTrue($this->stubRefProperty->equals($this->stubRefProperty)); 
    44         $stubRefProperty1 = new stubReflectionProperty('stubTestProperty', 'property'); 
    45         $stubRefProperty2 = new stubReflectionProperty('stubTestProperty', 'anotherProperty'); 
     57        $stubRefProperty1 = new stubReflectionProperty('stubTestProperty1', 'property'); 
     58        $stubRefProperty2 = new stubReflectionProperty('stubTestProperty1', 'anotherProperty'); 
    4659        $this->assertTrue($this->stubRefProperty->equals($stubRefProperty1)); 
    4760        $this->assertTrue($stubRefProperty1->equals($this->stubRefProperty)); 
     
    5669    public function testToString() 
    5770    { 
    58         $this->assertEqual((string) $this->stubRefProperty, "net.stubbles.reflection.stubReflectionProperty[stubTestProperty::property] {\n}\n"); 
     71        $this->assertEqual((string) $this->stubRefProperty, "net.stubbles.reflection.stubReflectionProperty[stubTestProperty1::property] {\n}\n"); 
    5972    } 
    6073 
     
    6679        $declaringClass = $this->stubRefProperty->getDeclaringClass(); 
    6780        $this->assertIsA($declaringClass, 'stubReflectionClass'); 
    68         $this->assertEqual($declaringClass->getName(), 'stubTestProperty'); 
     81        $this->assertEqual($declaringClass->getName(), 'stubTestProperty1'); 
     82         
     83        $refClass1 = new stubReflectionClass('stubTestProperty1'); 
     84        $stubRefProperty1 = new stubReflectionProperty($refClass1, 'property'); 
     85        $this->assertReference($stubRefProperty1->getDeclaringClass(), $refClass1); 
     86         
     87        $refClass2 = new stubReflectionClass('stubTestProperty2'); 
     88        $stubRefProperty2 = new stubReflectionProperty($refClass2, 'property'); 
     89        $this->assertEqual($stubRefProperty2->getDeclaringClass()->getName(), 'stubTestProperty1');         
    6990    } 
    7091}