From 2b3006b49aed9e920431fa0cf64812ce0d22f23c Mon Sep 17 00:00:00 2001 From: Umherirrender Date: Sat, 20 Jan 2018 10:44:46 +0100 Subject: [PATCH] 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 --- includes/config/ConfigFactory.php | 7 ++++++- tests/phpunit/includes/config/ConfigFactoryTest.php | 9 +++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) 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 */ -- 2.20.1