Skip to content

Commit 7bdf3e1

Browse files
committed
Fix a bug where PHP 7.1 nullable types would generate a "Call to undefined method PhpParser\Node\NullableType::toString()" error.
1 parent 002f3e1 commit 7bdf3e1

File tree

2 files changed

+76
-2
lines changed

2 files changed

+76
-2
lines changed

src/PHPSemVerChecker/Comparator/Type.php

+12-2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace PHPSemVerChecker\Comparator;
44

5+
use PhpParser\Node\NullableType;
6+
57
class Type
68
{
79
/**
@@ -17,11 +19,19 @@ public static function isSame($typeA, $typeB)
1719
}
1820

1921
/**
20-
* @param \PhpParser\Node\Name|string|null $type
22+
* @param \PhpParser\Node\Name|\PhpParser\Node\NullableType|string|null $type
2123
* @return string|null
2224
*/
2325
public static function get($type)
2426
{
25-
return is_object($type) ? $type->toString() : $type;
27+
if (! is_object($type)) {
28+
return $type;
29+
}
30+
31+
if ($type instanceof NullableType) {
32+
return '?'.static::get($type->type);
33+
}
34+
35+
return $type->toString();
2636
}
2737
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
<?php
2+
3+
namespace PHPSemVerChecker\Test\Comparator;
4+
5+
use PhpParser\Node\Name;
6+
use PhpParser\Node\NullableType;
7+
use PHPSemVerChecker\Comparator\Type;
8+
use PHPSemVerChecker\Test\TestCase;
9+
10+
class TypeComparatorTest extends TestCase
11+
{
12+
/**
13+
* @dataProvider isSameProvider
14+
*/
15+
public function testIsSame($typeA, $typeB)
16+
{
17+
$this->assertTrue(Type::isSame($typeA, $typeB));
18+
}
19+
20+
public function isSameProvider()
21+
{
22+
return [
23+
[Name::concat(null, 'test'), Name::concat(null, 'test')],
24+
['test', 'test'],
25+
[null, null]
26+
];
27+
}
28+
29+
/**
30+
* @dataProvider isNotSameProvider
31+
*/
32+
public function testIsNotSame($typeA, $typeB)
33+
{
34+
$this->assertFalse(Type::isSame($typeA, $typeB));
35+
}
36+
37+
public function isNotSameProvider()
38+
{
39+
return [
40+
[Name::concat(null, 'test'), Name::concat(null, 'test1')],
41+
['test', 'test1'],
42+
[null, 'test']
43+
];
44+
}
45+
46+
/**
47+
* @dataProvider getProvider
48+
*/
49+
public function testGet($type, $expected)
50+
{
51+
$this->assertSame($expected, Type::get($type));
52+
}
53+
54+
public function getProvider()
55+
{
56+
return [
57+
[null, null],
58+
['test', 'test'],
59+
[Name::concat('namespaced', 'test'), 'namespaced\test'],
60+
[new NullableType('test'), '?test'],
61+
[new NullableType(Name::concat('namespaced', 'test')), '?namespaced\test'],
62+
];
63+
}
64+
}

0 commit comments

Comments
 (0)