1212namespace CodeIgniter \Database ;
1313
1414use CodeIgniter \Config \BaseConfig ;
15+ use Config \Database as DbConfig ;
1516use 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 )
0 commit comments