Skip to content

Commit f2cc01d

Browse files
xificurkdg
authored andcommitted
Reflection::getParameterDefaultValue() fixed constants parsing in namespaced scope (#129)
1 parent d552403 commit f2cc01d

File tree

2 files changed

+57
-45
lines changed

2 files changed

+57
-45
lines changed

src/Utils/Reflection.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -75,14 +75,17 @@ public static function getParameterType(\ReflectionParameter $param)
7575
public static function getParameterDefaultValue(\ReflectionParameter $param)
7676
{
7777
if ($param->isDefaultValueConstant()) {
78-
$const = $param->getDefaultValueConstantName();
78+
$const = $orig = $param->getDefaultValueConstantName();
7979
$pair = explode('::', $const);
8080
if (isset($pair[1]) && strtolower($pair[0]) === 'self') {
8181
$const = $param->getDeclaringClass()->getName() . '::' . $pair[1];
8282
}
8383
if (!defined($const)) {
84-
$name = self::toString($param);
85-
throw new \ReflectionException("Unable to resolve constant $const used as default value of $name.");
84+
$const = substr((string) strrchr($const, '\\'), 1);
85+
if (isset($pair[1]) || !defined($const)) {
86+
$name = self::toString($param);
87+
throw new \ReflectionException("Unable to resolve constant $orig used as default value of $name.");
88+
}
8689
}
8790
return constant($const);
8891
}

tests/Utils/Reflection.getParameterDefaultValue.phpt

Lines changed: 51 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -4,60 +4,69 @@
44
* Test: Nette\Utils\Reflection::getParameterDefaultValue()
55
*/
66

7-
use Nette\Utils\Reflection;
8-
use Tester\Assert;
7+
namespace NS {
8+
define('DEFINED', 123);
9+
define('NS_DEFINED', 'xxx');
10+
const NS_DEFINED = 456;
911

10-
require __DIR__ . '/../bootstrap.php';
11-
12-
13-
define('DEFINED', 123);
14-
15-
interface Bar
16-
{
17-
const DEFINED = 'xyz';
18-
}
12+
interface Bar
13+
{
14+
const DEFINED = 'xyz';
15+
}
1916

20-
class Foo
21-
{
22-
const DEFINED = 'abc';
23-
24-
function method(
25-
$a,
26-
$b = self::DEFINED,
27-
$c = Foo::DEFINED,
28-
$d = SELF::DEFINED,
29-
$e = bar::DEFINED,
30-
$f = self::UNDEFINED,
31-
$g = Undefined::ANY,
32-
$h = DEFINED,
33-
$i = UNDEFINED)
17+
class Foo
3418
{
19+
const DEFINED = 'abc';
20+
21+
function method(
22+
$a,
23+
$b = self::DEFINED,
24+
$c = Foo::DEFINED,
25+
$d = SELF::DEFINED,
26+
$e = bar::DEFINED,
27+
$f = self::UNDEFINED,
28+
$g = Undefined::ANY,
29+
$h = DEFINED,
30+
$i = UNDEFINED,
31+
$j = NS_DEFINED
32+
) {
33+
}
3534
}
3635
}
3736

3837

39-
Assert::exception(function () {
40-
Reflection::getParameterDefaultValue(new ReflectionParameter(['Foo', 'method'], 'a'));
41-
}, ReflectionException::class);
38+
namespace {
39+
use Nette\Utils\Reflection;
40+
use Tester\Assert;
4241

43-
Assert::same(Foo::DEFINED, Reflection::getParameterDefaultValue(new ReflectionParameter(['Foo', 'method'], 'b')));
42+
require __DIR__ . '/../bootstrap.php';
4443

45-
Assert::same(Foo::DEFINED, Reflection::getParameterDefaultValue(new ReflectionParameter(['Foo', 'method'], 'c')));
4644

47-
Assert::same(Foo::DEFINED, Reflection::getParameterDefaultValue(new ReflectionParameter(['Foo', 'method'], 'd')));
45+
Assert::exception(function () {
46+
Reflection::getParameterDefaultValue(new ReflectionParameter(['NS\Foo', 'method'], 'a'));
47+
}, ReflectionException::class);
4848

49-
Assert::same(Bar::DEFINED, Reflection::getParameterDefaultValue(new ReflectionParameter(['Foo', 'method'], 'e')));
49+
Assert::same(NS\Foo::DEFINED, Reflection::getParameterDefaultValue(new ReflectionParameter(['NS\Foo', 'method'], 'b')));
5050

51-
Assert::exception(function () {
52-
Reflection::getParameterDefaultValue(new ReflectionParameter(['Foo', 'method'], 'f'));
53-
}, ReflectionException::class, 'Unable to resolve constant Foo::UNDEFINED used as default value of $f in Foo::method().');
51+
Assert::same(NS\Foo::DEFINED, Reflection::getParameterDefaultValue(new ReflectionParameter(['NS\Foo', 'method'], 'c')));
5452

55-
Assert::exception(function () {
56-
Reflection::getParameterDefaultValue(new ReflectionParameter(['Foo', 'method'], 'g'));
57-
}, ReflectionException::class, 'Unable to resolve constant Undefined::ANY used as default value of $g in Foo::method().');
53+
Assert::same(NS\Foo::DEFINED, Reflection::getParameterDefaultValue(new ReflectionParameter(['NS\Foo', 'method'], 'd')));
5854

59-
Assert::same(DEFINED, Reflection::getParameterDefaultValue(new ReflectionParameter(['Foo', 'method'], 'h')));
55+
Assert::same(NS\Bar::DEFINED, Reflection::getParameterDefaultValue(new ReflectionParameter(['NS\Foo', 'method'], 'e')));
6056

61-
Assert::exception(function () {
62-
Reflection::getParameterDefaultValue(new ReflectionParameter(['Foo', 'method'], 'i'));
63-
}, ReflectionException::class, 'Unable to resolve constant UNDEFINED used as default value of $i in Foo::method().');
57+
Assert::exception(function () {
58+
Reflection::getParameterDefaultValue(new ReflectionParameter(['NS\Foo', 'method'], 'f'));
59+
}, ReflectionException::class, 'Unable to resolve constant self::UNDEFINED used as default value of $f in NS\Foo::method().');
60+
61+
Assert::exception(function () {
62+
Reflection::getParameterDefaultValue(new ReflectionParameter(['NS\Foo', 'method'], 'g'));
63+
}, ReflectionException::class, 'Unable to resolve constant NS\Undefined::ANY used as default value of $g in NS\Foo::method().');
64+
65+
Assert::same(DEFINED, Reflection::getParameterDefaultValue(new ReflectionParameter(['NS\Foo', 'method'], 'h')));
66+
67+
Assert::exception(function () {
68+
Reflection::getParameterDefaultValue(new ReflectionParameter(['NS\Foo', 'method'], 'i'));
69+
}, ReflectionException::class, 'Unable to resolve constant NS\UNDEFINED used as default value of $i in NS\Foo::method().');
70+
71+
Assert::same(NS\NS_DEFINED, Reflection::getParameterDefaultValue(new ReflectionParameter(['NS\Foo', 'method'], 'j')));
72+
}

0 commit comments

Comments
 (0)