From: Gergő Tisza Date: Thu, 29 Oct 2015 02:06:37 +0000 (-0700) Subject: Support named arguments in ObjectFactory X-Git-Tag: 1.31.0-rc.0~8899 X-Git-Url: http://git.heureux-cyclage.org/?p=lhc%2Fweb%2Fwiklou.git;a=commitdiff_plain;h=20b7696494572b6ce8b7bd70d63cdc186484d5af Support named arguments in ObjectFactory Arguments are sometimes given as array( 'foo' => 1, 'bar' => 2 ) which makes the configuration self-documenting. Change-Id: I43aa085090f1014ba841641867ebf9559d16e76d --- diff --git a/includes/libs/ObjectFactory.php b/includes/libs/ObjectFactory.php index 61916120fd..bd0da57fdc 100644 --- a/includes/libs/ObjectFactory.php +++ b/includes/libs/ObjectFactory.php @@ -128,8 +128,11 @@ class ObjectFactory { * @return mixed Constructed instance */ public static function constructClassInstance( $clazz, $args ) { + // args are sometimes specified in a 'name' => $value format for readability + $args = array_values( $args ); + // TODO: when PHP min version supported is >=5.6.0 replace this - // function body with `return new $clazz( ... $args );`. + // with `return new $clazz( ... $args );`. $obj = null; switch ( count( $args ) ) { case 0: diff --git a/tests/phpunit/includes/libs/ObjectFactoryTest.php b/tests/phpunit/includes/libs/ObjectFactoryTest.php index 622fce2fdf..633721097b 100644 --- a/tests/phpunit/includes/libs/ObjectFactoryTest.php +++ b/tests/phpunit/includes/libs/ObjectFactoryTest.php @@ -108,6 +108,14 @@ class ObjectFactoryTest extends PHPUnit_Framework_TestCase { '11 args' => array( array( 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, ) ), ); } + + public function testNamedArgs() { + $args = array( 'foo' => 1, 'bar' => 2, 'baz' => 3 ); + $obj = ObjectFactory::constructClassInstance( + 'ObjectFactoryTestFixture', $args + ); + $this->assertSame( array( 1, 2, 3 ), $obj->args ); + } } class ObjectFactoryTestFixture {