From 8ebaa1419e318b9f85916bcdca441462b47f227d Mon Sep 17 00:00:00 2001 From: Eric Date: Sun, 22 Jul 2012 14:48:27 +0200 Subject: [PATCH 1/4] Changed to old behavior for calls with more then 3 arguments. --- src/CallbackHandler.php | 6 ++++-- test/CallbackHandlerTest.php | 17 +++++++++++++++++ 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/src/CallbackHandler.php b/src/CallbackHandler.php index 7c8e7b2b9..216d47422 100644 --- a/src/CallbackHandler.php +++ b/src/CallbackHandler.php @@ -167,17 +167,19 @@ public function call(array $args = array()) $this->isPhp54 = version_compare(PHP_VERSION, '5.4.0rc1', '>='); } + $argCount = count($args); + if ($this->isPhp54 && is_string($callback)) { $result = $this->validateStringCallbackFor54($callback); - if ($result !== true) { + if ($result !== true && $argCount <= 3) { $callback = $result; } } // Minor performance tweak; use call_user_func() until > 3 arguments // reached - switch (count($args)) { + switch ($argCount) { case 0: if ($this->isPhp54) { return $callback(); diff --git a/test/CallbackHandlerTest.php b/test/CallbackHandlerTest.php index 1765de98b..80c671f72 100644 --- a/test/CallbackHandlerTest.php +++ b/test/CallbackHandlerTest.php @@ -130,6 +130,23 @@ public function testStringStaticCallbackForPhp54() $this->assertSame('staticHandler', $result); } + public function testStringStaticCallbackForPhp54WithMoreThan4Args() + { + if (version_compare(PHP_VERSION, '5.4.0rc1', '<=')) { + $this->markTestSkipped('Requires PHP 5.4'); + } + + $handler = new CallbackHandler('ZendTest\\Stdlib\\SignalHandlers\\InstanceMethod::staticHandler'); + $error = false; + set_error_handler(function ($errno, $errstr) use (&$error) { + $error = true; + }, E_STRICT); + $result = $handler->call(array(1, 2, 3, 4)); + restore_error_handler(); + $this->assertFalse($error); + $this->assertSame('staticHandler', $result); + } + public function testCallbackToClassImplementingOverloadingButNotInvocableShouldRaiseException() { $this->setExpectedException('Zend\Stdlib\Exception\InvalidCallbackException'); From 903c72ce05d55dbdc1f157711f71eb21ff566608 Mon Sep 17 00:00:00 2001 From: Eric Date: Sun, 22 Jul 2012 15:41:45 +0200 Subject: [PATCH 2/4] Added minor performance improvement for static string callbacks --- src/CallbackHandler.php | 5 ++++- test/CallbackHandlerTest.php | 2 +- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/src/CallbackHandler.php b/src/CallbackHandler.php index 216d47422..a374541aa 100644 --- a/src/CallbackHandler.php +++ b/src/CallbackHandler.php @@ -173,7 +173,10 @@ public function call(array $args = array()) $result = $this->validateStringCallbackFor54($callback); if ($result !== true && $argCount <= 3) { - $callback = $result; + $callback = $result; + // Minor performance tweak, if the callback gets called more + // then once + $this->callback = $result; } } diff --git a/test/CallbackHandlerTest.php b/test/CallbackHandlerTest.php index 80c671f72..124be3a90 100644 --- a/test/CallbackHandlerTest.php +++ b/test/CallbackHandlerTest.php @@ -130,7 +130,7 @@ public function testStringStaticCallbackForPhp54() $this->assertSame('staticHandler', $result); } - public function testStringStaticCallbackForPhp54WithMoreThan4Args() + public function testStringStaticCallbackForPhp54WithMoreThan3Args() { if (version_compare(PHP_VERSION, '5.4.0rc1', '<=')) { $this->markTestSkipped('Requires PHP 5.4'); From dbd9c06ddd65633c508fdae3c81bb1dbb240fc68 Mon Sep 17 00:00:00 2001 From: Eric Date: Mon, 23 Jul 2012 00:37:50 +0200 Subject: [PATCH 3/4] typo fix --- src/CallbackHandler.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/CallbackHandler.php b/src/CallbackHandler.php index a374541aa..55fd82ed9 100644 --- a/src/CallbackHandler.php +++ b/src/CallbackHandler.php @@ -162,7 +162,7 @@ public function call(array $args = array()) return null; } - // Minor performance tweak, if the callback gets called more the once + // Minor performance tweak, if the callback gets called more than once if (!isset($this->isPhp54)) { $this->isPhp54 = version_compare(PHP_VERSION, '5.4.0rc1', '>='); } @@ -175,7 +175,7 @@ public function call(array $args = array()) if ($result !== true && $argCount <= 3) { $callback = $result; // Minor performance tweak, if the callback gets called more - // then once + // than once $this->callback = $result; } } From 67e5c3519179f0755068ec2c35ab5f9ab80d772e Mon Sep 17 00:00:00 2001 From: Eric Date: Mon, 23 Jul 2012 14:51:04 +0200 Subject: [PATCH 4/4] isPhp54 is now static. --- src/CallbackHandler.php | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/src/CallbackHandler.php b/src/CallbackHandler.php index 55fd82ed9..9dfa14960 100644 --- a/src/CallbackHandler.php +++ b/src/CallbackHandler.php @@ -41,7 +41,7 @@ class CallbackHandler * PHP version is greater as 5.4rc1? * @var boolean */ - protected $isPhp54; + protected static $isPhp54; /** * Constructor @@ -163,13 +163,13 @@ public function call(array $args = array()) } // Minor performance tweak, if the callback gets called more than once - if (!isset($this->isPhp54)) { - $this->isPhp54 = version_compare(PHP_VERSION, '5.4.0rc1', '>='); + if (!isset(self::$isPhp54)) { + self::$isPhp54 = version_compare(PHP_VERSION, '5.4.0rc1', '>='); } $argCount = count($args); - if ($this->isPhp54 && is_string($callback)) { + if (self::$isPhp54 && is_string($callback)) { $result = $this->validateStringCallbackFor54($callback); if ($result !== true && $argCount <= 3) { @@ -184,19 +184,19 @@ public function call(array $args = array()) // reached switch ($argCount) { case 0: - if ($this->isPhp54) { + if (self::$isPhp54) { return $callback(); } return call_user_func($callback); case 1: - if ($this->isPhp54) { + if (self::$isPhp54) { return $callback(array_shift($args)); } return call_user_func($callback, array_shift($args)); case 2: $arg1 = array_shift($args); $arg2 = array_shift($args); - if ($this->isPhp54) { + if (self::$isPhp54) { return $callback($arg1, $arg2); } return call_user_func($callback, $arg1, $arg2); @@ -204,7 +204,7 @@ public function call(array $args = array()) $arg1 = array_shift($args); $arg2 = array_shift($args); $arg3 = array_shift($args); - if ($this->isPhp54) { + if (self::$isPhp54) { return $callback($arg1, $arg2, $arg3); } return call_user_func($callback, $arg1, $arg2, $arg3);