Skip to content

Commit 7afc9d7

Browse files
authored
Merge pull request #110 from jaapio/feature/return-types
Adds support for return types on methods
2 parents 66de6ad + 7acc2ee commit 7afc9d7

File tree

8 files changed

+153
-9
lines changed

8 files changed

+153
-9
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/Factory/Method.php

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,14 @@
1212

1313
namespace phpDocumentor\Reflection\Php\Factory;
1414

15-
use InvalidArgumentException;
1615
use phpDocumentor\Reflection\Location;
1716
use phpDocumentor\Reflection\Php\Method as MethodDescriptor;
1817
use phpDocumentor\Reflection\Php\ProjectFactoryStrategy;
1918
use phpDocumentor\Reflection\Php\StrategyContainer;
2019
use phpDocumentor\Reflection\Php\Visibility;
20+
use phpDocumentor\Reflection\TypeResolver;
2121
use phpDocumentor\Reflection\Types\Context;
22-
use PhpParser\Comment\Doc;
22+
use phpDocumentor\Reflection\Types\Mixed_;
2323
use PhpParser\Node\Stmt\ClassMethod;
2424

2525
/**
@@ -50,14 +50,21 @@ protected function doCreate($object, StrategyContainer $strategies, Context $con
5050
{
5151
$docBlock = $this->createDocBlock($strategies, $object->getDocComment(), $context);
5252

53+
$returnType = null;
54+
if ($object->returnType !== null) {
55+
$typeResolver = new TypeResolver();
56+
$returnType = $typeResolver->resolve($object->returnType, $context);
57+
}
58+
5359
$method = new MethodDescriptor(
5460
$object->fqsen,
5561
$this->buildVisibility($object),
5662
$docBlock,
5763
$object->isAbstract(),
5864
$object->isStatic(),
5965
$object->isFinal(),
60-
new Location($object->getLine())
66+
new Location($object->getLine()),
67+
$returnType
6168
);
6269

6370
foreach ($object->params as $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
}

src/phpDocumentor/Reflection/Php/Method.php

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
use phpDocumentor\Reflection\Element;
1818
use phpDocumentor\Reflection\Location;
1919
use phpDocumentor\Reflection\Php\Visibility;
20+
use phpDocumentor\Reflection\Type;
21+
use phpDocumentor\Reflection\Types\Mixed_;
2022

2123
/**
2224
* Descriptor representing a Method in a Class, Interface or Trait.
@@ -52,6 +54,10 @@ final class Method implements Element
5254
* @var Location
5355
*/
5456
private $location;
57+
/**
58+
* @var Type
59+
*/
60+
private $returnType;
5561

5662
/**
5763
* Initializes the all properties.
@@ -63,6 +69,7 @@ final class Method implements Element
6369
* @param bool $static
6470
* @param bool $final
6571
* @param Location|null $location
72+
* @param Type $returnType
6673
*/
6774
public function __construct(
6875
Fqsen $fqsen,
@@ -71,7 +78,8 @@ public function __construct(
7178
$abstract = false,
7279
$static = false,
7380
$final = false,
74-
Location $location = null
81+
Location $location = null,
82+
Type $returnType = null
7583
) {
7684
$this->fqsen = $fqsen;
7785
$this->visibility = $visibility;
@@ -85,10 +93,15 @@ public function __construct(
8593
$location = new Location(-1);
8694
}
8795

96+
if ($returnType === null) {
97+
$returnType = new Mixed_();
98+
}
99+
88100
$this->abstract = $abstract;
89101
$this->static = $static;
90102
$this->final = $final;
91103
$this->location = $location;
104+
$this->returnType = $returnType;
92105
}
93106

94107
/**
@@ -190,4 +203,18 @@ public function getLocation()
190203
{
191204
return $this->location;
192205
}
206+
207+
/**
208+
* Returns the in code defined return type.
209+
*
210+
* Return types are introduced in php 7.0 when your could doesn't have a
211+
* return type defined this method will return Mixed_ by default. The return value of this
212+
* method is not affected by the return tag in your docblock.
213+
*
214+
* @return Type
215+
*/
216+
public function getReturnType()
217+
{
218+
return $this->returnType;
219+
}
193220
}

tests/component/ProjectCreationTest.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,10 @@
1515
use Mockery as m;
1616
use phpDocumentor\Reflection\DocBlock\Tags\Param;
1717
use phpDocumentor\Reflection\File\LocalFile;
18+
use phpDocumentor\Reflection\Php\Interface_;
1819
use phpDocumentor\Reflection\Php\ProjectFactory;
1920
use phpDocumentor\Reflection\Types\Object_;
21+
use phpDocumentor\Reflection\Types\String_;
2022

2123
/**
2224
* Intergration tests to check the correct working of processing a file into a project.
@@ -173,4 +175,17 @@ public function testInterfaceExtends()
173175
$this->assertEquals(['\\Packing' => new Fqsen('\\Packing')], $interface->getParents());
174176

175177
}
178+
179+
public function testMethodReturnType()
180+
{
181+
$fileName = __DIR__ . '/project/Packing.php';
182+
$project = $this->fixture->create('MyProject', [
183+
new LocalFile($fileName)
184+
]);
185+
186+
$this->assertArrayHasKey('\\Packing', $project->getFiles()[$fileName]->getInterfaces());
187+
$interface = current($project->getFiles()[$fileName]->getInterfaces());
188+
189+
$this->assertEquals(new String_(), $interface->getMethods()['\Packing::getName()']->getReturnType());
190+
}
176191
}

tests/component/project/Packing.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,5 @@
1212

1313
interface Packing
1414
{
15-
public function getName();
16-
}
15+
public function getName() : string;
16+
}

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
}

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

Lines changed: 33 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 Method class.
@@ -135,4 +137,35 @@ public function testGetDefaultVisibility()
135137
$method = new Method($this->fqsen);
136138
$this->assertEquals(new Visibility('public'), $method->getVisibility());
137139
}
140+
141+
/**
142+
* @covers ::getReturnType
143+
* @covers ::__construct
144+
*/
145+
public function testGetDefaultReturnType()
146+
{
147+
$method = new Method($this->fqsen);
148+
$this->assertEquals(new Mixed_(), $method->getReturnType());
149+
}
150+
151+
/**
152+
* @covers ::getReturnType
153+
* @covers ::__construct
154+
*/
155+
public function testGetReturnTypeFromConstructor()
156+
{
157+
$returnType = new String_();
158+
$method = new Method(
159+
$this->fqsen,
160+
new Visibility('public'),
161+
null,
162+
false,
163+
false,
164+
false,
165+
null,
166+
$returnType
167+
);
168+
169+
$this->assertSame($returnType, $method->getReturnType());
170+
}
138171
}

0 commit comments

Comments
 (0)