Browse Source

Merge pull request #11491 from cakephp/master-type-debuginfo

Add DebugInfo and deprecate bad object usage.
Mark Story 8 years ago
parent
commit
52894d7f6b
2 changed files with 76 additions and 25 deletions
  1. 38 22
      src/Database/Type.php
  2. 38 3
      tests/TestCase/Database/TypeTest.php

+ 38 - 22
src/Database/Type.php

@@ -29,7 +29,7 @@ class Type implements TypeInterface
      * identifier is used as key and a complete namespaced class name as value
      * representing the class that will do actual type conversions.
      *
-     * @var array
+     * @var string[]|\Cake\Database\Type[]
      */
     protected static $_types = [
         'tinyinteger' => 'Cake\Database\Type\IntegerType',
@@ -52,7 +52,7 @@ class Type implements TypeInterface
 
     /**
      * List of basic type mappings, used to avoid having to instantiate a class
-     * for doing conversion on these
+     * for doing conversion on these.
      *
      * @var array
      * @deprecated 3.1 All types will now use a specific class
@@ -67,9 +67,9 @@ class Type implements TypeInterface
     ];
 
     /**
-     * Contains a map of type object instances to be reused if needed
+     * Contains a map of type object instances to be reused if needed.
      *
-     * @var array
+     * @var \Cake\Database\Type[]
      */
     protected static $_builtTypes = [];
 
@@ -91,7 +91,7 @@ class Type implements TypeInterface
     }
 
     /**
-     * Returns a Type object capable of converting a type identified by $name
+     * Returns a Type object capable of converting a type identified by name.
      *
      * @param string $name type identifier
      * @throws \InvalidArgumentException If type identifier is unknown
@@ -113,14 +113,14 @@ class Type implements TypeInterface
     }
 
     /**
-     * Returns an arrays with all the mapped type objects, indexed by name
+     * Returns an arrays with all the mapped type objects, indexed by name.
      *
      * @return array
      */
     public static function buildAll()
     {
         $result = [];
-        foreach (self::$_types as $name => $type) {
+        foreach (static::$_types as $name => $type) {
             $result[$name] = isset(static::$_builtTypes[$name]) ? static::$_builtTypes[$name] : static::build($name);
         }
 
@@ -144,27 +144,30 @@ class Type implements TypeInterface
      * If called with no arguments it will return current types map array
      * If $className is omitted it will return mapped class for $type
      *
-     * @param string|array|\Cake\Database\Type|null $type if string name of type to map, if array list of arrays to be mapped
-     * @param string|null $className The classname to register.
-     * @return array|string|null if $type is null then array with current map, if $className is null string
+     * Deprecated: The usage of $type as \Cake\Database\Type[] is deprecated. Please always use string[] if you pass an array
+     * as first argument.
+     *
+     * @param string|string[]|\Cake\Database\Type[]|null $type If string name of type to map, if array list of arrays to be mapped
+     * @param string|\Cake\Database\Type|null $className The classname or object instance of it to register.
+     * @return array|string|null If $type is null then array with current map, if $className is null string
      * configured class name for give $type, null otherwise
      */
     public static function map($type = null, $className = null)
     {
         if ($type === null) {
-            return self::$_types;
+            return static::$_types;
         }
         if (is_array($type)) {
-            self::$_types = $type;
+            static::$_types = $type;
 
             return null;
         }
         if ($className === null) {
-            return isset(self::$_types[$type]) ? self::$_types[$type] : null;
+            return isset(static::$_types[$type]) ? static::$_types[$type] : null;
         }
 
-        self::$_types[$type] = $className;
-        unset(self::$_builtTypes[$type]);
+        static::$_types[$type] = $className;
+        unset(static::$_builtTypes[$type]);
     }
 
     /**
@@ -174,8 +177,8 @@ class Type implements TypeInterface
      */
     public static function clear()
     {
-        self::$_types = [];
-        self::$_builtTypes = [];
+        static::$_types = [];
+        static::$_builtTypes = [];
     }
 
     /**
@@ -205,8 +208,8 @@ class Type implements TypeInterface
     /**
      * Casts given value from a database type to PHP equivalent
      *
-     * @param mixed $value value to be converted to PHP equivalent
-     * @param \Cake\Database\Driver $driver object from which database preferences and configuration will be extracted
+     * @param mixed $value Value to be converted to PHP equivalent
+     * @param \Cake\Database\Driver $driver Object from which database preferences and configuration will be extracted
      * @return mixed
      */
     public function toPHP($value, Driver $driver)
@@ -218,7 +221,7 @@ class Type implements TypeInterface
      * Checks whether this type is a basic one and can be converted using a callback
      * If it is, returns converted value
      *
-     * @param mixed $value value to be converted to PHP equivalent
+     * @param mixed $value Value to be converted to PHP equivalent
      * @return mixed
      * @deprecated 3.1 All types should now be a specific class
      */
@@ -227,8 +230,8 @@ class Type implements TypeInterface
         if ($value === null) {
             return null;
         }
-        if (!empty(self::$_basicTypes[$this->_name])) {
-            $typeInfo = self::$_basicTypes[$this->_name];
+        if (!empty(static::$_basicTypes[$this->_name])) {
+            $typeInfo = static::$_basicTypes[$this->_name];
             if (isset($typeInfo['callback'])) {
                 return $typeInfo['callback']($value);
             }
@@ -300,4 +303,17 @@ class Type implements TypeInterface
     {
         return $this->_basicTypeCast($value);
     }
+
+    /**
+     * Returns an array that can be used to describe the internal state of this
+     * object.
+     *
+     * @return array
+     */
+    public function __debugInfo()
+    {
+        return [
+            'name' => $this->_name,
+        ];
+    }
 }

+ 38 - 3
tests/TestCase/Database/TypeTest.php

@@ -15,6 +15,9 @@
 namespace Cake\Test\TestCase\Database;
 
 use Cake\Database\Type;
+use Cake\Database\Type\BoolType;
+use Cake\Database\Type\IntegerType;
+use Cake\Database\Type\UuidType;
 use Cake\TestSuite\TestCase;
 use PDO;
 use TestApp\Database\Type\BarType;
@@ -149,7 +152,7 @@ class TypeTest extends TestCase
     public function testReMapAndBuild()
     {
         $fooType = FooType::class;
-        $map = Type::map('foo', $fooType);
+        Type::map('foo', $fooType);
         $type = Type::build('foo');
         $this->assertInstanceOf($fooType, $type);
 
@@ -160,6 +163,23 @@ class TypeTest extends TestCase
     }
 
     /**
+     * Tests new types can be registered and built as objects
+     *
+     * @return void
+     */
+    public function testMapAndBuildWithObjects()
+    {
+        $map = Type::map();
+        Type::clear();
+
+        $uuidType = new UuidType('uuid');
+        Type::map('uuid', $uuidType);
+
+        $this->assertSame($uuidType, Type::build('uuid'));
+        Type::map($map);
+    }
+
+    /**
      * Tests clear function in conjunction with map
      *
      * @return void
@@ -174,9 +194,11 @@ class TypeTest extends TestCase
 
         $this->assertEmpty(Type::map());
         Type::map($map);
-        $this->assertEquals($map, Type::map());
+        $newMap = Type::map();
 
-        $this->assertNotSame($type, Type::build('float'));
+        $this->assertEquals(array_keys($map), array_keys($newMap));
+        $this->assertEquals($map['integer'], $newMap['integer']);
+        $this->assertEquals($type, Type::build('float'));
     }
 
     /**
@@ -251,4 +273,17 @@ class TypeTest extends TestCase
         Type::set('random', $instance);
         $this->assertSame($instance, Type::build('random'));
     }
+
+    /**
+     * @return void
+     */
+    public function testDebugInfo()
+    {
+        $type = new Type('foo');
+        $result = $type->__debugInfo();
+        $expected = [
+            'name' => 'foo',
+        ];
+        $this->assertEquals($expected, $result);
+    }
 }