Skip to content

Commit 12e9ec0

Browse files
committed
Fixes #1 Primitive types cause exceptions: "unknown class"
1 parent fca626c commit 12e9ec0

File tree

3 files changed

+181
-6
lines changed

3 files changed

+181
-6
lines changed

src/PhpDocReader/PhpDocReader.php

Lines changed: 36 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,20 @@ class PhpDocReader
1818
*/
1919
private $phpParser;
2020

21+
private $ignoredTypes = array(
22+
'bool',
23+
'boolean',
24+
'string',
25+
'int',
26+
'integer',
27+
'float',
28+
'double',
29+
'array',
30+
'object',
31+
'callable',
32+
'resource',
33+
);
34+
2135
public function __construct()
2236
{
2337
$this->phpParser = new PhpParser();
@@ -30,6 +44,7 @@ public function __construct()
3044
*
3145
* @throws AnnotationException
3246
* @return string|null Type of the property (content of var annotation)
47+
* @todo Rename to getPropertyClass
3348
*/
3449
public function getPropertyType(ReflectionProperty $property)
3550
{
@@ -40,6 +55,11 @@ public function getPropertyType(ReflectionProperty $property)
4055
return null;
4156
}
4257

58+
// Ignore primitive types
59+
if (in_array($type, $this->ignoredTypes)) {
60+
return null;
61+
}
62+
4363
$class = $property->getDeclaringClass();
4464

4565
// If the class name is not fully qualified (i.e. doesn't start with a \)
@@ -74,8 +94,9 @@ public function getPropertyType(ReflectionProperty $property)
7494

7595
if (!$found) {
7696
throw new AnnotationException(sprintf(
77-
'The @var annotation on %s::%s contains a non existent class. '
78-
. 'Did you maybe forget to add a \'use\' statement for this annotation?',
97+
'The @var annotation on %s::%s contains a non existent class "%s". '
98+
. 'Did you maybe forget to add a "use" statement for this annotation?',
99+
$type,
79100
$class->name,
80101
$property->getName()
81102
));
@@ -84,7 +105,8 @@ public function getPropertyType(ReflectionProperty $property)
84105

85106
if (!$this->classExists($type)) {
86107
throw new AnnotationException(sprintf(
87-
'The @var annotation on %s::%s contains a non existent class',
108+
'The @var annotation on %s::%s contains a non existent class "%s"',
109+
$type,
88110
$class->name,
89111
$property->getName()
90112
));
@@ -103,6 +125,7 @@ public function getPropertyType(ReflectionProperty $property)
103125
*
104126
* @throws AnnotationException
105127
* @return string|null Type of the property (content of var annotation)
128+
* @todo Rename to getParameterClass
106129
*/
107130
public function getParameterType(ReflectionParameter $parameter)
108131
{
@@ -121,6 +144,11 @@ public function getParameterType(ReflectionParameter $parameter)
121144
return null;
122145
}
123146

147+
// Ignore primitive types
148+
if (in_array($type, $this->ignoredTypes)) {
149+
return null;
150+
}
151+
124152
$class = $parameter->getDeclaringClass();
125153

126154
// If the class name is not fully qualified (i.e. doesn't start with a \)
@@ -155,8 +183,9 @@ public function getParameterType(ReflectionParameter $parameter)
155183

156184
if (!$found) {
157185
throw new AnnotationException(sprintf(
158-
'The @param annotation for parameter %s of %s::%s contains a non existent class. '
159-
. 'Did you maybe forget to add a \'use\' statement for this annotation?',
186+
'The @param annotation for parameter %s of %s::%s contains a non existent class "%s". '
187+
. 'Did you maybe forget to add a "use" statement for this annotation?',
188+
$type,
160189
$parameterName,
161190
$class->name,
162191
$method->name
@@ -166,7 +195,8 @@ public function getParameterType(ReflectionParameter $parameter)
166195

167196
if (!$this->classExists($type)) {
168197
throw new AnnotationException(sprintf(
169-
'The @param annotation for parameter %s of %s::%s contains a non existent class',
198+
'The @param annotation for parameter %s of %s::%s contains a non existent class "%s"',
199+
$type,
170200
$parameterName,
171201
$class->name,
172202
$method->name
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
<?php
2+
3+
namespace UnitTest\PhpDocReader\FixturesIssue1;
4+
5+
class Class1
6+
{
7+
/**
8+
* @var bool
9+
*/
10+
public $bool;
11+
12+
/**
13+
* @var boolean
14+
*/
15+
public $boolean;
16+
17+
/**
18+
* @var string
19+
*/
20+
public $string;
21+
22+
/**
23+
* @var int
24+
*/
25+
public $int;
26+
27+
/**
28+
* @var integer
29+
*/
30+
public $integer;
31+
32+
/**
33+
* @var float
34+
*/
35+
public $float;
36+
37+
/**
38+
* @var double
39+
*/
40+
public $double;
41+
42+
/**
43+
* @var array
44+
*/
45+
public $array;
46+
47+
/**
48+
* @var object
49+
*/
50+
public $object;
51+
52+
/**
53+
* @var callable
54+
*/
55+
public $callable;
56+
57+
/**
58+
* @var resource
59+
*/
60+
public $resource;
61+
62+
/**
63+
* @param bool $bool
64+
* @param boolean $boolean
65+
* @param string $string
66+
* @param int $int
67+
* @param integer $integer
68+
* @param float $float
69+
* @param double $double
70+
* @param array $array
71+
* @param object $object
72+
* @param callable $callable
73+
* @param resource $resource
74+
*/
75+
public function foo(
76+
$bool,
77+
$boolean,
78+
$string,
79+
$int,
80+
$integer,
81+
$float,
82+
$double,
83+
$array,
84+
$object,
85+
$callable,
86+
$resource
87+
) {
88+
}
89+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
<?php
2+
3+
namespace UnitTest\PhpDocReader;
4+
5+
use PhpDocReader\PhpDocReader;
6+
7+
/**
8+
* @see https://github.com/mnapoli/PhpDocReader/issues/1
9+
*/
10+
class Issue1Test extends \PHPUnit_Framework_TestCase
11+
{
12+
/**
13+
* @dataProvider typeProvider
14+
*/
15+
public function testProperties($type)
16+
{
17+
$parser = new PhpDocReader();
18+
$class = new \ReflectionClass('UnitTest\PhpDocReader\FixturesIssue1\Class1');
19+
20+
$this->assertNull($parser->getPropertyType($class->getProperty($type)));
21+
}
22+
23+
/**
24+
* @dataProvider typeProvider
25+
*/
26+
public function testMethodParameters($type)
27+
{
28+
$parser = new PhpDocReader();
29+
30+
$class = new \ReflectionClass('UnitTest\PhpDocReader\FixturesIssue1\Class1');
31+
$params = $class->getMethod('foo')->getParameters();
32+
$keys = array_map(function (\ReflectionParameter $param) {
33+
return $param->getName();
34+
}, $params);
35+
$params = array_combine($keys, $params);
36+
37+
$this->assertNull($parser->getParameterType($params[$type]));
38+
}
39+
40+
public function typeProvider()
41+
{
42+
return array(
43+
'bool' => array('bool'),
44+
'boolean' => array('boolean'),
45+
'string' => array('string'),
46+
'int' => array('int'),
47+
'integer' => array('integer'),
48+
'float' => array('float'),
49+
'double' => array('double'),
50+
'array' => array('array'),
51+
'object' => array('object'),
52+
'callable' => array('callable'),
53+
'resource' => array('resource'),
54+
);
55+
}
56+
}

0 commit comments

Comments
 (0)