Skip to content

Commit

Permalink
fix: phpstan
Browse files Browse the repository at this point in the history
  • Loading branch information
vgreb committed Mar 21, 2024
1 parent aabf473 commit 83e53f6
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 17 deletions.
5 changes: 0 additions & 5 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -705,11 +705,6 @@ parameters:
count: 1
path: src/Ting/Driver/Pgsql/Driver.php

-
message: "#^Parameter \\#1 \\$result of method CCMBenchmark\\\\Ting\\\\Driver\\\\Pgsql\\\\Result\\:\\:setResult\\(\\) expects object, PgSql\\\\Result\\|resource given\\.$#"
count: 1
path: src/Ting/Driver/Pgsql/Driver.php

-
message: "#^Parameter \\#1 \\$statementName of class CCMBenchmark\\\\Ting\\\\Driver\\\\Pgsql\\\\Statement constructor expects object, string given\\.$#"
count: 1
Expand Down
4 changes: 2 additions & 2 deletions src/Ting/Driver/CacheResult.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class CacheResult implements ResultInterface
protected $database = null;

/**
* @var (\Iterator&\Traversable)|null
* @var \Iterator|null
*/
protected $result = null;

Expand Down Expand Up @@ -158,6 +158,6 @@ public function valid()
*/
public function getNumRows()
{
return iterator_count($this->result);
return $this->result !== null ? iterator_count($this->result) : 0;
}
}
55 changes: 47 additions & 8 deletions src/Ting/Driver/Pgsql/Driver.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ class Driver implements DriverInterface
protected $currentTimezone = null;

/**
* @var resource|null|\PgSql\Connection pgsql
* @var \PgSql\Connection|null
*/
protected $connection = null;

Expand All @@ -78,7 +78,7 @@ class Driver implements DriverInterface
protected $objectHash = '';

/**
* @var resource|\PgSql\Result
* @var \PgSql\Result
*/
protected $result = null;

Expand Down Expand Up @@ -147,7 +147,7 @@ public function setCharset($charset)
return;
}

if (pg_set_client_encoding($this->connection, $charset) === -1) {
if ($this->connection === null || pg_set_client_encoding($this->connection, $charset) === -1) {
throw new DriverException('Can\'t set charset ' . $charset . ' (' . pg_last_error($this->connection) . ')');
}

Expand Down Expand Up @@ -208,6 +208,10 @@ public function execute($originalSQL, array $params = array(), CollectionInterfa
{
list ($sql, $paramsOrder) = $this->convertParameters($originalSQL);

if ($this->connection === null) {
throw new DriverException('No active connection.');
}

$values = array();
foreach (array_keys($paramsOrder) as $key) {
$values[] = &$params[$key];
Expand All @@ -218,18 +222,23 @@ public function execute($originalSQL, array $params = array(), CollectionInterfa
}

if ($values === []) {
$this->result = pg_query($this->connection, $sql);
$result = pg_query($this->connection, $sql);
if ($result === false) {
throw new QueryException(pg_last_error($this->connection) . ' (Query: ' . $sql . ')');
}
$this->result = $result;
} else {
$this->result = pg_query_params($this->connection, $sql, $values);
$result = pg_query_params($this->connection, $sql, $values);
if ($result === false) {
throw new QueryException(pg_last_error($this->connection) . ' (Query: ' . $sql . ')');
}
$this->result = $result;
}

if ($this->logger !== null) {
$this->logger->stopQuery();
}

if ($this->result === false) {
throw new QueryException(pg_last_error($this->connection) . ' (Query: ' . $sql . ')');
}

if ($collection === null) {
$resultStatus = pg_result_status($this->result);
Expand Down Expand Up @@ -284,6 +293,9 @@ public function prepare($originalSQL)
$this->logger->startPrepare($originalSQL, $this->objectHash, $this->database);
$statement->setLogger($this->logger);
}
if ($this->connection === null) {
throw new DriverException('No active connection.');
}
$result = pg_prepare($this->connection, $statementName, $sql);
if ($this->logger !== null) {
$this->logger->stopPrepare($statementName);
Expand Down Expand Up @@ -382,6 +394,9 @@ public function startTransaction()
if ($this->transactionOpened === true) {
throw new TransactionException('Cannot start another transaction');
}
if ($this->connection === null) {
throw new DriverException('No active connection.');
}
pg_query($this->connection, 'BEGIN');
$this->transactionOpened = true;
}
Expand All @@ -395,6 +410,9 @@ public function commit()
if ($this->transactionOpened === false) {
throw new TransactionException('Cannot commit no transaction');
}
if ($this->connection === null) {
throw new DriverException('No active connection.');
}
pg_query($this->connection, 'COMMIT');
$this->transactionOpened = false;
}
Expand All @@ -408,6 +426,9 @@ public function rollback()
if ($this->transactionOpened === false) {
throw new TransactionException('Cannot rollback no transaction');
}
if ($this->connection === null) {
throw new DriverException('No active connection.');
}
pg_query($this->connection, 'ROLLBACK');
$this->transactionOpened = false;
}
Expand All @@ -418,8 +439,17 @@ public function rollback()
*/
public function getInsertedId()
{
if ($this->connection === null) {
throw new DriverException('No active connection.');
}
$resultResource = pg_query($this->connection, 'SELECT lastval()');
if ($resultResource === false) {
throw new DriverException('Could not fetch last inserted id.');
}
$row = pg_fetch_row($resultResource);
if ($row === false) {
throw new DriverException('Could not fetch last inserted id.');
}
return (int) $row[0];
}

Expand All @@ -430,6 +460,9 @@ public function getInsertedId()
*/
public function getInsertedIdForSequence($sequenceName)
{
if ($this->connection === null) {
throw new DriverException('No active connection.');
}
$sql = "SELECT currval('$sequenceName')";
$resultResource = @pg_query($this->connection, $sql);

Expand All @@ -438,6 +471,9 @@ public function getInsertedIdForSequence($sequenceName)
}

$row = pg_fetch_row($resultResource);
if ($row === false) {
throw new DriverException('Could not fetch last inserted id. Details: '. pg_last_error($this->connection));
}
return (int) $row[0];
}

Expand Down Expand Up @@ -502,6 +538,9 @@ public function setTimezone($timezone)
$value = 'DEFAULT';
$query = str_replace('"', '', $query);
}
if ($this->connection === null) {
throw new DriverException('No active connection.');
}
pg_query($this->connection, sprintf($query, $value));
$this->currentTimezone = $timezone;
}
Expand Down
6 changes: 4 additions & 2 deletions src/Ting/UnitOfWork.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,11 @@ class UnitOfWork implements PropertyListenerInterface
protected $connectionPool = null;
protected $metadataRepository = null;
protected $queryFactory = null;
/** @var WeakMap<NotifyPropertyInterface, NotifyPropertyInterface|bool> */
protected WeakMap $entities;
/** @var WeakMap<PropertyListenerInterface, array<string, array<mixed, mixed>>> */
/** @var WeakMap<NotifyPropertyInterface, array<string, array<mixed, mixed>>> */
protected WeakMap $entitiesChanged;
/** @var WeakMap<NotifyPropertyInterface, int> */
protected WeakMap $entitiesShouldBePersisted;
protected $statements = [];

Expand Down Expand Up @@ -163,7 +165,7 @@ public function propertyChanged(NotifyPropertyInterface $entity, $propertyName,
}

if (isset($this->entitiesChanged[$entity]) === false) {
$this->entitiesChanged[$entity] = [];
$this->entitiesChanged[$entity] = [];
}

if (isset($this->entitiesChanged[$entity][$propertyName]) === false) {
Expand Down

0 comments on commit 83e53f6

Please sign in to comment.