From 0befd64babcf64154daebc9c16d05df1634e0045 Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Wed, 14 Jan 2015 01:05:12 +0700 Subject: [PATCH 1/2] remove $self = $this; use ($self) in 5.4 --- library/Zend/Cache/Pattern/CaptureCache.php | 5 ++--- .../Driver/Pdo/Feature/OracleRowCounter.php | 7 +++---- .../Driver/Pdo/Feature/SqliteRowCounter.php | 7 +++---- .../Zend/ServiceManager/AbstractPluginManager.php | 5 ++--- library/Zend/ServiceManager/ServiceManager.php | 13 +++++-------- library/Zend/Session/AbstractContainer.php | 14 ++++---------- tests/ZendTest/Db/Sql/DeleteTest.php | 5 ++--- .../ZendTest/Db/Sql/Predicate/PredicateSetTest.php | 5 ++--- tests/ZendTest/Db/Sql/SelectTest.php | 5 ++--- tests/ZendTest/Db/Sql/UpdateTest.php | 5 ++--- .../EventManager/AbstractListenerAggregateTest.php | 5 ++--- .../EventManager/ListenerAggregateTraitTest.php | 5 ++--- tests/ZendTest/Log/LoggerTest.php | 10 ++++------ tests/ZendTest/Mail/Transport/SendmailTest.php | 13 ++++++------- .../Listener/LocatorRegistrationListenerTest.php | 5 ++--- tests/ZendTest/Mvc/ApplicationTest.php | 10 ++++------ .../Mvc/Service/ServiceManagerConfigTest.php | 5 ++--- .../ZendTest/ServiceManager/ServiceManagerTest.php | 12 +++++------- tests/ZendTest/Soap/Client/DotNetTest.php | 5 ++--- 19 files changed, 56 insertions(+), 85 deletions(-) diff --git a/library/Zend/Cache/Pattern/CaptureCache.php b/library/Zend/Cache/Pattern/CaptureCache.php index f74b2161999..fb787e5a87c 100644 --- a/library/Zend/Cache/Pattern/CaptureCache.php +++ b/library/Zend/Cache/Pattern/CaptureCache.php @@ -26,9 +26,8 @@ public function start($pageId = null) $pageId = $this->detectPageId(); } - $that = $this; - ob_start(function ($content) use ($that, $pageId) { - $that->set($content, $pageId); + ob_start(function ($content) use ($pageId) { + $this->set($content, $pageId); // http://php.net/manual/function.ob-start.php // -> If output_callback returns FALSE original input is sent to the browser. diff --git a/library/Zend/Db/Adapter/Driver/Pdo/Feature/OracleRowCounter.php b/library/Zend/Db/Adapter/Driver/Pdo/Feature/OracleRowCounter.php index 199949a9823..2f4240ac910 100644 --- a/library/Zend/Db/Adapter/Driver/Pdo/Feature/OracleRowCounter.php +++ b/library/Zend/Db/Adapter/Driver/Pdo/Feature/OracleRowCounter.php @@ -67,12 +67,11 @@ public function getCountForSql($sql) */ public function getRowCountClosure($context) { - $oracleRowCounter = $this; - return function () use ($oracleRowCounter, $context) { + return function () use ($context) { /** @var $oracleRowCounter OracleRowCounter */ return ($context instanceof Pdo\Statement) - ? $oracleRowCounter->getCountForStatement($context) - : $oracleRowCounter->getCountForSql($context); + ? $this->getCountForStatement($context) + : $this->getCountForSql($context); }; } } diff --git a/library/Zend/Db/Adapter/Driver/Pdo/Feature/SqliteRowCounter.php b/library/Zend/Db/Adapter/Driver/Pdo/Feature/SqliteRowCounter.php index 9f795fef6d2..67b539c9a37 100644 --- a/library/Zend/Db/Adapter/Driver/Pdo/Feature/SqliteRowCounter.php +++ b/library/Zend/Db/Adapter/Driver/Pdo/Feature/SqliteRowCounter.php @@ -67,12 +67,11 @@ public function getCountForSql($sql) */ public function getRowCountClosure($context) { - $sqliteRowCounter = $this; - return function () use ($sqliteRowCounter, $context) { + return function () use ($context) { /** @var $sqliteRowCounter SqliteRowCounter */ return ($context instanceof Pdo\Statement) - ? $sqliteRowCounter->getCountForStatement($context) - : $sqliteRowCounter->getCountForSql($context); + ? $this->getCountForStatement($context) + : $this->getCountForSql($context); }; } } diff --git a/library/Zend/ServiceManager/AbstractPluginManager.php b/library/Zend/ServiceManager/AbstractPluginManager.php index 4f1341456e1..7fa0737888c 100644 --- a/library/Zend/ServiceManager/AbstractPluginManager.php +++ b/library/Zend/ServiceManager/AbstractPluginManager.php @@ -62,10 +62,9 @@ abstract class AbstractPluginManager extends ServiceManager implements ServiceLo public function __construct(ConfigInterface $configuration = null) { parent::__construct($configuration); - $self = $this; - $this->addInitializer(function ($instance) use ($self) { + $this->addInitializer(function ($instance) { if ($instance instanceof ServiceLocatorAwareInterface) { - $instance->setServiceLocator($self); + $instance->setServiceLocator($this); } }); } diff --git a/library/Zend/ServiceManager/ServiceManager.php b/library/Zend/ServiceManager/ServiceManager.php index f48a1bbc88d..953b1748d55 100644 --- a/library/Zend/ServiceManager/ServiceManager.php +++ b/library/Zend/ServiceManager/ServiceManager.php @@ -605,12 +605,10 @@ public function create($name) */ private function createDelegatorCallback($delegatorFactory, $rName, $cName, $creationCallback) { - $serviceManager = $this; - - return function () use ($serviceManager, $delegatorFactory, $rName, $cName, $creationCallback) { + return function () use ($delegatorFactory, $rName, $cName, $creationCallback) { return $delegatorFactory instanceof DelegatorFactoryInterface - ? $delegatorFactory->createDelegatorWithName($serviceManager, $cName, $rName, $creationCallback) - : $delegatorFactory($serviceManager, $cName, $rName, $creationCallback); + ? $delegatorFactory->createDelegatorWithName($this, $cName, $rName, $creationCallback) + : $delegatorFactory($this, $cName, $rName, $creationCallback); }; } @@ -1144,10 +1142,9 @@ protected function checkNestedContextStop($force = false) */ protected function createDelegatorFromFactory($canonicalName, $requestedName) { - $serviceManager = $this; $delegatorsCount = count($this->delegators[$canonicalName]); - $creationCallback = function () use ($serviceManager, $requestedName, $canonicalName) { - return $serviceManager->doCreate($requestedName, $canonicalName); + $creationCallback = function () use ($requestedName, $canonicalName) { + return $this->doCreate($requestedName, $canonicalName); }; for ($i = 0; $i < $delegatorsCount; $i += 1) { diff --git a/library/Zend/Session/AbstractContainer.php b/library/Zend/Session/AbstractContainer.php index 9c48c080ebf..affa9760847 100644 --- a/library/Zend/Session/AbstractContainer.php +++ b/library/Zend/Session/AbstractContainer.php @@ -518,12 +518,9 @@ public function setExpirationSeconds($ttl, $vars = null) $this->expireKeys(); // first we need to expire global key, since it can already be expired $data = array('EXPIRE' => $ts); } elseif (is_array($vars)) { - // Cannot pass "$this" to a lambda - $container = $this; - // Filter out any items not in our container - $expires = array_filter($vars, function ($value) use ($container) { - return $container->offsetExists($value); + $expires = array_filter($vars, function ($value) { + return $this->offsetExists($value); }); // Map item keys => timestamp @@ -569,12 +566,9 @@ public function setExpirationHops($hops, $vars = null) $this->expireKeys(); // first we need to expire global key, since it can already be expired $data = array('EXPIRE_HOPS' => array('hops' => $hops, 'ts' => $ts)); } elseif (is_array($vars)) { - // Cannot pass "$this" to a lambda - $container = $this; - // FilterInterface out any items not in our container - $expires = array_filter($vars, function ($value) use ($container) { - return $container->offsetExists($value); + $expires = array_filter($vars, function ($value) { + return $this->offsetExists($value); }); // Map item keys => timestamp diff --git a/tests/ZendTest/Db/Sql/DeleteTest.php b/tests/ZendTest/Db/Sql/DeleteTest.php index 81c854de880..045cb5279aa 100644 --- a/tests/ZendTest/Db/Sql/DeleteTest.php +++ b/tests/ZendTest/Db/Sql/DeleteTest.php @@ -100,9 +100,8 @@ public function testWhere() $this->delete->where($where); $this->assertSame($where, $this->delete->where); - $test = $this; - $this->delete->where(function ($what) use ($test, $where) { - $test->assertSame($where, $what); + $this->delete->where(function ($what) use ($where) { + $this->assertSame($where, $what); }); } diff --git a/tests/ZendTest/Db/Sql/Predicate/PredicateSetTest.php b/tests/ZendTest/Db/Sql/Predicate/PredicateSetTest.php index 2dab121a7b9..c82780db052 100644 --- a/tests/ZendTest/Db/Sql/Predicate/PredicateSetTest.php +++ b/tests/ZendTest/Db/Sql/Predicate/PredicateSetTest.php @@ -122,9 +122,8 @@ public function testAddPredicates() $this->assertEquals('AND', $predicates[6][0]); $this->assertInstanceOf('Zend\Db\Sql\Predicate\IsNotNull', $predicates[6][1]); - $test = $this; - $predicateSet->addPredicates(function ($what) use ($test, $predicateSet) { - $test->assertSame($predicateSet, $what); + $predicateSet->addPredicates(function ($what) use ($predicateSet) { + $this->assertSame($predicateSet, $what); }); $this->setExpectedException('Zend\Db\Sql\Exception\InvalidArgumentException', 'Predicate cannot be null'); diff --git a/tests/ZendTest/Db/Sql/SelectTest.php b/tests/ZendTest/Db/Sql/SelectTest.php index ce8d4043f81..eea00c842e0 100644 --- a/tests/ZendTest/Db/Sql/SelectTest.php +++ b/tests/ZendTest/Db/Sql/SelectTest.php @@ -317,9 +317,8 @@ public function testWhereArgument1IsClosure() $select = new Select; $where = $select->getRawState('where'); - $test = $this; - $select->where(function ($what) use ($test, $where) { - $test->assertSame($where, $what); + $select->where(function ($what) use ($where) { + $this->assertSame($where, $what); }); } diff --git a/tests/ZendTest/Db/Sql/UpdateTest.php b/tests/ZendTest/Db/Sql/UpdateTest.php index afbcccaa6c3..8114445199d 100644 --- a/tests/ZendTest/Db/Sql/UpdateTest.php +++ b/tests/ZendTest/Db/Sql/UpdateTest.php @@ -131,9 +131,8 @@ public function testWhere() $this->update->where($where); $this->assertSame($where, $this->update->where); - $test = $this; - $this->update->where(function ($what) use ($test, $where) { - $test->assertSame($where, $what); + $this->update->where(function ($what) use ($where) { + $this->assertSame($where, $what); }); $this->setExpectedException('Zend\Db\Sql\Exception\InvalidArgumentException', 'Predicate cannot be null'); diff --git a/tests/ZendTest/EventManager/AbstractListenerAggregateTest.php b/tests/ZendTest/EventManager/AbstractListenerAggregateTest.php index 35a82ed55a8..ebaeb53ce60 100644 --- a/tests/ZendTest/EventManager/AbstractListenerAggregateTest.php +++ b/tests/ZendTest/EventManager/AbstractListenerAggregateTest.php @@ -37,13 +37,12 @@ public function testDetach() $eventManager = $this->getMock('Zend\\EventManager\\EventManagerInterface'); $unrelatedEventManager = $this->getMock('Zend\\EventManager\\EventManagerInterface'); $callbackHandlers = array(); - $test = $this; $eventManager ->expects($this->exactly(2)) ->method('attach') - ->will($this->returnCallback(function () use (&$callbackHandlers, $test) { - return $callbackHandlers[] = $test->getMock('Zend\\Stdlib\\CallbackHandler', array(), array(), '', false); + ->will($this->returnCallback(function () use (&$callbackHandlers) { + return $callbackHandlers[] = $this->getMock('Zend\\Stdlib\\CallbackHandler', array(), array(), '', false); })); $this->listener->attach($eventManager); diff --git a/tests/ZendTest/EventManager/ListenerAggregateTraitTest.php b/tests/ZendTest/EventManager/ListenerAggregateTraitTest.php index 334278c18d3..95209573651 100644 --- a/tests/ZendTest/EventManager/ListenerAggregateTraitTest.php +++ b/tests/ZendTest/EventManager/ListenerAggregateTraitTest.php @@ -22,13 +22,12 @@ public function testDetach() $eventManager = $this->getMock('Zend\\EventManager\\EventManagerInterface'); $unrelatedEventManager = $this->getMock('Zend\\EventManager\\EventManagerInterface'); $callbackHandlers = array(); - $test = $this; $eventManager ->expects($this->exactly(2)) ->method('attach') - ->will($this->returnCallback(function () use (&$callbackHandlers, $test) { - return $callbackHandlers[] = $test->getMock('Zend\\Stdlib\\CallbackHandler', array(), array(), '', false); + ->will($this->returnCallback(function () use (&$callbackHandlers) { + return $callbackHandlers[] = $this->getMock('Zend\\Stdlib\\CallbackHandler', array(), array(), '', false); })); $listener->attach($eventManager); diff --git a/tests/ZendTest/Log/LoggerTest.php b/tests/ZendTest/Log/LoggerTest.php index 70836126396..f60c2c0ab93 100644 --- a/tests/ZendTest/Log/LoggerTest.php +++ b/tests/ZendTest/Log/LoggerTest.php @@ -452,9 +452,8 @@ public function testRegisterFatalShutdownFunction() // check for single error handler instance $this->assertFalse(Logger::registerFatalErrorShutdownFunction($this->logger)); - $self = $this; - register_shutdown_function(function () use ($writer, $self) { - $self->assertEquals( + register_shutdown_function(function () use ($writer) { + $this->assertEquals( 'Call to undefined method ZendTest\Log\LoggerTest::callToNonExistingMethod()', $writer->events[0]['message'] ); @@ -481,9 +480,8 @@ public function testRegisterFatalErrorShutdownFunctionHandlesCompileTimeErrors() // check for single error handler instance $this->assertFalse(Logger::registerFatalErrorShutdownFunction($this->logger)); - $self = $this; - register_shutdown_function(function () use ($writer, $self) { - $self->assertStringMatchesFormat( + register_shutdown_function(function () use ($writer) { + $this->assertStringMatchesFormat( 'syntax error%A', $writer->events[0]['message'] ); diff --git a/tests/ZendTest/Mail/Transport/SendmailTest.php b/tests/ZendTest/Mail/Transport/SendmailTest.php index 8e8f75037dd..3bef8e9af58 100644 --- a/tests/ZendTest/Mail/Transport/SendmailTest.php +++ b/tests/ZendTest/Mail/Transport/SendmailTest.php @@ -27,13 +27,12 @@ class SendmailTest extends \PHPUnit_Framework_TestCase public function setUp() { $this->transport = new Sendmail(); - $self = $this; - $this->transport->setCallable(function ($to, $subject, $message, $additional_headers, $additional_parameters = null) use ($self) { - $self->to = $to; - $self->subject = $subject; - $self->message = $message; - $self->additional_headers = $additional_headers; - $self->additional_parameters = $additional_parameters; + $this->transport->setCallable(function ($to, $subject, $message, $additional_headers, $additional_parameters = null) { + $this->to = $to; + $this->subject = $subject; + $this->message = $message; + $this->additional_headers = $additional_headers; + $this->additional_parameters = $additional_parameters; }); $this->operating_system = strtoupper(substr(PHP_OS, 0, 3)); } diff --git a/tests/ZendTest/ModuleManager/Listener/LocatorRegistrationListenerTest.php b/tests/ZendTest/ModuleManager/Listener/LocatorRegistrationListenerTest.php index af8f1c9d0ef..f91a89e131e 100644 --- a/tests/ZendTest/ModuleManager/Listener/LocatorRegistrationListenerTest.php +++ b/tests/ZendTest/ModuleManager/Listener/LocatorRegistrationListenerTest.php @@ -93,9 +93,8 @@ public function testModuleClassIsRegisteredWithDiAndInjectedWithSharedInstances( $locatorRegistrationListener = new LocatorRegistrationListener; $this->moduleManager->getEventManager()->attachAggregate($locatorRegistrationListener); - $test = $this; - $this->moduleManager->getEventManager()->attach(ModuleEvent::EVENT_LOAD_MODULE, function ($e) use ($test) { - $test->module = $e->getModule(); + $this->moduleManager->getEventManager()->attach(ModuleEvent::EVENT_LOAD_MODULE, function ($e) { + $this->module = $e->getModule(); }, -1000); $this->moduleManager->loadModules(); diff --git a/tests/ZendTest/Mvc/ApplicationTest.php b/tests/ZendTest/Mvc/ApplicationTest.php index e96a7e0e0e7..6c1ddc48755 100644 --- a/tests/ZendTest/Mvc/ApplicationTest.php +++ b/tests/ZendTest/Mvc/ApplicationTest.php @@ -581,10 +581,9 @@ public function testReturnsResponseFromListenerWhenRouteEventShortCircuits() return $testResponse; }, 100); - $self = $this; $triggered = false; - $events->attach(MvcEvent::EVENT_FINISH, function ($e) use ($self, $testResponse, &$triggered) { - $self->assertSame($testResponse, $e->getResponse()); + $events->attach(MvcEvent::EVENT_FINISH, function ($e) use ($testResponse, &$triggered) { + $this->assertSame($testResponse, $e->getResponse()); $triggered = true; }); @@ -607,10 +606,9 @@ public function testReturnsResponseFromListenerWhenDispatchEventShortCircuits() return $testResponse; }, 100); - $self = $this; $triggered = false; - $events->attach(MvcEvent::EVENT_FINISH, function ($e) use ($self, $testResponse, &$triggered) { - $self->assertSame($testResponse, $e->getResponse()); + $events->attach(MvcEvent::EVENT_FINISH, function ($e) use ($testResponse, &$triggered) { + $this->assertSame($testResponse, $e->getResponse()); $triggered = true; }); diff --git a/tests/ZendTest/Mvc/Service/ServiceManagerConfigTest.php b/tests/ZendTest/Mvc/Service/ServiceManagerConfigTest.php index d4795d89736..49c12e658df 100644 --- a/tests/ZendTest/Mvc/Service/ServiceManagerConfigTest.php +++ b/tests/ZendTest/Mvc/Service/ServiceManagerConfigTest.php @@ -150,11 +150,10 @@ public function testDefinesServiceManagerService() */ public function testCanOverrideServiceManager() { - $test = $this; $serviceManager = new ServiceManager(new ServiceManagerConfig(array( 'factories' => array( - 'ServiceManager' => function () use ($test) { - return $test; + 'ServiceManager' => function () { + return $this; } ), ))); diff --git a/tests/ZendTest/ServiceManager/ServiceManagerTest.php b/tests/ZendTest/ServiceManager/ServiceManagerTest.php index 948ae18224f..857b74ed08a 100644 --- a/tests/ZendTest/ServiceManager/ServiceManagerTest.php +++ b/tests/ZendTest/ServiceManager/ServiceManagerTest.php @@ -715,15 +715,13 @@ public function testGetGlobIteratorServiceWorksProperly() public function duplicateService() { - $self = $this; - return array( array( 'setFactory', - function ($services) use ($self) { - return $self; + function ($services) { + return $this; }, - $self, + $this, 'assertSame', ), array( @@ -734,8 +732,8 @@ function ($services) use ($self) { ), array( 'setService', - $self, - $self, + $this, + $this, 'assertSame', ), ); diff --git a/tests/ZendTest/Soap/Client/DotNetTest.php b/tests/ZendTest/Soap/Client/DotNetTest.php index ed6cc9e2825..153eba98c22 100644 --- a/tests/ZendTest/Soap/Client/DotNetTest.php +++ b/tests/ZendTest/Soap/Client/DotNetTest.php @@ -92,10 +92,9 @@ public function testCurlClientRequestIsDoneWhenUsingNtlmAuthentication() */ public function testDefaultSoapClientRequestIsDoneWhenNotUsingNtlmAuthentication() { - $unitTest = $this; $soapClient = new Common( - function (Common $client, $request, $location, $action, $version, $oneWay = null) use ($unitTest) { - $unitTest->assertEquals('http://unit/test#TestMethod', $action); + function (Common $client, $request, $location, $action, $version, $oneWay = null) { + $this->assertEquals('http://unit/test#TestMethod', $action); $result = '' . ''; From 68a626a6bc7dd28356340af4d004a2a70ef686ca Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Wed, 14 Jan 2015 02:53:57 +0700 Subject: [PATCH 2/2] rollback use $this in createDelegatorFromFactory --- library/Zend/ServiceManager/ServiceManager.php | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/library/Zend/ServiceManager/ServiceManager.php b/library/Zend/ServiceManager/ServiceManager.php index 953b1748d55..1205a684b39 100644 --- a/library/Zend/ServiceManager/ServiceManager.php +++ b/library/Zend/ServiceManager/ServiceManager.php @@ -1142,9 +1142,10 @@ protected function checkNestedContextStop($force = false) */ protected function createDelegatorFromFactory($canonicalName, $requestedName) { + $serviceManager = $this; $delegatorsCount = count($this->delegators[$canonicalName]); - $creationCallback = function () use ($requestedName, $canonicalName) { - return $this->doCreate($requestedName, $canonicalName); + $creationCallback = function () use ($serviceManager, $requestedName, $canonicalName) { + return $serviceManager->doCreate($requestedName, $canonicalName); }; for ($i = 0; $i < $delegatorsCount; $i += 1) {