Skip to content

Add template types to Result #6749

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 1, 2022
Merged
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
74 changes: 2 additions & 72 deletions phpstan-baseline.neon.dist
Original file line number Diff line number Diff line change
Expand Up @@ -75,11 +75,6 @@ parameters:
count: 1
path: system/Database/BaseConnection.php

-
message: "#^Negated boolean expression is always false\\.$#"
count: 3
path: system/Database/BaseResult.php

-
message: "#^Unreachable statement \\- code above always terminates\\.$#"
count: 2
Expand All @@ -106,55 +101,15 @@ parameters:
path: system/Database/Migration.php

-
message: "#^Property CodeIgniter\\\\Database\\\\BaseConnection\\<mysqli\\|false\\>\\:\\:\\$strictOn \\(bool\\) in isset\\(\\) is not nullable\\.$#"
message: "#^Property CodeIgniter\\\\Database\\\\BaseConnection\\<mysqli,mysqli_result\\>\\:\\:\\$strictOn \\(bool\\) in isset\\(\\) is not nullable\\.$#"
count: 1
path: system/Database/MySQLi/Connection.php

-
message: "#^Access to an undefined property CodeIgniter\\\\Database\\\\BaseConnection\\<mysqli\\|false\\>\\:\\:\\$mysqli\\.$#"
message: "#^Access to an undefined property CodeIgniter\\\\Database\\\\BaseConnection\\<mysqli, mysqli_result\\>\\:\\:\\$mysqli\\.$#"
count: 3
path: system/Database/MySQLi/PreparedQuery.php

-
message: "#^Cannot access property \\$field_count on object\\|resource\\|false\\.$#"
count: 1
path: system/Database/MySQLi/Result.php

-
message: "#^Cannot access property \\$num_rows on object\\|resource\\|false\\.$#"
count: 1
path: system/Database/MySQLi/Result.php

-
message: "#^Cannot call method data_seek\\(\\) on object\\|resource\\|false\\.$#"
count: 1
path: system/Database/MySQLi/Result.php

-
message: "#^Cannot call method fetch_assoc\\(\\) on object\\|resource\\|false\\.$#"
count: 1
path: system/Database/MySQLi/Result.php

-
message: "#^Cannot call method fetch_field\\(\\) on object\\|resource\\|false\\.$#"
count: 1
path: system/Database/MySQLi/Result.php

-
message: "#^Cannot call method fetch_fields\\(\\) on object\\|resource\\|false\\.$#"
count: 1
path: system/Database/MySQLi/Result.php

-
message: "#^Cannot call method fetch_object\\(\\) on object\\|resource\\|false\\.$#"
count: 1
path: system/Database/MySQLi/Result.php

-
message: "#^Cannot call method field_seek\\(\\) on object\\|resource\\|false\\.$#"
count: 1
path: system/Database/MySQLi/Result.php

-
message: "#^Strict comparison using \\=\\=\\= between array<string, int|string|null> and false will always evaluate to false\\.$#"
count: 1
Expand All @@ -170,31 +125,6 @@ parameters:
count: 13
path: system/Database/SQLSRV/Forge.php

-
message: "#^Cannot call method columnName\\(\\) on object\\|resource\\|false\\.$#"
count: 2
path: system/Database/SQLite3/Result.php

-
message: "#^Cannot call method columnType\\(\\) on object\\|resource\\|false\\.$#"
count: 1
path: system/Database/SQLite3/Result.php

-
message: "#^Cannot call method fetchArray\\(\\) on object\\|resource\\|false\\.$#"
count: 2
path: system/Database/SQLite3/Result.php

-
message: "#^Cannot call method numColumns\\(\\) on object\\|resource\\|false\\.$#"
count: 1
path: system/Database/SQLite3/Result.php

-
message: "#^Cannot call method reset\\(\\) on object\\|resource\\|false\\.$#"
count: 2
path: system/Database/SQLite3/Result.php

-
message: "#^Call to an undefined method CodeIgniter\\\\View\\\\RendererInterface\\:\\:getPerformanceData\\(\\)\\.$#"
count: 1
Expand Down
17 changes: 11 additions & 6 deletions system/Database/BaseConnection.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,10 @@
* @property bool $transFailure
* @property bool $transStatus
*
* @template TConnection of false|object|resource
* @template TConnection of object|resource
* @template TResult of object|resource
*
* @implements ConnectionInterface<TConnection>
* @implements ConnectionInterface<TConnection, TResult>
*/
abstract class BaseConnection implements ConnectionInterface
{
Expand Down Expand Up @@ -194,14 +195,15 @@ abstract class BaseConnection implements ConnectionInterface
* Connection ID
*
* @var false|object|resource
* @phpstan-var TConnection
* @phpstan-var false|TConnection
*/
public $connID = false;

/**
* Result ID
*
* @var bool|object|resource
* @var false|object|resource
* @phpstan-var false|TResult
*/
public $resultID = false;

Expand Down Expand Up @@ -538,7 +540,8 @@ public function addTableAlias(string $table)
/**
* Executes the query against the database.
*
* @return bool|object|resource
* @return false|object|resource
* @phpstan-return false|TResult
*/
abstract protected function execute(string $sql);

Expand All @@ -553,6 +556,7 @@ abstract protected function execute(string $sql);
* @param mixed ...$binds
*
* @return BaseResult|bool|Query BaseResult when “read” type query, bool when “write” type query, Query when prepared query
* @phpstan-return BaseResult<TConnection, TResult>|bool|Query
*
* @todo BC set $queryClass default as null in 4.1
*/
Expand Down Expand Up @@ -662,7 +666,8 @@ public function query(string $sql, $binds = null, bool $setEscapeFlags = true, s
* is performed, nor are transactions handled. Simply takes a raw
* query string and returns the database-specific result id.
*
* @return mixed
* @return false|object|resource
* @phpstan-return false|TResult
*/
public function simpleQuery(string $sql)
{
Expand Down
16 changes: 8 additions & 8 deletions system/Database/BasePreparedQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,11 @@
use ErrorException;

/**
* Base prepared query
*
* @template TConnection of false|object|resource
* @template TConnection of object|resource
* @template TStatement of object|resource
* @template TResult of object|resource
*
* @implements PreparedQueryInterface<TConnection, TStatement>
* @implements PreparedQueryInterface<TConnection, TStatement, TResult>
*/
abstract class BasePreparedQuery implements PreparedQueryInterface
{
Expand Down Expand Up @@ -61,7 +60,7 @@ abstract class BasePreparedQuery implements PreparedQueryInterface
* A reference to the db connection to use.
*
* @var BaseConnection
* @phpstan-var BaseConnection<TConnection>
* @phpstan-var BaseConnection<TConnection, TResult>
*/
protected $db;

Expand All @@ -77,7 +76,7 @@ public function __construct(BaseConnection $db)
* NOTE: This version is based on SQL code. Child classes should
* override this method.
*
* @return mixed
* @return $this
*/
public function prepare(string $sql, array $options = [], string $queryClass = Query::class)
{
Expand All @@ -103,7 +102,7 @@ public function prepare(string $sql, array $options = [], string $queryClass = Q
/**
* The database-dependent portion of the prepare statement.
*
* @return mixed
* @return $this
*/
abstract public function _prepare(string $sql, array $options = []);

Expand All @@ -112,6 +111,7 @@ abstract public function _prepare(string $sql, array $options = []);
* prepared query. Upon success, will return a Results object.
*
* @return bool|ResultInterface
* @phpstan-return bool|ResultInterface<TConnection, TResult>
*
* @throws DatabaseException
*/
Expand Down Expand Up @@ -195,7 +195,7 @@ abstract public function _execute(array $data): bool;
/**
* Returns the result object for the prepared query.
*
* @return mixed
* @return object|resource|null
*/
abstract public function _getResult();

Expand Down
20 changes: 16 additions & 4 deletions system/Database/BaseResult.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,26 @@
use CodeIgniter\Entity\Entity;

/**
* Class BaseResult
* @template TConnection of object|resource
* @template TResult of object|resource
*
* @implements ResultInterface<TConnection, TResult>
*/
abstract class BaseResult implements ResultInterface
{
/**
* Connection ID
*
* @var object|resource
* @phpstan-var TConnection
*/
public $connID;

/**
* Result ID
*
* @var false|object|resource
* @phpstan-var false|TResult
*/
public $resultID;

Expand Down Expand Up @@ -79,6 +84,8 @@ abstract class BaseResult implements ResultInterface
*
* @param object|resource $connID
* @param object|resource $resultID
* @phpstan-param TConnection $connID
* @phpstan-param TResult $resultID
*/
public function __construct(&$connID, &$resultID)
{
Expand Down Expand Up @@ -117,7 +124,7 @@ public function getCustomResultObject(string $className)
return $this->customResultObject[$className];
}

if (is_bool($this->resultID) || ! $this->resultID) {
if (! $this->isValidResultId()) {
return [];
}

Expand Down Expand Up @@ -171,7 +178,7 @@ public function getResultArray(): array
// In the event that query caching is on, the result_id variable
// will not be a valid resource so we'll simply return an empty
// array.
if (is_bool($this->resultID) || ! $this->resultID) {
if (! $this->isValidResultId()) {
return [];
}

Expand Down Expand Up @@ -208,7 +215,7 @@ public function getResultObject(): array
// In the event that query caching is on, the result_id variable
// will not be a valid resource so we'll simply return an empty
// array.
if (is_bool($this->resultID) || ! $this->resultID) {
if (! $this->isValidResultId()) {
return [];
}

Expand Down Expand Up @@ -463,6 +470,11 @@ public function getNumRows(): int
return $this->numRows = count($this->getResultArray());
}

private function isValidResultId(): bool
{
return is_resource($this->resultID) || is_object($this->resultID);
}

/**
* Gets the number of fields in the result set.
*/
Expand Down
15 changes: 8 additions & 7 deletions system/Database/ConnectionInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,8 @@
namespace CodeIgniter\Database;

/**
* Interface ConnectionInterface
*
* @template TConnection of false|object|resource
* @template TConnection of object|resource
* @template TResult of object|resource
*/
interface ConnectionInterface
{
Expand All @@ -29,15 +28,15 @@ public function initialize();
* Connect to the database.
*
* @return false|object|resource
* @phpstan-return TConnection
* @phpstan-return false|TConnection
*/
public function connect(bool $persistent = false);

/**
* Create a persistent database connection.
*
* @return false|object|resource
* @phpstan-return TConnection
* @phpstan-return false|TConnection
*/
public function persistentConnect();

Expand All @@ -56,7 +55,7 @@ public function reconnect();
* connection is present, it must return the sole connection.
*
* @return false|object|resource
* @phpstan-return TConnection
* @phpstan-return false|TConnection
*/
public function getConnection(?string $alias = null);

Expand Down Expand Up @@ -102,6 +101,7 @@ public function getVersion(): string;
* @param mixed ...$binds
*
* @return BaseResult|bool|Query
* @phpstan-return BaseResult<TConnection, TResult>|bool|Query
*/
public function query(string $sql, $binds = null);

Expand All @@ -110,7 +110,8 @@ public function query(string $sql, $binds = null);
* is performed, nor are transactions handled. Simply takes a raw
* query string and returns the database-specific result id.
*
* @return mixed
* @return false|object|resource
* @phpstan-return false|TResult
*/
public function simpleQuery(string $sql);

Expand Down
5 changes: 3 additions & 2 deletions system/Database/MySQLi/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,15 @@
use CodeIgniter\Database\Exceptions\DatabaseException;
use LogicException;
use mysqli;
use mysqli_result;
use mysqli_sql_exception;
use stdClass;
use Throwable;

/**
* Connection for MySQLi
*
* @extends BaseConnection<false|mysqli>
* @extends BaseConnection<mysqli, mysqli_result>
*/
class Connection extends BaseConnection
{
Expand Down Expand Up @@ -279,7 +280,7 @@ public function getVersion(): string
/**
* Executes the query against the database.
*
* @return bool|object
* @return false|mysqli_result;
*/
protected function execute(string $sql)
{
Expand Down
Loading