diff --git a/phpstan-baseline.neon b/phpstan-baseline.neon index 4bcb70c..8d3cf47 100644 --- a/phpstan-baseline.neon +++ b/phpstan-baseline.neon @@ -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 diff --git a/src/Ting/Driver/CacheResult.php b/src/Ting/Driver/CacheResult.php index 035ddfd..1adf8f3 100644 --- a/src/Ting/Driver/CacheResult.php +++ b/src/Ting/Driver/CacheResult.php @@ -38,7 +38,7 @@ class CacheResult implements ResultInterface protected $database = null; /** - * @var (\Iterator&\Traversable)|null + * @var \Iterator|null */ protected $result = null; @@ -158,6 +158,6 @@ public function valid() */ public function getNumRows() { - return iterator_count($this->result); + return $this->result !== null ? iterator_count($this->result) : 0; } } diff --git a/src/Ting/Driver/Pgsql/Driver.php b/src/Ting/Driver/Pgsql/Driver.php index 34e8c48..51e29c6 100755 --- a/src/Ting/Driver/Pgsql/Driver.php +++ b/src/Ting/Driver/Pgsql/Driver.php @@ -58,7 +58,7 @@ class Driver implements DriverInterface protected $currentTimezone = null; /** - * @var resource|null|\PgSql\Connection pgsql + * @var \PgSql\Connection|null */ protected $connection = null; @@ -78,7 +78,7 @@ class Driver implements DriverInterface protected $objectHash = ''; /** - * @var resource|\PgSql\Result + * @var \PgSql\Result */ protected $result = null; @@ -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) . ')'); } @@ -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]; @@ -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); @@ -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); @@ -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; } @@ -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; } @@ -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; } @@ -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]; } @@ -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); @@ -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]; } @@ -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; } diff --git a/src/Ting/UnitOfWork.php b/src/Ting/UnitOfWork.php index afc9c19..249f0c1 100644 --- a/src/Ting/UnitOfWork.php +++ b/src/Ting/UnitOfWork.php @@ -41,9 +41,11 @@ class UnitOfWork implements PropertyListenerInterface protected $connectionPool = null; protected $metadataRepository = null; protected $queryFactory = null; + /** @var WeakMap */ protected WeakMap $entities; - /** @var WeakMap>> */ + /** @var WeakMap>> */ protected WeakMap $entitiesChanged; + /** @var WeakMap */ protected WeakMap $entitiesShouldBePersisted; protected $statements = []; @@ -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) {