r79504 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r79503‎ | r79504 | r79505 >
Date:02:10, 3 January 2011
Author:soxred93
Status:ok (Comments)
Tags:
Comment:
Move wfCreateObject to MWFunction::newObj. This uses the ReflectionClass to
instantiate a variable-length constructor in php 5.1.3 and up, and falls
back to the old, ugly, manual method that was in the old wfCreateObject
function. The instances in the core have been replaced.
Modified paths:
  • /trunk/phase3/includes/GlobalFunctions.php (modified) (history)
  • /trunk/phase3/includes/MWFunction.php (modified) (history)
  • /trunk/phase3/includes/SpecialPage.php (modified) (history)
  • /trunk/phase3/includes/StubObject.php (modified) (history)
  • /trunk/phase3/tests/phpunit/includes/MWFunctionTest.php (modified) (history)

Diff [purge]

Index: trunk/phase3/tests/phpunit/includes/MWFunctionTest.php
@@ -5,30 +5,66 @@
66 function testCallUserFuncWorkarounds() {
77
88 $this->assertEquals(
9 - MWFunction::call( 'MWFunctionTest::someMethod' ),
10 - call_user_func( array( 'MWFunctionTest', 'someMethod' ) )
 9+ call_user_func( array( 'MWFunctionTest', 'someMethod' ) ),
 10+ MWFunction::call( 'MWFunctionTest::someMethod' )
1111 );
1212 $this->assertEquals(
13 - MWFunction::call( 'MWFunctionTest::someMethod', 'foo', 'bar', 'baz' ),
14 - call_user_func( array( 'MWFunctionTest', 'someMethod' ), 'foo', 'bar', 'baz' )
 13+ call_user_func( array( 'MWFunctionTest', 'someMethod' ), 'foo', 'bar', 'baz' ),
 14+ MWFunction::call( 'MWFunctionTest::someMethod', 'foo', 'bar', 'baz' )
1515 );
1616
1717
1818
1919 $this->assertEquals(
20 - MWFunction::callArray( 'MWFunctionTest::someMethod', array() ),
21 - call_user_func_array( array( 'MWFunctionTest', 'someMethod' ), array() )
 20+ call_user_func_array( array( 'MWFunctionTest', 'someMethod' ), array() ),
 21+ MWFunction::callArray( 'MWFunctionTest::someMethod', array() )
2222 );
2323 $this->assertEquals(
24 - MWFunction::callArray( 'MWFunctionTest::someMethod', array( 'foo', 'bar', 'baz' ) ),
25 - call_user_func_array( array( 'MWFunctionTest', 'someMethod' ), array( 'foo', 'bar', 'baz' ) )
 24+ call_user_func_array( array( 'MWFunctionTest', 'someMethod' ), array( 'foo', 'bar', 'baz' ) ),
 25+ MWFunction::callArray( 'MWFunctionTest::someMethod', array( 'foo', 'bar', 'baz' ) )
2626 );
2727
2828 }
2929
 30+ function testNewObjFunction() {
 31+
 32+ $arg1 = 'Foo';
 33+ $arg2 = 'Bar';
 34+ $arg3 = array( 'Baz' );
 35+ $arg4 = new ExampleObject;
 36+
 37+ $args = array( $arg1, $arg2, $arg3, $arg4 );
 38+
 39+ $newObject = new MWBlankClass( $arg1, $arg2, $arg3, $arg4 );
 40+
 41+ $this->assertEquals(
 42+ MWFunction::newObj( 'MWBlankClass', $args )->args,
 43+ $newObject->args
 44+ );
 45+
 46+ $this->assertEquals(
 47+ MWFunction::newObj( 'MWBlankClass', $args, true )->args,
 48+ $newObject->args,
 49+ 'Works even with PHP version < 5.1.3'
 50+ );
 51+
 52+ }
 53+
3054 public static function someMethod() {
3155 return func_get_args();
3256 }
3357
3458 }
3559
 60+class MWBlankClass {
 61+
 62+ public $args = array();
 63+
 64+ function __construct( $arg1, $arg2, $arg3, $arg4 ) {
 65+ $this->args = array( $arg1, $arg2, $arg3, $arg4 );
 66+ }
 67+
 68+}
 69+
 70+class ExampleObject {
 71+}
Index: trunk/phase3/includes/GlobalFunctions.php
@@ -2842,27 +2842,10 @@
28432843 * Create an object with a given name and an array of construct parameters
28442844 * @param $name String
28452845 * @param $p Array: parameters
 2846+ * @deprecated
28462847 */
28472848 function wfCreateObject( $name, $p ) {
2848 - $p = array_values( $p );
2849 - switch ( count( $p ) ) {
2850 - case 0:
2851 - return new $name;
2852 - case 1:
2853 - return new $name( $p[0] );
2854 - case 2:
2855 - return new $name( $p[0], $p[1] );
2856 - case 3:
2857 - return new $name( $p[0], $p[1], $p[2] );
2858 - case 4:
2859 - return new $name( $p[0], $p[1], $p[2], $p[3] );
2860 - case 5:
2861 - return new $name( $p[0], $p[1], $p[2], $p[3], $p[4] );
2862 - case 6:
2863 - return new $name( $p[0], $p[1], $p[2], $p[3], $p[4], $p[5] );
2864 - default:
2865 - throw new MWException( 'Too many arguments to construtor in wfCreateObject' );
2866 - }
 2849+ return MWFunction::newObj( $name, $p );
28672850 }
28682851
28692852 function wfHttpOnlySafe() {
Index: trunk/phase3/includes/MWFunction.php
@@ -42,11 +42,92 @@
4343
4444 }
4545
46 - public static function callArray( $callback, $params ) {
 46+ public static function callArray( $callback, $argsarams ) {
4747
4848 $callback = self::cleanCallback( $callback );
49 - return call_user_func_array( $callback, $params );
 49+ return call_user_func_array( $callback, $argsarams );
5050
5151 }
5252
 53+ public static function newObj( $class, $args = array(), $force_fallback = false ) {
 54+ if( !count( $args ) ) {
 55+ return new $class;
 56+ }
 57+
 58+ if ( version_compare( PHP_VERSION, '5.1.3', '<' ) || $force_fallback ) {
 59+
 60+ //If only MW needed 5.1.3 and up... sigh
 61+
 62+ $args = array_values( $args );
 63+ switch ( count( $args ) ) {
 64+ case 0:
 65+ return new $class;
 66+ case 1:
 67+ return new $class( $args[0] );
 68+ case 2:
 69+ return new $class( $args[0], $args[1] );
 70+ case 3:
 71+ return new $class( $args[0], $args[1], $args[2] );
 72+ case 4:
 73+ return new $class( $args[0], $args[1], $args[2], $args[3] );
 74+ case 5:
 75+ return new $class( $args[0], $args[1], $args[2], $args[3], $args[4] );
 76+ case 6:
 77+ return new $class( $args[0], $args[1], $args[2], $args[3], $args[4], $args[5] );
 78+ case 7:
 79+ return new $class( $args[0], $args[1], $args[2], $args[3], $args[4], $args[5], $args[6] );
 80+ case 8:
 81+ return new $class(
 82+ $args[0], $args[1], $args[2], $args[3], $args[4], $args[5], $args[6],
 83+ $args[7]
 84+ );
 85+ case 9:
 86+ return new $class(
 87+ $args[0], $args[1], $args[2], $args[3], $args[4], $args[5], $args[6],
 88+ $args[7], $args[8]
 89+ );
 90+ case 10:
 91+ return new $class(
 92+ $args[0], $args[1], $args[2], $args[3], $args[4], $args[5], $args[6],
 93+ $args[7], $args[8], $args[9]
 94+ );
 95+ case 11:
 96+ return new $class(
 97+ $args[0], $args[1], $args[2], $args[3], $args[4], $args[5], $args[6],
 98+ $args[7], $args[8], $args[9], $args[10]
 99+ );
 100+ case 12:
 101+ return new $class(
 102+ $args[0], $args[1], $args[2], $args[3], $args[4], $args[5], $args[6],
 103+ $args[7], $args[8], $args[9], $args[10], $args[11]
 104+ );
 105+ case 13:
 106+ return new $class(
 107+ $args[0], $args[1], $args[2], $args[3], $args[4], $args[5], $args[6],
 108+ $args[7], $args[8], $args[9], $args[10], $args[11], $args[12]
 109+ );
 110+ case 14:
 111+ return new $class(
 112+ $args[0], $args[1], $args[2], $args[3], $args[4], $args[5], $args[6],
 113+ $args[7], $args[8], $args[9], $args[10], $args[11], $args[12], $args[13]
 114+ );
 115+ case 15:
 116+ return new $class(
 117+ $args[0], $args[1], $args[2], $args[3], $args[4], $args[5], $args[6],
 118+ $args[7], $args[8], $args[9], $args[10], $args[11], $args[12], $args[13],
 119+ $args[14]
 120+ );
 121+ default:
 122+ throw new MWException( 'Too many arguments to construtor in MWFunction::newObj' );
 123+ }
 124+ }
 125+
 126+ else {
 127+
 128+ $ref = new ReflectionClass($class);
 129+ return $ref->newInstanceArgs($args);
 130+ }
 131+
 132+ }
 133+
53134 }
Index: trunk/phase3/includes/StubObject.php
@@ -62,7 +62,7 @@
6363 * Create a new object to replace this stub object.
6464 */
6565 function _newObject() {
66 - return wfCreateObject( $this->mClass, $this->mParams );
 66+ return MWFunction::newObj( $this->mClass, $this->mParams );
6767 }
6868
6969 /**
Index: trunk/phase3/includes/SpecialPage.php
@@ -407,7 +407,7 @@
408408 self::$mList[$name] = new $className;
409409 } elseif ( is_array( $rec ) ) {
410410 $className = array_shift( $rec );
411 - self::$mList[$name] = wfCreateObject( $className, $rec );
 411+ self::$mList[$name] = MWFunction::newObj( $className, $rec );
412412 }
413413 return self::$mList[$name];
414414 } else {

Comments

#Comment by 😂 (talk | contribs)   22:32, 25 May 2011

Should copy the docs from wfCreateObject() to MWFunction::newObj()

Status & tagging log