Skip to content

Commit 1e81c2e

Browse files
committed
Adds support for return types on methods
In php7 return types are added to the syntax of php. This change adds the detection of return types to the method factory.
1 parent 66de6ad commit 1e81c2e

File tree

5 files changed

+88
-6
lines changed

5 files changed

+88
-6
lines changed

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/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/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)