Changeset 900
- Timestamp:
- 09/11/07 17:36:21 (1 year ago)
- Files:
-
- trunk/src/main/php/net/stubbles/reflection/stubReflectionFunction.php (modified) (1 diff)
- trunk/src/main/php/net/stubbles/reflection/stubReflectionMethod.php (modified) (1 diff)
- trunk/src/test/php/net/stubbles/reflection/stubReflectionFunctionTestCase.php (modified) (2 diffs)
- trunk/src/test/php/net/stubbles/reflection/stubReflectionMethodTestCase.php (modified) (3 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/src/main/php/net/stubbles/reflection/stubReflectionFunction.php
r834 r900 115 115 return $stubParameters; 116 116 } 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 } 117 147 } 118 148 ?> trunk/src/main/php/net/stubbles/reflection/stubReflectionMethod.php
r834 r900 136 136 return $stubParameters; 137 137 } 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 } 138 168 } 139 169 ?> trunk/src/test/php/net/stubbles/reflection/stubReflectionFunctionTestCase.php
r697 r900 8 8 */ 9 9 stubClassLoader::load('net.stubbles.reflection.stubReflectionFunction'); 10 /** 11 * does not return anything 12 */ 10 13 function stubTestWithOutParams() {} 14 /** 15 * returns a string 16 * 17 * @param string $param1 18 * @param mixed $param2 19 * @return string 20 */ 11 21 function stubTestWithParams($param1, $param2) {} 22 function stubTestWithOutDocBlock() {} 23 /** 24 * returns a class 25 * 26 * @return stubReflectionFunctionTestCase 27 */ 28 function stubTestWithClassReturnType() {} 12 29 /** 13 30 * Test for stubReflectionFunction. … … 79 96 $this->assertEqual(count($stubRefParameters), 0); 80 97 } 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 } 81 113 } 82 114 ?> trunk/src/test/php/net/stubbles/reflection/stubReflectionMethodTestCase.php
r697 r900 10 10 class stubTest 11 11 { 12 /** 13 * does not return anything 14 */ 12 15 public function methodWithoutParams() 13 16 { 14 17 // nothing to to here 15 18 } 16 19 20 /** 21 * returns a scalar value 22 * 23 * @param string $param1 24 * @param mixed $param2 25 * @return string 26 */ 17 27 public function methodWithParams($param1, $param2) 18 28 { … … 22 32 class stubTest2 extends stubTest 23 33 { 34 /** 35 * returns a class instance 36 * 37 * @param int $param3 38 * @return stubTest 39 */ 24 40 public function methodWithParams2($param3) 25 41 { … … 185 201 } 186 202 } 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 } 187 217 } 188 218 ?>
