Skip to content

Commit ea6def2

Browse files
committed
Close pgsql connection in destructor
1 parent 0b23e06 commit ea6def2

File tree

3 files changed

+17
-0
lines changed

3 files changed

+17
-0
lines changed

phpstan.neon.dist

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,9 @@ parameters:
148148
- '~^Parameter #1 \$row of method Doctrine\\DBAL\\Driver\\PgSQL\\Result\:\:mapAssociativeRow\(\) expects array<string, string\|null>, array<int\|string, string\|null> given\.$~'
149149
- '~^Parameter #1 \$row of method Doctrine\\DBAL\\Driver\\PgSQL\\Result\:\:mapNumericRow\(\) expects array<int, string\|null>, array<int\|string, string\|null> given\.$~'
150150

151+
# Ignore isset() checks in destructors.
152+
- '~^Property Doctrine\\DBAL\\Driver\\PgSQL\\Connection\:\:\$connection \(PgSql\\Connection\|resource\) in isset\(\) is not nullable\.$~'
153+
151154
# On PHP 7.4, pg_fetch_all() might return false for empty result sets.
152155
-
153156
message: '~^Strict comparison using === between array<int, array> and false will always evaluate to false\.$~'

psalm.xml.dist

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -645,6 +645,7 @@
645645
<errorLevel type="suppress">
646646
<!-- PgSql objects are represented as resources in PHP 7.4 -->
647647
<referencedFunction name="pg_affected_rows"/>
648+
<referencedFunction name="pg_close"/>
648649
<referencedFunction name="pg_escape_bytea"/>
649650
<referencedFunction name="pg_escape_literal"/>
650651
<referencedFunction name="pg_fetch_all"/>
@@ -748,6 +749,9 @@
748749
<errorLevel type="suppress">
749750
<!-- Running isset() checks on properties that should be initialized by setUp() is fine. -->
750751
<directory name="tests"/>
752+
753+
<!-- Ignore isset() checks in destructors. -->
754+
<file name="src/Driver/PgSQL/Connection.php"/>
751755
</errorLevel>
752756
</RedundantPropertyInitializationCheck>
753757
<ReferenceConstraintViolation>

src/Driver/PgSQL/Connection.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use function gettype;
1515
use function is_object;
1616
use function is_resource;
17+
use function pg_close;
1718
use function pg_escape_bytea;
1819
use function pg_escape_literal;
1920
use function pg_get_result;
@@ -47,6 +48,15 @@ public function __construct($connection)
4748
$this->parser = new Parser(false);
4849
}
4950

51+
public function __destruct()
52+
{
53+
if (isset($this->connection)) {
54+
@pg_close($this->connection);
55+
}
56+
57+
unset($this->connection);
58+
}
59+
5060
public function prepare(string $sql): Statement
5161
{
5262
$visitor = new ConvertParameters();

0 commit comments

Comments
 (0)