Profiler: Explicitly convert output type to class name
authorKunal Mehta <legoktm@gmail.com>
Tue, 18 Nov 2014 19:12:07 +0000 (11:12 -0800)
committerKunal Mehta <legoktm@gmail.com>
Tue, 18 Nov 2014 19:12:07 +0000 (11:12 -0800)
Follows-up b8d93fb4fd06.

Change-Id: I50faa25fdc4fb980e0ff71014b8d1b2a5818af80

includes/profiler/Profiler.php

index 12e999b..d23c777 100644 (file)
@@ -39,6 +39,15 @@ abstract class Profiler {
        /** @var TransactionProfiler */
        protected $trxProfiler;
 
+       /**
+        * @var array Mapping of output type to class name
+        */
+       private static $outputTypes = array(
+               'db' => 'ProfilerOutputDb',
+               'text' => 'ProfilerOutputText',
+               'udp' => 'ProfilerOutputUdp',
+       );
+
        // @codingStandardsIgnoreStart PSR2.Classes.PropertyDeclaration.Underscore
        /** @var Profiler Do not call this outside Profiler and ProfileSection */
        public static $__instance = null;
@@ -131,6 +140,7 @@ abstract class Profiler {
        /**
         * Log the data to some store or even the page output
         *
+        * @throws MWException
         * @since 1.25
         */
        public function logData() {
@@ -147,7 +157,12 @@ abstract class Profiler {
                }
 
                foreach ( $output as $outType ) {
-                       $class = 'ProfilerOutput' . ucfirst( strtolower( $outType ) );
+                       if ( isset( self::$outputTypes[$outType] ) ) {
+                               $class = self::$outputTypes[$outType];
+                       } else {
+                               throw new MWException( "'$outType' is an invalid output type" );
+                       }
+                       /** @var ProfilerOutput $profileOut */
                        $profileOut = new $class( $this, $this->params );
                        if ( $profileOut->canUse() ) {
                                $profileOut->log();