Browse Source

Adding get_var_type() to basics.php

This will replace the recurring ternary checks on this.
Florian Krämer 8 years ago
parent
commit
9eb5a52d17

+ 1 - 2
src/Console/Command.php

@@ -113,11 +113,10 @@ class Command
 
         $parser = $this->buildOptionParser($parser);
         if (!($parser instanceof ConsoleOptionParser)) {
-            $actualType = is_object($parser) ? get_class($parser) : gettype($parser);
             throw new RuntimeException(sprintf(
                 "Invalid option parser returned from buildOptionParser(). Expected %s, got %s",
                 ConsoleOptionParser::class,
-                $actualType
+                get_var_type($parser)
             ));
         }
 

+ 1 - 1
src/Console/CommandRunner.php

@@ -119,7 +119,7 @@ class CommandRunner
         ]);
         $commands = $this->app->console($commands);
         if (!($commands instanceof CommandCollection)) {
-            $type = is_object($commands) ? get_class($commands) : gettype($commands);
+            $type = get_var_type($commands);
             throw new RuntimeException(
                 "The application's `console` method did not return a CommandCollection." .
                 " Got '{$type}' instead."

+ 1 - 1
src/Http/Cookie/CookieCollection.php

@@ -183,7 +183,7 @@ class CookieCollection implements IteratorAggregate, Countable
                     sprintf(
                         'Expected `%s[]` as $cookies but instead got `%s` at index %d',
                         static::class,
-                        is_object($cookie) ? get_class($cookie) : gettype($cookie),
+                        get_var_type($cookie),
                         $index
                     )
                 );

+ 1 - 1
src/ORM/Marshaller.php

@@ -247,7 +247,7 @@ class Marshaller
         }
         if (!is_object($options['validate'])) {
             throw new RuntimeException(
-                sprintf('validate must be a boolean, a string or an object. Got %s.', gettype($options['validate']))
+                sprintf('validate must be a boolean, a string or an object. Got %s.', get_var_type($options['validate']))
             );
         }
 

+ 1 - 1
src/View/Form/ContextFactory.php

@@ -146,7 +146,7 @@ class ContextFactory
             throw new RuntimeException(sprintf(
                 'Context providers must return object implementing %s. Got "%s" instead.',
                 ContextInterface::class,
-                is_object($context) ? get_class($context) : gettype($context)
+                get_var_type($context)
             ));
         }
 

+ 13 - 0
src/basics.php

@@ -155,3 +155,16 @@ if (!function_exists('loadPHPUnitAliases')) {
         require_once dirname(__DIR__) . DS . 'tests' . DS . 'phpunit_aliases.php';
     }
 }
+
+if (!function_exists('get_var_type')) {
+    /**
+     * Returns the objects class or var type of it's not an object
+     *
+     * @param mixed $var Variable to check
+     * @return string Returns the class name or variable type
+     */
+    function get_var_type($var)
+    {
+        return is_object($var) ? get_class($var) : gettype($var);
+    }
+}