Skip to content

Commit 031231e

Browse files
authored
Merge pull request #6743 from kenjis/refactor-Database-connect
refactor: Database connect()
2 parents 7c471f1 + 276aaa4 commit 031231e

File tree

4 files changed

+23
-14
lines changed

4 files changed

+23
-14
lines changed

system/BaseModel.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ abstract class BaseModel
6464
* should be instantiated.
6565
*
6666
* @var string
67+
* @phpstan-var non-empty-string
6768
*/
6869
protected $DBGroup;
6970

system/Database/Config.php

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace CodeIgniter\Database;
1313

1414
use CodeIgniter\Config\BaseConfig;
15+
use Config\Database as DbConfig;
1516
use InvalidArgumentException;
1617

1718
/**
@@ -38,8 +39,10 @@ class Config extends BaseConfig
3839
/**
3940
* Creates the default
4041
*
41-
* @param array|string $group The name of the connection group to use, or an array of configuration settings.
42-
* @param bool $getShared Whether to return a shared instance of the connection.
42+
* @param array|BaseConnection|string|null $group The name of the connection group to use,
43+
* or an array of configuration settings.
44+
* @phpstan-param array|BaseConnection|non-empty-string|null $group
45+
* @param bool $getShared Whether to return a shared instance of the connection.
4346
*
4447
* @return BaseConnection
4548
*/
@@ -53,16 +56,21 @@ public static function connect($group = null, bool $getShared = true)
5356
if (is_array($group)) {
5457
$config = $group;
5558
$group = 'custom-' . md5(json_encode($config));
56-
}
59+
} else {
60+
/** @var DbConfig $dbConfig */
61+
$dbConfig = config('Database');
5762

58-
$config ??= config('Database');
63+
if ($group === null) {
64+
$group = (ENVIRONMENT === 'testing') ? 'tests' : $dbConfig->defaultGroup;
65+
}
5966

60-
if (empty($group)) {
61-
$group = ENVIRONMENT === 'testing' ? 'tests' : $config->defaultGroup;
62-
}
67+
assert(is_string($group));
68+
69+
if (! isset($dbConfig->{$group})) {
70+
throw new InvalidArgumentException($group . ' is not a valid database connection group.');
71+
}
6372

64-
if (is_string($group) && ! isset($config->{$group}) && strpos($group, 'custom-') !== 0) {
65-
throw new InvalidArgumentException($group . ' is not a valid database connection group.');
73+
$config = $dbConfig->{$group};
6674
}
6775

6876
if ($getShared && isset(static::$instances[$group])) {
@@ -71,13 +79,9 @@ public static function connect($group = null, bool $getShared = true)
7179

7280
static::ensureFactory();
7381

74-
if (isset($config->{$group})) {
75-
$config = $config->{$group};
76-
}
77-
7882
$connection = static::$factory->load($config, $group);
7983

80-
static::$instances[$group] = &$connection;
84+
static::$instances[$group] = $connection;
8185

8286
return $connection;
8387
}
@@ -122,6 +126,8 @@ public static function utils($group = null)
122126
/**
123127
* Returns a new instance of the Database Seeder.
124128
*
129+
* @phpstan-param null|non-empty-string $group
130+
*
125131
* @return Seeder
126132
*/
127133
public static function seeder(?string $group = null)

system/Database/Seeder.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ class Seeder
2626
* The name of the database group to use.
2727
*
2828
* @var string
29+
* @phpstan-var non-empty-string
2930
*/
3031
protected $DBGroup;
3132

system/Test/CIUnitTestCase.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,7 @@ abstract class CIUnitTestCase extends TestCase
134134
* If not present, will use the defaultGroup.
135135
*
136136
* @var string
137+
* @phpstan-var non-empty-string
137138
*/
138139
protected $DBGroup = 'tests';
139140

0 commit comments

Comments
 (0)