Changeset 1080

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

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

Files:

Legend:

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

    r1072 r1080  
    2121{ 
    2222    /** 
    23      * name of reflected function 
     23     * name of reflected routine 
    2424     * 
    2525     * @var  string 
    2626     */ 
    27     protected $functionName; 
     27    protected $routineName; 
     28    /** 
     29     * reflection instance of routine containing this parameter 
     30     * 
     31     * @var  stubReflectionRoutine 
     32     */ 
     33    protected $refRoutine; 
    2834    /** 
    2935     * name of reflected parameter 
     
    3238     */ 
    3339    protected $paramName; 
    34     /** 
    35      * doc block of method or function where this param belongs to 
    36      * 
    37      * @var  string 
    38      */ 
    39     protected $docComment; 
    4040 
    4141    /** 
    4242     * constructor 
    4343     * 
    44      * @param string  $functionName  name of function to reflect 
    45      * @param string  $paramName     name of parameter to reflect 
    46      */ 
    47     public function __construct($functionName, $paramName) 
    48     { 
    49         $this->functionName = $functionName; 
    50         $this->paramName    = $paramName; 
    51         parent::__construct($functionName, $paramName); 
     44     * @param  string  $routine    name of function to reflect 
     45     * @param  string  $paramName  name of parameter to reflect 
     46     */ 
     47    public function __construct($routine, $paramName) 
     48    { 
     49        if ($routine instanceof stubReflectionMethod) { 
     50            $this->refRoutine  = $routine; 
     51            $this->routineName = array($routine->getDeclaringClass()->getName(), $routine->getName()); 
     52        } elseif ($routine instanceof stubReflectionFunction) { 
     53            $this->refRoutine  = $routine; 
     54            $this->routineName = $routine->getName(); 
     55        } else { 
     56            $this->routineName = $routine; 
     57        } 
     58         
     59        $this->paramName = $paramName; 
     60        parent::__construct($this->routineName, $paramName); 
    5261    } 
    5362 
     
    6069    public function hasAnnotation($annotationName) 
    6170    { 
    62         if (is_array($this->functionName) === true) { 
    63             $targetName = $this->functionName[0] . '::' . $this->functionName[1] . '()'; 
    64             $ref        = new stubReflectionMethod($this->functionName[0], $this->functionName[1]); 
    65         } else { 
    66             $targetName = $this->functionName; 
    67             $ref        = new stubReflectionFunction($this->functionName); 
    68         } 
    69          
    70         return stubAnnotationFactory::has($ref->getDocComment(), $annotationName . '#' . $this->paramName, stubAnnotation::TARGET_PARAM, $targetName, $ref->getFileName()); 
     71        $refRoutine = $this->getRefRoutine(); 
     72        $targetName = ((is_array($this->routineName) === true) ? ($this->routineName[0] . '::' . $this->routineName[1] . '()') : ($this->routineName)); 
     73        return stubAnnotationFactory::has($refRoutine->getDocComment(), $annotationName . '#' . $this->paramName, stubAnnotation::TARGET_PARAM, $targetName, $refRoutine->getFileName()); 
    7174    } 
    7275 
     
    8083    public function getAnnotation($annotationName) 
    8184    { 
    82         if (is_array($this->functionName) === true) { 
    83             $targetName = $this->functionName[0] . '::' . $this->functionName[1] . '()'; 
    84             $ref        = new stubReflectionMethod($this->functionName[0], $this->functionName[1]); 
    85         } else { 
    86             $targetName = $this->functionName; 
    87             $ref        = new stubReflectionFunction($this->functionName); 
    88         } 
    89          
    90         return stubAnnotationFactory::create($ref->getDocComment(), $annotationName . '#' . $this->paramName, stubAnnotation::TARGET_PARAM, $targetName, $ref->getFileName()); 
     85        $refRoutine = $this->getRefRoutine(); 
     86        $targetName = ((is_array($this->routineName) === true) ? ($this->routineName[0] . '::' . $this->routineName[1] . '()') : ($this->routineName)); 
     87        return stubAnnotationFactory::create($refRoutine->getDocComment(), $annotationName . '#' . $this->paramName, stubAnnotation::TARGET_PARAM, $targetName, $refRoutine->getFileName()); 
     88    } 
     89 
     90    /** 
     91     * helper method to return the reflection routine defining this parameter 
     92     * 
     93     * @return  stubReflectionRoutine 
     94     * @todo    replace by getDeclaringFunction() as soon as Stubbles requires at least PHP 5.2.3 
     95     */ 
     96    protected function getRefRoutine() 
     97    { 
     98        if (null === $this->refRoutine) { 
     99            if (is_array($this->routineName) === true) { 
     100                $this->refRoutine = new stubReflectionMethod($this->routineName[0], $this->routineName[1]); 
     101            } else { 
     102                $this->refRoutine = new stubReflectionFunction($this->routineName); 
     103            } 
     104        } 
     105         
     106        return $this->refRoutine; 
    91107    } 
    92108 
     
    110126         
    111127        if (null == $class) { 
    112             return ($compare->functionName == $this->functionName && $compare->paramName == $this->paramName); 
    113         } 
    114          
    115         return ($compareClass->getName() == $class->getName() && $compare->functionName == $this->functionName && $compare->paramName == $this->paramName); 
     128            return ($compare->routineName == $this->routineName && $compare->paramName == $this->paramName); 
     129        } 
     130         
     131        return ($compareClass->getName() == $class->getName() && $compare->routineName == $this->routineName && $compare->paramName == $this->paramName); 
    116132    } 
    117133 
     
    133149    public function __toString() 
    134150    { 
    135         if (is_array($this->functionName) == false) { 
    136             return 'net.stubbles.reflection.stubReflectionParameter[' . $this->functionName . '(): Argument ' . $this->paramName . "] {\n}\n"; 
    137         } 
    138          
    139         return 'net.stubbles.reflection.stubReflectionParameter[' . $this->functionName[0] . '::' . $this->functionName[1] . '(): Argument ' . $this->paramName . "] {\n}\n"; 
     151        if (is_array($this->routineName) == false) { 
     152            return 'net.stubbles.reflection.stubReflectionParameter[' . $this->routineName . '(): Argument ' . $this->paramName . "] {\n}\n"; 
     153        } 
     154         
     155        return 'net.stubbles.reflection.stubReflectionParameter[' . $this->routineName[0] . '::' . $this->routineName[1] . '(): Argument ' . $this->paramName . "] {\n}\n"; 
    140156    } 
    141157 
     
    162178    public function getDeclaringClass() 
    163179    { 
    164         $refClass = parent::getDeclaringClass(); 
    165         if (null === $refClass) { 
     180        if (is_array($this->routineName) === false) { 
    166181            return null; 
    167182        } 
    168183         
     184        $refClass     = parent::getDeclaringClass(); 
    169185        $stubRefClass = new stubReflectionClass($refClass->getName()); 
    170186        return $stubRefClass; 
  • trunk/src/test/php/net/stubbles/reflection/stubReflectionParameterTestCase.php

    r1072 r1080  
    11<?php 
    22/** 
    3  * Test for stubReflectionParameter. 
     3 * Test for net::stubbles::reflection::stubReflectionParameter. 
    44 * 
    55 * @author      Frank Kleine <mikey@stubbles.net> 
     
    8888} 
    8989/** 
    90  * Test for stubReflectionParameter. 
     90 * Test for net::stubbles::reflection::stubReflectionParameter. 
    9191 * 
    9292 * @package     stubbles 
     
    245245        $this->assertIsA($refClass, 'stubReflectionClass'); 
    246246        $this->assertEqual($refClass->getName(), 'stubParamTest'); 
    247        # currently not possible, see http://bugs.php.net/bug.php?id=39884  
    248        # $refClass = $this->stubRefParamMethod4->getClass(); 
    249        # $this->assertIsA($refClass, 'stubReflectionClass'); 
    250        # $this->assertEqual($refClass->getName(), 'stubParamTest2'); 
     247        $refClass = $this->stubRefParamMethod4->getClass(); 
     248        $this->assertIsA($refClass, 'stubReflectionClass'); 
     249        $this->assertEqual($refClass->getName(), 'stubParamTest2'); 
    251250    } 
    252251}