ConfigFactory: Improve error message for invalid callback
authorUmherirrender <umherirrender_de.wp@web.de>
Sat, 20 Jan 2018 09:44:46 +0000 (10:44 +0100)
committerUmherirrender <umherirrender_de.wp@web.de>
Sat, 20 Jan 2018 09:48:04 +0000 (10:48 +0100)
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
tests/phpunit/includes/config/ConfigFactoryTest.php

index e175765..2c7afda 100644 (file)
@@ -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] );
index 608d8d9..c0e51d7 100644 (file)
@@ -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
         */