Skip to content

Commit 7acc2ee

Browse files
committed
Adds return type to function
1 parent 1e81c2e commit 7acc2ee

File tree

3 files changed

+65
-3
lines changed

3 files changed

+65
-3
lines changed

src/phpDocumentor/Reflection/Php/Factory/Function_.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
use phpDocumentor\Reflection\Php\Function_ as FunctionDescriptor;
2020
use phpDocumentor\Reflection\Php\ProjectFactoryStrategy;
2121
use phpDocumentor\Reflection\Php\StrategyContainer;
22+
use phpDocumentor\Reflection\TypeResolver;
2223
use phpDocumentor\Reflection\Types\Context;
2324
use PhpParser\Comment\Doc;
2425
use PhpParser\Node\Stmt\Function_ as FunctionNode;
@@ -57,7 +58,13 @@ protected function doCreate($object, StrategyContainer $strategies, Context $con
5758
{
5859
$docBlock = $this->createDocBlock($strategies, $object->getDocComment(), $context);
5960

60-
$function = new FunctionDescriptor($object->fqsen, $docBlock, new Location($object->getLine()));
61+
$returnType = null;
62+
if ($object->returnType !== null) {
63+
$typeResolver = new TypeResolver();
64+
$returnType = $typeResolver->resolve($object->returnType, $context);
65+
}
66+
67+
$function = new FunctionDescriptor($object->fqsen, $docBlock, new Location($object->getLine()), $returnType);
6168

6269
foreach ($object->params as $param) {
6370
$strategy = $strategies->findMatching($param);

src/phpDocumentor/Reflection/Php/Function_.php

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
use phpDocumentor\Reflection\Element;
1717
use phpDocumentor\Reflection\Fqsen;
1818
use phpDocumentor\Reflection\Location;
19+
use phpDocumentor\Reflection\Type;
20+
use phpDocumentor\Reflection\Types\Mixed_;
1921

2022
/**
2123
* Descriptor representing a function
@@ -42,21 +44,37 @@ final class Function_ implements Element
4244
*/
4345
private $location;
4446

47+
/**
48+
* @var Type
49+
*/
50+
private $returnType;
51+
4552
/**
4653
* Initializes the object.
4754
*
4855
* @param Fqsen $fqsen
4956
* @param DocBlock|null $docBlock
57+
* @param Location|null $location
58+
* @param Type|null $returnType
5059
*/
51-
public function __construct(Fqsen $fqsen, DocBlock $docBlock = null, Location $location = null)
52-
{
60+
public function __construct(
61+
Fqsen $fqsen,
62+
DocBlock $docBlock = null,
63+
Location $location = null,
64+
Type $returnType = null
65+
) {
5366
if ($location === null) {
5467
$location = new Location(-1);
5568
}
5669

70+
if ($returnType === null) {
71+
$returnType = new Mixed_();
72+
}
73+
5774
$this->fqsen = $fqsen;
5875
$this->docBlock = $docBlock;
5976
$this->location = $location;
77+
$this->returnType = $returnType;
6078
}
6179

6280
/**
@@ -116,4 +134,12 @@ public function getLocation()
116134
{
117135
return $this->location;
118136
}
137+
138+
/**
139+
* @return Type
140+
*/
141+
public function getReturnType() : Type
142+
{
143+
return $this->returnType;
144+
}
119145
}

tests/unit/phpDocumentor/Reflection/Php/Function_Test.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
use \Mockery as m;
1616
use phpDocumentor\Reflection\DocBlock;
1717
use phpDocumentor\Reflection\Fqsen;
18+
use phpDocumentor\Reflection\Types\Mixed_;
19+
use phpDocumentor\Reflection\Types\String_;
1820

1921
/**
2022
* Tests the functionality for the Function_ class.
@@ -81,4 +83,31 @@ public function testGetDocblock()
8183
{
8284
$this->assertSame($this->docBlock, $this->fixture->getDocBlock());
8385
}
86+
87+
/**
88+
* @covers ::getReturnType
89+
* @covers ::__construct
90+
*/
91+
public function testGetDefaultReturnType()
92+
{
93+
$method = new Function_($this->fqsen);
94+
$this->assertEquals(new Mixed_(), $method->getReturnType());
95+
}
96+
97+
/**
98+
* @covers ::getReturnType
99+
* @covers ::__construct
100+
*/
101+
public function testGetReturnTypeFromConstructor()
102+
{
103+
$returnType = new String_();
104+
$method = new Function_(
105+
$this->fqsen,
106+
null,
107+
null,
108+
$returnType
109+
);
110+
111+
$this->assertSame($returnType, $method->getReturnType());
112+
}
84113
}

0 commit comments

Comments
 (0)