Changeset 900

Show
Ignore:
Timestamp:
09/11/07 17:36:21 (1 year ago)
Author:
mikey
Message:

added getReturnType() for stubReflectionFunction and stubReflectionMethod

Files:

Legend:

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

    r834 r900  
    115115        return $stubParameters; 
    116116    } 
     117 
     118    /** 
     119     * returns information about the return type of a function 
     120     *  
     121     * If the return type is a known class the return value is an instance of 
     122     * stubReflectionClass, if it is a scalar type or an unknown class the 
     123     * return value is a string and if the method does not have a return value 
     124     * this method returns null. 
     125     * Please be aware that this is guessing from the doc block with which the 
     126     * method is documented. If the doc block is missing or incorrect the return 
     127     * value of this method may be wrong. This is due to missing type hints for 
     128     * return values in PHP itself. 
     129     * 
     130     * @return  string|stubReflectionClass 
     131     */ 
     132    public function getReturnType() 
     133    { 
     134        $returnPart = strstr($this->docComment, '@return'); 
     135        if (false === $returnPart) { 
     136            return null; 
     137        } 
     138         
     139        $returnParts = explode(' ', trim(str_replace('@return', '', $returnPart))); 
     140        $returnType  = trim($returnParts[0]); 
     141        if (class_exists($returnType, false) === true) { 
     142            return new stubReflectionClass($returnType); 
     143        } 
     144         
     145        return $returnType; 
     146    } 
    117147} 
    118148?> 
  • trunk/src/main/php/net/stubbles/reflection/stubReflectionMethod.php

    r834 r900  
    136136        return $stubParameters; 
    137137    } 
     138 
     139    /** 
     140     * returns information about the return type of a method 
     141     *  
     142     * If the return type is a known class the return value is an instance of 
     143     * stubReflectionClass, if it is a scalar type or an unknown class the 
     144     * return value is a string and if the method does not have a return value 
     145     * this method returns null. 
     146     * Please be aware that this is guessing from the doc block with which the 
     147     * method is documented. If the doc block is missing or incorrect the return 
     148     * value of this method may be wrong. This is due to missing type hints for 
     149     * return values in PHP itself. 
     150     * 
     151     * @return  string|stubReflectionClass 
     152     */ 
     153    public function getReturnType() 
     154    { 
     155        $returnPart = strstr($this->docComment, '@return'); 
     156        if (false === $returnPart) { 
     157            return null; 
     158        } 
     159         
     160        $returnParts = explode(' ', trim(str_replace('@return', '', $returnPart))); 
     161        $returnType  = trim($returnParts[0]); 
     162        if (class_exists($returnType, false) === true) { 
     163            return new stubReflectionClass($returnType); 
     164        } 
     165         
     166        return $returnType; 
     167    } 
    138168} 
    139169?> 
  • trunk/src/test/php/net/stubbles/reflection/stubReflectionFunctionTestCase.php

    r697 r900  
    88 */ 
    99stubClassLoader::load('net.stubbles.reflection.stubReflectionFunction'); 
     10/** 
     11 * does not return anything 
     12 */ 
    1013function stubTestWithOutParams() {} 
     14/** 
     15 * returns a string 
     16 * 
     17 * @param   string $param1 
     18 * @param   mixed  $param2 
     19 * @return  string 
     20 */ 
    1121function stubTestWithParams($param1, $param2) {} 
     22function stubTestWithOutDocBlock() {} 
     23/** 
     24 * returns a class 
     25 * 
     26 * @return  stubReflectionFunctionTestCase 
     27 */ 
     28function stubTestWithClassReturnType() {} 
    1229/** 
    1330 * Test for stubReflectionFunction. 
     
    7996        $this->assertEqual(count($stubRefParameters), 0); 
    8097    } 
     98 
     99    /** 
     100     * test the return type hint 
     101     */ 
     102    public function testGetReturnType() 
     103    { 
     104        $this->assertNull($this->stubRefFunction2->getReturnType()); 
     105        $this->assertEqual($this->stubRefFunction1->getReturnType(), 'string'); 
     106        $refFunction3 = new stubReflectionFunction('stubTestWithOutDocBlock'); 
     107        $this->assertNull($refFunction3->getReturnType()); 
     108        $refFunction4 = new stubReflectionFunction('stubTestWithClassReturnType'); 
     109        $refClass = $refFunction4->getReturnType(); 
     110        $this->assertIsA($refClass, 'stubReflectionClass'); 
     111        $this->assertEqual($refClass->getName(), 'stubReflectionFunctionTestCase'); 
     112    } 
    81113} 
    82114?> 
  • trunk/src/test/php/net/stubbles/reflection/stubReflectionMethodTestCase.php

    r697 r900  
    1010class stubTest 
    1111{ 
     12    /** 
     13     * does not return anything 
     14     */ 
    1215    public function methodWithoutParams() 
    1316    { 
    1417        // nothing to to here 
    1518    } 
    16      
     19 
     20    /** 
     21     * returns a scalar value 
     22     * 
     23     * @param   string  $param1 
     24     * @param   mixed   $param2 
     25     * @return  string 
     26     */ 
    1727    public function methodWithParams($param1, $param2) 
    1828    { 
     
    2232class stubTest2 extends stubTest 
    2333{ 
     34    /** 
     35     * returns a class instance 
     36     * 
     37     * @param   int       $param3 
     38     * @return  stubTest 
     39     */ 
    2440    public function methodWithParams2($param3) 
    2541    { 
     
    185201        } 
    186202    } 
     203 
     204    /** 
     205     * test the return type hint 
     206     */ 
     207    public function testGetReturnType() 
     208    { 
     209        $this->assertNull($this->stubRefMethod1->getReturnType()); 
     210        $this->assertEqual($this->stubRefMethod2->getReturnType(), 'string'); 
     211        $this->assertNull($this->stubRefMethod3->getReturnType()); 
     212        $this->assertEqual($this->stubRefMethod4->getReturnType(), 'string'); 
     213        $refClass = $this->stubRefMethod5->getReturnType(); 
     214        $this->assertIsA($refClass, 'stubReflectionClass'); 
     215        $this->assertEqual($refClass->getName(), 'stubTest'); 
     216    } 
    187217} 
    188218?>