Skip to content

Commit 8eeaa96

Browse files
committed
Simplified usage
1 parent 31d995d commit 8eeaa96

File tree

2 files changed

+27
-22
lines changed

2 files changed

+27
-22
lines changed

src/PhpDocReader/PhpDocReader.php

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@
33
namespace PhpDocReader;
44

55
use Doctrine\Common\Annotations\PhpParser;
6-
use ReflectionClass;
7-
use ReflectionMethod;
86
use ReflectionParameter;
97
use ReflectionProperty;
108

@@ -30,14 +28,17 @@ public function __construct()
3028
}
3129

3230
/**
33-
* Parse the docblock of the property to get the var annotation
34-
* @param ReflectionClass $class
31+
* Parse the docblock of the property to get the var annotation.
32+
*
3533
* @param ReflectionProperty $property
34+
*
3635
* @throws AnnotationException
3736
* @return string|null Type of the property (content of var annotation)
3837
*/
39-
public function getPropertyType(ReflectionClass $class, ReflectionProperty $property)
38+
public function getPropertyType(ReflectionProperty $property)
4039
{
40+
$class = $property->getDeclaringClass();
41+
4142
// Get the content of the @var annotation
4243
if (preg_match('/@var\s+([^\s]+)/', $property->getDocComment(), $matches)) {
4344
list(, $type) = $matches;
@@ -93,14 +94,18 @@ public function getPropertyType(ReflectionClass $class, ReflectionProperty $prop
9394
}
9495

9596
/**
96-
* Parse the docblock of the property to get the param annotation
97-
* @param ReflectionClass $class
98-
* @param ReflectionMethod $method
97+
* Parse the docblock of the property to get the param annotation.
98+
*
99+
* @param ReflectionParameter $parameter
99100
* @throws AnnotationException
101+
*
100102
* @return string|null Type of the property (content of var annotation)
101103
*/
102-
public function getParameterType(ReflectionClass $class, ReflectionMethod $method, ReflectionParameter $parameter)
104+
public function getParameterType(ReflectionParameter $parameter)
103105
{
106+
$method = $parameter->getDeclaringFunction();
107+
$class = $parameter->getDeclaringClass();
108+
104109
// Use reflection
105110
$parameterClass = $parameter->getClass();
106111
if ($parameterClass !== null) {
@@ -121,7 +126,7 @@ public function getParameterType(ReflectionClass $class, ReflectionMethod $metho
121126
$loweredAlias = strtolower($alias);
122127

123128
// Retrieve "use" statements
124-
$uses = $this->phpParser->parseClass($method->getDeclaringClass());
129+
$uses = $this->phpParser->parseClass($class);
125130

126131
$found = false;
127132

tests/UnitTest/PhpDocReader/PhpDocReaderTest.php

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,13 @@ public function testGetParameterTypeUseStatementBeforeLocalNamespace()
2323
$target1ReflectionMethod = $target1ReflectionClass->getMethod("SomeMethod");
2424
$target1ReflectionParams = $target1ReflectionMethod->getParameters();
2525

26-
$result = $parser->getParameterType($target1ReflectionClass, $target1ReflectionMethod, $target1ReflectionParams[0]);
26+
$result = $parser->getParameterType($target1ReflectionParams[0]);
2727

2828
//Since TargetFixture1 file has a use statement to the Subspace namespace, that's the one that should be returned
2929
$this->assertEquals('UnitTest\PhpDocReader\Fixtures\Subspace\SomeDependencyFixture', $result);
3030

3131

32-
$result = $parser->getParameterType($target1ReflectionClass, $target1ReflectionMethod, $target1ReflectionParams[1]);
32+
$result = $parser->getParameterType($target1ReflectionParams[1]);
3333

3434
//this parameter should be unaffected by use namespace since it has a relative type path
3535
$this->assertEquals('UnitTest\PhpDocReader\Fixtures\Subspace\SomeDependencyFixture2', $result);
@@ -42,13 +42,13 @@ public function testGetParameterTypeUseStatementBeforeLocalNamespace()
4242
$target2ReflectionMethod = $target2ReflectionClass->getMethod("SomeMethod");
4343
$target2ReflectionParams = $target2ReflectionMethod->getParameters();
4444

45-
$result = $parser->getParameterType($target2ReflectionClass, $target2ReflectionMethod, $target2ReflectionParams[0]);
45+
$result = $parser->getParameterType($target2ReflectionParams[0]);
4646

4747
//Since TargetFixture2 file has a use statement with an alias to the Subspace namespace, that's the one that should be returned
4848
$this->assertEquals('UnitTest\PhpDocReader\Fixtures\Subspace\SomeDependencyFixture2', $result);
4949

5050

51-
$result = $parser->getParameterType($target2ReflectionClass, $target2ReflectionMethod, $target2ReflectionParams[1]);
51+
$result = $parser->getParameterType($target2ReflectionParams[1]);
5252

5353
//this parameter should be unaffected by use namespace since it has a relative type path
5454
$this->assertEquals('UnitTest\PhpDocReader\Fixtures\Subspace\SomeDependencyFixture2', $result);
@@ -61,13 +61,13 @@ public function testGetParameterTypeUseStatementBeforeLocalNamespace()
6161
$target3ReflectionMethod = $target3ReflectionClass->getMethod("SomeMethod");
6262
$target3ReflectionParams = $target3ReflectionMethod->getParameters();
6363

64-
$result = $parser->getParameterType($target3ReflectionClass, $target3ReflectionMethod, $target3ReflectionParams[0]);
64+
$result = $parser->getParameterType($target3ReflectionParams[0]);
6565

6666
//Since TargetFixture3 file has NO use statement, the one local to the target's namespace should be used
6767
$this->assertEquals('UnitTest\PhpDocReader\Fixtures\SomeDependencyFixture', $result);
6868

6969

70-
$result = $parser->getParameterType($target3ReflectionClass, $target3ReflectionMethod, $target3ReflectionParams[1]);
70+
$result = $parser->getParameterType($target3ReflectionParams[1]);
7171

7272
//this parameter should be unaffected by use namespace since it has a relative type path
7373
$this->assertEquals('UnitTest\PhpDocReader\Fixtures\Subspace\SomeDependencyFixture2', $result);
@@ -87,15 +87,15 @@ public function testGetPropertyTypeUseStatementBeforeLocalNamespace()
8787
$target1ReflectionClass = new \ReflectionClass($target1);
8888
$target1ReflectionProperty1 = $target1ReflectionClass->getProperty("dependency1");
8989

90-
$result = $parser->getPropertyType($target1ReflectionClass, $target1ReflectionProperty1);
90+
$result = $parser->getPropertyType($target1ReflectionProperty1);
9191

9292
//Since TargetFixture1 file has a use statement to the Subspace namespace, that's the one that should be returned
9393
$this->assertEquals('UnitTest\PhpDocReader\Fixtures\Subspace\SomeDependencyFixture', $result);
9494

9595

9696
$target1ReflectionProperty2 = $target1ReflectionClass->getProperty("dependency2");
9797

98-
$result = $parser->getPropertyType($target1ReflectionClass, $target1ReflectionProperty2);
98+
$result = $parser->getPropertyType($target1ReflectionProperty2);
9999

100100
//this property should be unaffected by use namespace since it has a relative type path
101101
$this->assertEquals('UnitTest\PhpDocReader\Fixtures\Subspace\SomeDependencyFixture2', $result);
@@ -107,15 +107,15 @@ public function testGetPropertyTypeUseStatementBeforeLocalNamespace()
107107
$target2ReflectionClass = new \ReflectionClass($target2);
108108
$target2ReflectionProperty1 = $target2ReflectionClass->getProperty("dependency1");
109109

110-
$result = $parser->getPropertyType($target2ReflectionClass, $target2ReflectionProperty1);
110+
$result = $parser->getPropertyType($target2ReflectionProperty1);
111111

112112
//Since TargetFixture2 file has a use statement with an alias to the Subspace namespace, that's the one that should be returned
113113
$this->assertEquals('UnitTest\PhpDocReader\Fixtures\Subspace\SomeDependencyFixture2', $result);
114114

115115

116116
$target2ReflectionProperty2 = $target2ReflectionClass->getProperty("dependency2");
117117

118-
$result = $parser->getPropertyType($target2ReflectionClass, $target2ReflectionProperty2);
118+
$result = $parser->getPropertyType($target2ReflectionProperty2);
119119

120120
//this property should be unaffected by use namespace since it has a relative type path
121121
$this->assertEquals('UnitTest\PhpDocReader\Fixtures\Subspace\SomeDependencyFixture2', $result);
@@ -127,15 +127,15 @@ public function testGetPropertyTypeUseStatementBeforeLocalNamespace()
127127
$target3ReflectionClass = new \ReflectionClass($target3);
128128
$target3ReflectionProperty1 = $target3ReflectionClass->getProperty("dependency1");
129129

130-
$result = $parser->getPropertyType($target3ReflectionClass, $target3ReflectionProperty1);
130+
$result = $parser->getPropertyType($target3ReflectionProperty1);
131131

132132
//Since TargetFixture3 file has NO use statement, the one local to the target's namespace should be used
133133
$this->assertEquals('UnitTest\PhpDocReader\Fixtures\SomeDependencyFixture', $result);
134134

135135

136136
$target3ReflectionProperty2 = $target3ReflectionClass->getProperty("dependency2");
137137

138-
$result = $parser->getPropertyType($target3ReflectionClass, $target3ReflectionProperty2);
138+
$result = $parser->getPropertyType($target3ReflectionProperty2);
139139

140140
//this property should be unaffected by use namespace since it has a relative type path
141141
$this->assertEquals('UnitTest\PhpDocReader\Fixtures\Subspace\SomeDependencyFixture2', $result);

0 commit comments

Comments
 (0)