Skip to content

Commit f5427a1

Browse files
committed
Callback: fixed bug in is_callable($object, true) [Closes #176]
1 parent 27716f0 commit f5427a1

File tree

3 files changed

+24
-1
lines changed

3 files changed

+24
-1
lines changed

src/Utils/Callback.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ public static function toString($callable): string
107107
} elseif (is_string($callable) && $callable[0] === "\0") {
108108
return '{lambda}';
109109
} else {
110-
is_callable($callable, true, $textual);
110+
is_callable(is_object($callable) ? [$callable, '__invoke'] : $callable, true, $textual);
111111
return $textual;
112112
}
113113
}

tests/Utils/Callback.check.phpt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,13 @@ Assert::exception(function () {
2626
Assert::exception(function () {
2727
Callback::check('undefined');
2828
}, Nette\InvalidArgumentException::class, "Callback 'undefined' is not callable.");
29+
30+
31+
// PHP bugs - is_callable($object, true) fails
32+
Assert::exception(function () {
33+
Callback::check(new stdClass);
34+
}, Nette\InvalidArgumentException::class, "Callback 'stdClass::__invoke' is not callable.");
35+
36+
Assert::exception(function () {
37+
Callback::check(new stdClass, true);
38+
}, Nette\InvalidArgumentException::class, 'Given value is not a callable type.');

tests/Utils/Callback.closure.phpt

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,3 +205,16 @@ test(function () { // magic methods
205205
Callback::toReflection(Callback::closure(new Test, 'magic'));
206206
}, ReflectionException::class, 'Method Test::magic() does not exist');
207207
});
208+
209+
210+
test(function () { // PHP bugs - is_callable($object, true) fails
211+
Assert::exception(function () {
212+
Callback::closure(new stdClass);
213+
}, Nette\InvalidArgumentException::class, 'Failed to create closure from callable: no array or string given');
214+
215+
Assert::same('stdClass::__invoke', Callback::toString(new stdClass));
216+
217+
Assert::exception(function () {
218+
Callback::toReflection(new stdClass);
219+
}, ReflectionException::class, 'Method stdClass::__invoke() does not exist');
220+
});

0 commit comments

Comments
 (0)