diff --git a/CHANGELOG.md b/CHANGELOG.md index 634f9c9..0be1625 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,13 +4,14 @@ There are next changes: +- fix `is_callable` method which fails on malformed arrays + ## v2.0.4 There are next changes: - make more unique rewritten files paths depends on php internals - ## v2.0.3 There are next changes: diff --git a/src/Badoo/SoftMocks.php b/src/Badoo/SoftMocks.php index 05976af..73e37f7 100644 --- a/src/Badoo/SoftMocks.php +++ b/src/Badoo/SoftMocks.php @@ -1293,6 +1293,10 @@ public static function isCallable($callable) } if (is_array($callable) && sizeof($callable) === 2) { + if (!isset($callable[0]) || !isset($callable[1])) { + return is_callable($callable); + } + if (is_object($callable[0])) { $class = get_class($callable[0]); } else { diff --git a/tests/unit/Badoo/SoftMocksTest.php b/tests/unit/Badoo/SoftMocksTest.php index 97341dc..b8710f3 100644 --- a/tests/unit/Badoo/SoftMocksTest.php +++ b/tests/unit/Badoo/SoftMocksTest.php @@ -2777,16 +2777,35 @@ public function testGetDeclaringTrait() $getDeclaringTrait->setAccessible(true); // No traits. - $this->assertNull($getDeclaringTrait->invoke(null, ClassWithoutTraits::class, 'do_things')); + $this->assertNull($getDeclaringTrait->invoke(null, ClassWithoutTraits::class, 'doThings')); // Direct trait usage. /** @var \ReflectionClass $result */ - $result = $getDeclaringTrait->invoke(null, ClassWithTraitB::class, 'do_things'); + $result = $getDeclaringTrait->invoke(null, ClassWithTraitB::class, 'doThings'); $this->assertSame(TraitB::class, $result->getName()); // Trait in trait usage. /** @var \ReflectionClass $result */ - $result = $getDeclaringTrait->invoke(null, ClassWithTraitA::class, 'do_things'); + $result = $getDeclaringTrait->invoke(null, ClassWithTraitA::class, 'doThings'); $this->assertSame(TraitB::class, $result->getName()); } + + public function providerClassWithIsCallable() + { + return [ + 'numeric keys' => [[ClassWithIsCallable::class, 'doThings'], true], + 'assoc keys' => [['x' => ClassWithIsCallable::class, 'y' => 'doThings'], false], + ]; + } + + /** + * @dataProvider providerClassWithIsCallable + * + * @param callable $callable + * @param bool $expected_result + */ + public function testClassWithIsCallable($callable, $expected_result) + { + $this->assertSame($expected_result, ClassWithIsCallable::check($callable)); + } } diff --git a/tests/unit/Badoo/SoftMocksTestClasses.php b/tests/unit/Badoo/SoftMocksTestClasses.php index dc381b4..a92fd6e 100644 --- a/tests/unit/Badoo/SoftMocksTestClasses.php +++ b/tests/unit/Badoo/SoftMocksTestClasses.php @@ -273,10 +273,7 @@ trait TraitA trait TraitB { - public function do_things() - { - return; - } + public function doThings() {} } class ClassWithTraitA @@ -291,8 +288,18 @@ class ClassWithTraitB class ClassWithoutTraits { - public function do_things() + public function doThings() {} +} + +class ClassWithIsCallable +{ + public static function doThings() {} + + public static function check($callable) { - return; + if (is_callable($callable)) { + return true; + } + return false; } -} \ No newline at end of file +}