Browse Source

Optimize according to the code review

Val Bancer 6 years ago
parent
commit
2085d4e919

+ 2 - 2
src/Database/Driver.php

@@ -432,9 +432,9 @@ abstract class Driver implements DriverInterface
      *
      * @param string $table The table name.
      * @param array $columns The list of columns for the schema.
-     * @return \Cake\Database\Schema\TableSchema
+     * @return \Cake\Database\Schema\TableSchemaInterface
      */
-    public function newTableSchema($table, $columns = [])
+    public function newTableSchema($table, array $columns = [])
     {
         return new TableSchema($table, $columns);
     }

+ 1 - 0
src/Datasource/ConnectionInterface.php

@@ -18,6 +18,7 @@ namespace Cake\Datasource;
  * This interface defines the methods you can depend on in
  * a connection.
  *
+ * @method object getDriver() Gets the driver instance.
  * @method object getLogger() Get the current logger instance
  * @method $this setLogger($logger) Set the current logger.
  * @method bool supportsDynamicConstraints()

+ 4 - 5
src/ORM/Table.php

@@ -524,6 +524,9 @@ class Table implements RepositoryInterface, EventListenerInterface, EventDispatc
      */
     public function getConnection()
     {
+        if (!$this->_connection) {
+            $this->_connection = ConnectionManager::get(static::defaultConnectionName());
+        }
         return $this->_connection;
     }
 
@@ -584,11 +587,7 @@ class Table implements RepositoryInterface, EventListenerInterface, EventDispatc
                 unset($schema['_constraints']);
             }
 
-            $connection = $this->getConnection();
-            if ($connection === null) {
-                $connection = ConnectionManager::get(static::defaultConnectionName());
-            }
-            $schema = $connection->getDriver()->newTableSchema($this->getTable(), $schema);
+            $schema = $this->getConnection()->getDriver()->newTableSchema($this->getTable(), $schema);
 
             foreach ($constraints as $name => $value) {
                 $schema->addConstraint($name, $value);

+ 0 - 2
tests/TestCase/ORM/TableTest.php

@@ -248,7 +248,6 @@ class TableTest extends TestCase
     {
         $this->deprecated(function () {
             $table = new Table(['table' => 'users']);
-            $this->assertNull($table->connection());
             $table->connection($this->connection);
             $this->assertSame($this->connection, $table->connection());
         });
@@ -262,7 +261,6 @@ class TableTest extends TestCase
     public function testSetConnection()
     {
         $table = new Table(['table' => 'users']);
-        $this->assertNull($table->getConnection());
         $this->assertSame($table, $table->setConnection($this->connection));
         $this->assertSame($this->connection, $table->getConnection());
     }