jenkins-bot has submitted this change and it was merged.

Change subject: Support named arguments in ObjectFactory
......................................................................


Support named arguments in ObjectFactory

Arguments are sometimes given as array( 'foo' => 1, 'bar' => 2 )
which makes the configuration self-documenting.

Change-Id: I43aa085090f1014ba841641867ebf9559d16e76d
---
M includes/libs/ObjectFactory.php
M tests/phpunit/includes/libs/ObjectFactoryTest.php
2 files changed, 12 insertions(+), 1 deletion(-)

Approvals:
  BryanDavis: Looks good to me, approved
  jenkins-bot: Verified



diff --git a/includes/libs/ObjectFactory.php b/includes/libs/ObjectFactory.php
index 6191612..bd0da57 100644
--- a/includes/libs/ObjectFactory.php
+++ b/includes/libs/ObjectFactory.php
@@ -128,8 +128,11 @@
         * @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 622fce2..6337210 100644
--- a/tests/phpunit/includes/libs/ObjectFactoryTest.php
+++ b/tests/phpunit/includes/libs/ObjectFactoryTest.php
@@ -108,6 +108,14 @@
                        '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 {

-- 
To view, visit https://gerrit.wikimedia.org/r/249679
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: merged
Gerrit-Change-Id: I43aa085090f1014ba841641867ebf9559d16e76d
Gerrit-PatchSet: 4
Gerrit-Project: mediawiki/core
Gerrit-Branch: master
Gerrit-Owner: Gergő Tisza <gti...@wikimedia.org>
Gerrit-Reviewer: Anomie <bjor...@wikimedia.org>
Gerrit-Reviewer: BryanDavis <bda...@wikimedia.org>
Gerrit-Reviewer: jenkins-bot <>

_______________________________________________
MediaWiki-commits mailing list
MediaWiki-commits@lists.wikimedia.org
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to