Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 8 additions & 6 deletions system/Database/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class Config extends BaseConfig
* Cache for instance of any connections that
* have been requested as a "shared" instance.
*
* @var array
* @var array<string, BaseConnection>
*/
protected static $instances = [];

Expand All @@ -41,9 +41,9 @@ class Config extends BaseConfig
/**
* Returns the database connection
*
* @param array|BaseConnection|non-empty-string|null $group The name of the connection group to use,
* or an array of configuration settings.
* @param bool $getShared Whether to return a shared instance of the connection.
* @param array<string, mixed>|BaseConnection|non-empty-string|null $group The name of the connection group to use,
* or an array of configuration settings.
* @param bool $getShared Whether to return a shared instance of the connection.
*
* @return BaseConnection
*/
Expand Down Expand Up @@ -90,6 +90,8 @@ public static function connect($group = null, bool $getShared = true)

/**
* Returns an array of all db connections currently made.
*
* @return array<string, BaseConnection>
*/
public static function getConnections(): array
{
Expand All @@ -100,7 +102,7 @@ public static function getConnections(): array
* Loads and returns an instance of the Forge for the specified
* database group, and loads the group if it hasn't been loaded yet.
*
* @param array|ConnectionInterface|string|null $group
* @param array<string, mixed>|ConnectionInterface|string|null $group
*
* @return Forge
*/
Expand All @@ -114,7 +116,7 @@ public static function forge($group = null)
/**
* Returns a new instance of the Database Utilities class.
*
* @param array|string|null $group
* @param array<string, mixed>|string|null $group
*
* @return BaseUtils
*/
Expand Down
14 changes: 10 additions & 4 deletions system/Database/Database.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,15 @@ class Database
* Helps to keep track of all open connections for performance
* monitoring, logging, etc.
*
* @var array
* @var array<string, BaseConnection>
*/
protected $connections = [];

/**
* Parses the connection binds and creates a Database Connection instance.
*
* @param array<string, mixed> $params
*
* @return BaseConnection
*
* @throws InvalidArgumentException
Expand Down Expand Up @@ -94,6 +96,10 @@ public function loadUtils(ConnectionInterface $db): BaseUtils
/**
* Parses universal DSN string
*
* @param array<string, mixed> $params
*
* @return array<string, mixed>
*
* @throws InvalidArgumentException
*/
protected function parseDSN(array $params): array
Expand Down Expand Up @@ -132,9 +138,9 @@ protected function parseDSN(array $params): array
/**
* Creates a database object.
*
* @param string $driver Driver name. FQCN can be used.
* @param string $class 'Connection'|'Forge'|'Utils'
* @param array|ConnectionInterface $argument The constructor parameter or DB connection
* @param string $driver Driver name. FQCN can be used.
* @param string $class 'Connection'|'Forge'|'Utils'
* @param array<string, mixed>|ConnectionInterface $argument The constructor parameter or DB connection
*
* @return BaseConnection|BaseUtils|Forge
*/
Expand Down
16 changes: 13 additions & 3 deletions system/Database/MigrationRunner.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ class MigrationRunner
/**
* used to return messages for CLI.
*
* @var array
* @var list<string>
*/
protected $cliMessages = [];

Expand Down Expand Up @@ -139,7 +139,7 @@ class MigrationRunner
* default DB group so that it creates the `migrations` table in the default
* DB group. Therefore, passing $db is for testing purposes only.
*
* @param array|ConnectionInterface|string|null $db DB group. For testing purposes only.
* @param array<string, mixed>|ConnectionInterface|string|null $db DB group. For testing purposes only.
*
* @throws ConfigException
*/
Expand Down Expand Up @@ -440,7 +440,7 @@ public function force(string $path, string $namespace, ?string $group = null)
/**
* Retrieves list of available migration scripts
*
* @return array List of all located migrations by their UID
* @return array<string, stdClass> List of all located migrations by their UID
*/
public function findMigrations(): array
{
Expand All @@ -465,6 +465,8 @@ public function findMigrations(): array

/**
* Retrieves a list of available migration scripts for one namespace
*
* @return list<stdClass>
*/
public function findNamespaceMigrations(string $namespace): array
{
Expand Down Expand Up @@ -610,6 +612,8 @@ public function getObjectUid($object): string

/**
* Retrieves messages formatted for CLI output
*
* @return list<string>
*/
public function getCliMessages(): array
{
Expand Down Expand Up @@ -693,6 +697,8 @@ protected function removeHistory($history)

/**
* Grabs the full migration history from the database for a group
*
* @return list<stdClass>
*/
public function getHistory(string $group = 'default'): array
{
Expand All @@ -719,6 +725,8 @@ public function getHistory(string $group = 'default'): array
* Returns the migration history for a single batch.
*
* @param string $order
*
* @return list<stdClass>
*/
public function getBatchHistory(int $batch, $order = 'asc'): array
{
Expand All @@ -734,6 +742,8 @@ public function getBatchHistory(int $batch, $order = 'asc'): array

/**
* Returns all the batches from the database history in order
*
* @return list<int>
*/
public function getBatches(): array
{
Expand Down
10 changes: 9 additions & 1 deletion system/Database/Query.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ class Query implements QueryInterface, Stringable
/**
* The binds and their values used for binding.
*
* @var array
* @var array<array-key, array{0: mixed, 1: bool}>
*/
protected $binds = [];

Expand Down Expand Up @@ -129,6 +129,8 @@ public function setQuery(string $sql, mixed $binds = null, bool $setEscape = tru
/**
* Will store the variables to bind into the query later.
*
* @param array<array-key, mixed> $binds
*
* @return $this
*/
public function setBinds(array $binds, bool $setEscape = true)
Expand Down Expand Up @@ -266,6 +268,9 @@ protected function compileBinds()
}
}

/**
* @param array<array-key, array{0: mixed, 1: bool}> $binds
*/
protected function matchNamedBinds(string $sql, array $binds): string
{
$replacers = [];
Expand All @@ -287,6 +292,9 @@ protected function matchNamedBinds(string $sql, array $binds): string
return strtr($sql, $replacers);
}

/**
* @param array<array-key, array{0: mixed, 1: bool}> $binds
*/
protected function matchSimpleBinds(string $sql, array $binds, int $bindCount, int $ml): string
{
if ($c = preg_match_all("/'[^']*'/", $sql, $matches) >= 1) {
Expand Down
14 changes: 9 additions & 5 deletions system/Database/SQLite3/Table.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,14 @@ class Table
/**
* All of the unique/primary keys in the table.
*
* @var array
* @var array<string, array{fields: list<string>, type: string}>
*/
protected $keys = [];

/**
* All of the foreign keys in the table.
*
* @var array
* @var array<array-key, stdClass>
*/
protected $foreignKeys = [];

Expand Down Expand Up @@ -230,6 +230,8 @@ public function dropForeignKey(string $foreignName)

/**
* Adds primary key
*
* @param array{fields?: list<string>} $fields
*/
public function addPrimaryKey(array $fields): Table
{
Expand All @@ -254,6 +256,8 @@ public function addPrimaryKey(array $fields): Table
/**
* Add a foreign key
*
* @param list<array{field: list<string>, referenceTable: string, referenceField: list<string>, onDelete: string, onUpdate: string, fkName: string}> $foreignKeys
*
* @return $this
*/
public function addForeignKey(array $foreignKeys)
Expand Down Expand Up @@ -374,9 +378,9 @@ protected function copyData()
* Converts fields retrieved from the database to
* the format needed for creating fields with Forge.
*
* @param array|bool $fields
* @param bool|list<stdClass> $fields
*
* @return ($fields is array ? array : mixed)
* @return ($fields is array ? array<string, array<string, bool|int|string|null>> : mixed)
*/
protected function formatFields($fields)
{
Expand Down Expand Up @@ -452,7 +456,7 @@ private function isNumericType(string $type): bool
*
* @param array<string, stdClass> $keys
*
* @return array<string, array{fields: string, type: string}>
* @return array<string, array{fields: list<string>, type: string}>
*/
protected function formatKeys($keys)
{
Expand Down
11 changes: 9 additions & 2 deletions tests/system/Database/Live/SQLite3/AlterTableTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
namespace CodeIgniter\Database\Live\SQLite3;

use CodeIgniter\Database\Exceptions\DataException;
use CodeIgniter\Database\SQLite3\Connection;
use CodeIgniter\Database\SQLite3\Forge;
use CodeIgniter\Database\SQLite3\Table;
use CodeIgniter\Test\CIUnitTestCase;
Expand Down Expand Up @@ -54,8 +55,14 @@ protected function setUp(): void
'database' => ':memory:',
'DBDebug' => true,
];
$this->db = db_connect($config);
$this->forge = Database::forge($config);
$db = db_connect($config);
$this->assertInstanceOf(Connection::class, $db);
$this->db = $db;

$forge = Database::forge($config);
$this->assertInstanceOf(Forge::class, $forge);
$this->forge = $forge;

$this->table = new Table($this->db, $this->forge);

$this->dropTables();
Expand Down
7 changes: 5 additions & 2 deletions tests/system/Database/Live/SQLite3/GetIndexDataTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,11 @@ protected function setUp(): void
'database' => 'database.db',
'DBDebug' => true,
];
$this->db = db_connect($config, false);
$this->forge = Database::forge($config);
$this->db = db_connect($config, false);

$forge = Database::forge($config);
$this->assertInstanceOf(Forge::class, $forge);
$this->forge = $forge;
}

public function testGetIndexData(): void
Expand Down
7 changes: 7 additions & 0 deletions tests/system/Database/Migrations/MigrationRunnerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@
namespace CodeIgniter\Database\Migrations;

use CodeIgniter\Database\BaseConnection;
use CodeIgniter\Database\ConnectionInterface;
use CodeIgniter\Database\MigrationRunner;
use CodeIgniter\Database\SQLSRV\Connection as SQLSRVConnection;
use CodeIgniter\Events\Events;
use CodeIgniter\Exceptions\ConfigException;
use CodeIgniter\Test\CIUnitTestCase;
Expand Down Expand Up @@ -121,6 +123,7 @@ public function testGetHistory(): void
];

if ($this->db->DBDriver === 'SQLSRV') {
$this->assertInstanceOf(SQLSRVConnection::class, $this->db);
$this->db->simpleQuery('SET IDENTITY_INSERT ' . $this->db->escapeIdentifiers($this->db->schema) . '.' . $this->db->prefixTable('migrations') . ' ON');
}

Expand All @@ -138,6 +141,7 @@ public function testGetHistory(): void
$this->assertSame($expected, $history);

if ($this->db->DBDriver === 'SQLSRV') {
$this->assertInstanceOf(SQLSRVConnection::class, $this->db);
$this->db->simpleQuery('SET IDENTITY_INSERT ' . $this->db->escapeIdentifiers($this->db->schema) . '.' . $this->db->prefixTable('migrations') . ' OFF');
$db = $this->getPrivateProperty($runner, 'db');
$db->table('migrations')->delete(['id' => 4]);
Expand Down Expand Up @@ -477,6 +481,9 @@ public function testMigrationUsesSameConnectionAsMigrationRunner(): void
}
}

/**
* @param array<string, mixed>|ConnectionInterface|string|null $db
*/
protected function resetTables($db = null): void
{
$forge = Database::forge($db);
Expand Down
12 changes: 1 addition & 11 deletions utils/phpstan-baseline/assign.propertyType.neon
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# total 24 errors
# total 22 errors

parameters:
ignoreErrors:
Expand All @@ -17,16 +17,6 @@ parameters:
count: 1
path: ../../tests/system/Commands/Utilities/Routes/FilterFinderTest.php

-
message: '#^Property CodeIgniter\\Database\\Live\\SQLite3\\AlterTableTest\:\:\$forge \(CodeIgniter\\Database\\SQLite3\\Forge\) does not accept CodeIgniter\\Database\\Forge\.$#'
count: 1
path: ../../tests/system/Database/Live/SQLite3/AlterTableTest.php

-
message: '#^Property CodeIgniter\\Database\\Live\\SQLite3\\GetIndexDataTest\:\:\$forge \(CodeIgniter\\Database\\SQLite3\\Forge\) does not accept CodeIgniter\\Database\\Forge\.$#'
count: 1
path: ../../tests/system/Database/Live/SQLite3/GetIndexDataTest.php

-
message: '#^Property CodeIgniter\\Filters\\CSRFTest\:\:\$response \(CodeIgniter\\HTTP\\Response\|null\) does not accept CodeIgniter\\HTTP\\ResponseInterface\.$#'
count: 2
Expand Down
2 changes: 1 addition & 1 deletion utils/phpstan-baseline/loader.neon
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# total 703 errors
# total 670 errors

includes:
- argument.type.neon
Expand Down
Loading
Loading