Skip to content

Commit c49c92f

Browse files
committed
Fixes #3 Handle unrecognizable types: multiple types, generics, arrays, …
1 parent 77ecf23 commit c49c92f

File tree

3 files changed

+94
-0
lines changed

3 files changed

+94
-0
lines changed

src/PhpDocReader/PhpDocReader.php

+10
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,11 @@ public function getPropertyClass(ReflectionProperty $property)
7474
return null;
7575
}
7676

77+
// Ignore types containing special characters ([], <> ...)
78+
if (! preg_match('/^[a-zA-Z0-9\\\\]+$/', $type)) {
79+
return null;
80+
}
81+
7782
$class = $property->getDeclaringClass();
7883

7984
// If the class name is not fully qualified (i.e. doesn't start with a \)
@@ -175,6 +180,11 @@ public function getParameterClass(ReflectionParameter $parameter)
175180
return null;
176181
}
177182

183+
// Ignore types containing special characters ([], <> ...)
184+
if (! preg_match('/^[a-zA-Z0-9\\\\]+$/', $type)) {
185+
return null;
186+
}
187+
178188
$class = $parameter->getDeclaringClass();
179189

180190
// If the class name is not fully qualified (i.e. doesn't start with a \)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
3+
namespace UnitTest\PhpDocReader\FixturesUnknownTypes;
4+
5+
class Class1
6+
{
7+
/**
8+
* @var
9+
*/
10+
public $empty;
11+
12+
/**
13+
* @var Foo[]
14+
*/
15+
public $array;
16+
17+
/**
18+
* @var array<Foo>
19+
*/
20+
public $generics;
21+
22+
/**
23+
* @var Foo|Bar
24+
*/
25+
public $multiple;
26+
27+
/**
28+
* @param $empty
29+
* @param Foo[] $array
30+
* @param array<Foo> $generics
31+
* @param Foo|Bar $multiple
32+
*/
33+
public function foo(
34+
$empty,
35+
$array,
36+
$generics,
37+
$multiple
38+
) {
39+
}
40+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?php
2+
3+
namespace UnitTest\PhpDocReader;
4+
5+
use PhpDocReader\PhpDocReader;
6+
use ReflectionParameter;
7+
8+
/**
9+
* @see https://github.com/mnapoli/PhpDocReader/issues/3
10+
*/
11+
class UnknownTypesTest extends \PHPUnit_Framework_TestCase
12+
{
13+
/**
14+
* @dataProvider typeProvider
15+
*/
16+
public function testProperties($type)
17+
{
18+
$parser = new PhpDocReader();
19+
$class = new \ReflectionClass('UnitTest\PhpDocReader\FixturesUnknownTypes\Class1');
20+
21+
$this->assertNull($parser->getPropertyClass($class->getProperty($type)));
22+
}
23+
24+
/**
25+
* @dataProvider typeProvider
26+
*/
27+
public function testMethodParameters($type)
28+
{
29+
$parser = new PhpDocReader();
30+
$parameter = new ReflectionParameter(array('UnitTest\PhpDocReader\FixturesUnknownTypes\Class1', 'foo'), $type);
31+
32+
$this->assertNull($parser->getParameterClass($parameter));
33+
}
34+
35+
public function typeProvider()
36+
{
37+
return array(
38+
'empty' => array('empty'),
39+
'array' => array('array'),
40+
'generics' => array('generics'),
41+
'multiple' => array('multiple'),
42+
);
43+
}
44+
}

0 commit comments

Comments
 (0)