Browse Source

Merge pull request #11305 from cakephp/3.next-get_var_type

Adding getTypeName() to functions.php
Mark Story 8 years ago
parent
commit
73ac1953d7

+ 1 - 2
src/Console/Command.php

@@ -114,11 +114,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
+                getTypeName($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 = getTypeName($commands);
             throw new RuntimeException(
                 "The application's `console` method did not return a CommandCollection." .
                 " Got '{$type}' instead."

+ 13 - 0
src/Core/functions.php

@@ -280,3 +280,16 @@ if (!function_exists('deprecationWarning')) {
         trigger_error($message, E_USER_DEPRECATED);
     }
 }
+
+if (!function_exists('getTypeName')) {
+    /**
+     * 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 getTypeName($var)
+    {
+        return is_object($var) ? get_class($var) : gettype($var);
+    }
+}

+ 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),
+                        getTypeName($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.', getTypeName($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)
+                getTypeName($context)
             ));
         }
 

+ 3 - 2
tests/TestCase/BasicsTest.php

@@ -576,7 +576,7 @@ EXPECTED;
     public function testEventManagerReset1()
     {
         $eventManager = EventManager::instance();
-        $this->assertInstanceOf('Cake\Event\EventManager', $eventManager);
+        $this->assertInstanceOf(EventManager::class, $eventManager);
 
         return $eventManager;
     }
@@ -589,7 +589,8 @@ EXPECTED;
      */
     public function testEventManagerReset2($prevEventManager)
     {
-        $this->assertInstanceOf('Cake\Event\EventManager', $prevEventManager);
+        $this->assertInstanceOf(EventManager::class, $prevEventManager);
         $this->assertNotSame($prevEventManager, EventManager::instance());
     }
+
 }

+ 12 - 0
tests/TestCase/Core/FunctionsTest.php

@@ -77,4 +77,16 @@ class FunctionsTest extends TestCase
         error_reporting(E_ALL ^ E_USER_DEPRECATED);
         $this->assertNull(deprecationWarning('This is going away'));
     }
+
+    /**
+     * testing getTypeName()
+     *
+     * @return void
+     */
+    public function testgetTypeName()
+    {
+        $this->assertEquals('stdClass', getTypeName(new \stdClass()));
+        $this->assertEquals('array', getTypeName([]));
+        $this->assertEquals('string', getTypeName(''));
+    }
 }