From: Umherirrender Date: Sat, 20 Jan 2018 09:44:46 +0000 (+0100) Subject: ConfigFactory: Improve error message for invalid callback X-Git-Tag: 1.31.0-rc.0~863 X-Git-Url: https://git.cyclocoop.org/%242?a=commitdiff_plain;h=2b3006b49aed9e920431fa0cf64812ce0d22f23c;p=lhc%2Fweb%2Fwiklou.git ConfigFactory: Improve error message for invalid callback Getting the following error for an invalid callback in extension registration is not helpful: Fatal error: Uncaught exception 'InvalidArgumentException' with message 'Invalid callback provided' in /includes/config/ConfigFactory.php:108 Changed message to Invalid callback '$1' provided Added a test case for the instanceof part of the if Change-Id: I425e2607b651c666336289c2c0d93730bb6312ed --- diff --git a/includes/config/ConfigFactory.php b/includes/config/ConfigFactory.php index e175765431..2c7afdae07 100644 --- a/includes/config/ConfigFactory.php +++ b/includes/config/ConfigFactory.php @@ -105,7 +105,12 @@ class ConfigFactory implements SalvageableService { */ public function register( $name, $callback ) { if ( !is_callable( $callback ) && !( $callback instanceof Config ) ) { - throw new InvalidArgumentException( 'Invalid callback provided' ); + if ( is_array( $callback ) ) { + $callback = '[ ' . implode( ', ', $callback ) . ' ]'; + } elseif ( is_object( $callback ) ) { + $callback = 'instanceof ' . get_class( $callback ); + } + throw new InvalidArgumentException( 'Invalid callback \'' . $callback . '\' provided' ); } unset( $this->configs[$name] ); diff --git a/tests/phpunit/includes/config/ConfigFactoryTest.php b/tests/phpunit/includes/config/ConfigFactoryTest.php index 608d8d949d..c0e51d7e2d 100644 --- a/tests/phpunit/includes/config/ConfigFactoryTest.php +++ b/tests/phpunit/includes/config/ConfigFactoryTest.php @@ -22,6 +22,15 @@ class ConfigFactoryTest extends MediaWikiTestCase { $factory->register( 'invalid', 'Invalid callback' ); } + /** + * @covers ConfigFactory::register + */ + public function testRegisterInvalidInstance() { + $factory = new ConfigFactory(); + $this->setExpectedException( InvalidArgumentException::class ); + $factory->register( 'invalidInstance', new stdClass ); + } + /** * @covers ConfigFactory::register */