Skip to content

Commit

Permalink
Merge 3.2.x into 4.0.x
Browse files Browse the repository at this point in the history
* 3.2.x:
  Use the driver API to begin a transaction on mysqli
  Refactor MySQLi statement to make it unaware of the connection
  Cleanup ignored PHPStan errors
  doctrine#4886 Fixed calling `AbstractPlatform::getAdvancedForeignKeyOptionsSQL` triggers a deprecation
  Add missing upcast
  • Loading branch information
derrabus committed Oct 20, 2021
2 parents 1819d25 + 99df446 commit 0d3af82
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 106 deletions.
85 changes: 0 additions & 85 deletions phpstan.neon.dist
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,6 @@ parameters:
- '~^Class Doctrine\\Common\\(Collections\\Collection|Persistence\\Proxy) not found\.\z~'
- '~^.+ on an unknown class Doctrine\\Common\\(Collections\\Collection|Persistence\\Proxy)\.\z~'

# may not exist when pdo_sqlsrv is not loaded but PDO is
- '~^Access to undefined constant PDO::SQLSRV_ENCODING_BINARY\.\z~'

# weird class name, represented in stubs as OCI_(Lob|Collection)
- '~unknown class OCI-(Lob|Collection)~'
- '~^Call to method writetemporary\(\) on an unknown class OCILob\.~'

# Requires a release of https://github.com/JetBrains/phpstorm-stubs/pull/553
-
message: '~^Call to function assert\(\) with true will always evaluate to true\.$~'
Expand All @@ -34,9 +27,6 @@ parameters:
message: '~^Strict comparison using === between string and null will always evaluate to false\.$~'
path: %currentWorkingDirectory%/src/Driver/IBMDB2/Connection.php

# Needs Generics
- '~Method Doctrine\\DBAL\\Schema\\SchemaDiff::getNewTablesSortedByDependencies\(\) should return array<Doctrine\\DBAL\\Schema\\Table> but returns array<object>.~'

# https://github.com/phpstan/phpstan/issues/3134
-
message: '~^Call to static method PHPUnit\\Framework\\Assert::assertSame\(\) with Doctrine\\DBAL\\Types\\Type and Doctrine\\DBAL\\Types\\Type will always evaluate to true\.$~'
Expand All @@ -52,12 +42,6 @@ parameters:
- src/Platforms/*Platform.php
- src/Schema/*SchemaManager.php

# Fixing the issue may cause a BC break.
-
message: '~^Method Doctrine\\DBAL\\Driver\\Mysqli\\Connection::lastInsertId\(\) should return string but returns int\|string\.$~'
paths:
- src/Driver/Mysqli/Connection.php

# In some namespaces, we use array<string,mixed>, some elements of which are actually boolean
-
message: '~^Only booleans are allowed in .*, mixed given~'
Expand All @@ -73,17 +57,6 @@ parameters:
- src/Schema/AbstractSchemaManager.php
- src/Schema/Column.php

# https://github.com/phpstan/phpstan/issues/3146
-
message: '~^Only numeric types are allowed in -, int<1, max>\|false given on the left side\.~'
paths:
- src/Platforms/SQLServerPlatform.php

# Temporaily suppressed during up-merging an upgrade PHPStan 0.12
-
message: '~^Call to an undefined method Doctrine\\DBAL\\Driver::createDatabasePlatformForVersion\(\)\.$~'
path: %currentWorkingDirectory%/tests/Driver/AbstractDriverTest.php

# Temporaily suppressed during up-merging an upgrade to PHPStan 0.12.33
-
message: '~^Parameter #1 \$expected of static method PHPUnit\\Framework\\Assert::assertInstanceOf\(\) expects class-string<Doctrine\\DBAL\\Connection&PHPUnit\\Framework\\MockObject\\MockObject>, class-string<Doctrine\\DBAL\\Connection>&class-string<PHPUnit\\Framework\\MockObject\\MockObject> given\.$~'
Expand All @@ -95,41 +68,6 @@ parameters:
paths:
- src/DriverManager.php

# Unlike Psalm, PHPStan doesn't understand that $matchesCount cannot be false
-
message: '~^Only numeric types are allowed in pre\-decrement, int\<1, max\>\|false given\.$~'
paths:
- src/Platforms/SQLServerPlatform.php

# https://github.com/phpstan/phpstan-phpunit/issues/83
-
message: '~^Only iterables can be unpacked, array<int, mixed>\|false given in argument #1\.$~'
paths:
- tests/Functional/Platform/DefaultExpressionTest.php

# https://github.com/phpstan/phpstan/issues/4126
-
message: '~^Call to function iterator_to_array\(\) on a separate line has no effect\.$~'
paths:
- %currentWorkingDirectory%/tests/Functional/Connection/FetchTest.php

# https://github.com/phpstan/phpstan/issues/4557
-
message: '~^Parameter #1 \$originalClassName of method PHPUnit\\Framework\\TestCase::createMock\(\) expects class-string<T of Doctrine\\DBAL\\Platforms\\AbstractPlatform>, string given\.$~'
paths:
- %currentWorkingDirectory%/tests/Platforms/AbstractPlatformTestCase.php

# https://github.com/phpstan/phpstan/issues/4557
-
message: '~^Unable to resolve the template type T in call to method PHPUnit\\Framework\\TestCase::createMock\(\)$~'
paths:
- %currentWorkingDirectory%/tests/Platforms/AbstractPlatformTestCase.php

-
message: '~^Instanceof between Doctrine\\DBAL\\Platforms\\Keywords\\KeywordList and Doctrine\\DBAL\\Platforms\\Keywords\\KeywordList will always evaluate to true\.~'
paths:
- src/Platforms/AbstractPlatform.php

# https://github.com/phpstan/phpstan-src/pull/692
-
message: '~^Parameter #2 \$count of function array_fill expects int<0, max>, int given\.~'
Expand All @@ -145,42 +83,19 @@ parameters:
message: '~^Unreachable statement - code above always terminates\.$~'
paths:
- src/Driver/OCI8/Result.php
-
message: '~^Call to function assert\(\) with true will always evaluate to true\.$~'
paths:
- src/Driver/OCI8/Statement.php
-
message: '~^Strict comparison using !== between OCILob\|null and false will always evaluate to true\.$~'
paths:
- src/Driver/OCI8/Statement.php

# This class has been added in PHP 8.1
- '/^Attribute class ReturnTypeWillChange does not exist\.$/'

# https://github.com/phpstan/phpstan/issues/5608
-
message: '~^Circular definition detected in type alias (Override)?Params\.$~'
paths:
- src/DriverManager.php

# https://github.com/phpstan/phpstan/issues/1901
-
message: '~^Method Doctrine\\DBAL\\Platforms\\AbstractPlatform::escapeStringForLike\(\) should return string but returns string\|null\.$~'
paths:
- src/Platforms/AbstractPlatform.php

# https://github.com/phpstan/phpstan/issues/4679
-
message: '~^Cannot call method writeTemporary\(\) on OCILob\|null\.$~'
paths:
- src/Driver/OCI8/Statement.php

# TODO: check for null after calling Connection::getDatabase()
-
message: '~Parameter.*\$database of method.*expects string, string\|null given\.~'
path: src/Schema/AbstractSchemaManager.php
count: 3

# TODO: prevent unsafe usage of operator - with null
-
message: '~Only numeric types are allowed in \-, float\|null given on the right side\.~'
Expand Down
14 changes: 12 additions & 2 deletions src/Driver/Mysqli/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,17 @@ public function getServerVersion(): string

public function prepare(string $sql): DriverStatement
{
return new Statement($this->conn, $sql);
try {
$stmt = $this->conn->prepare($sql);
} catch (mysqli_sql_exception $e) {
throw ConnectionError::upcast($e);
}

if ($stmt === false) {
throw ConnectionError::new($this->conn);
}

return new Statement($stmt);
}

public function query(string $sql): ResultInterface
Expand Down Expand Up @@ -148,7 +158,7 @@ public function lastInsertId()

public function beginTransaction(): void
{
$this->conn->query('START TRANSACTION');
$this->conn->begin_transaction();
}

public function commit(): void
Expand Down
20 changes: 1 addition & 19 deletions src/Driver/Mysqli/Statement.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,12 @@

use Doctrine\DBAL\Driver\Exception;
use Doctrine\DBAL\Driver\Exception\UnknownParameterType;
use Doctrine\DBAL\Driver\Mysqli\Exception\ConnectionError;
use Doctrine\DBAL\Driver\Mysqli\Exception\FailedReadingStreamOffset;
use Doctrine\DBAL\Driver\Mysqli\Exception\NonStreamResourceUsedAsLargeObject;
use Doctrine\DBAL\Driver\Mysqli\Exception\StatementError;
use Doctrine\DBAL\Driver\Result as ResultInterface;
use Doctrine\DBAL\Driver\Statement as StatementInterface;
use Doctrine\DBAL\ParameterType;
use mysqli;
use mysqli_sql_exception;
use mysqli_stmt;

Expand All @@ -40,8 +38,6 @@ final class Statement implements StatementInterface
ParameterType::LARGE_OBJECT => 'b',
];

private mysqli $conn;

private mysqli_stmt $stmt;

/** @var mixed[] */
Expand All @@ -58,23 +54,9 @@ final class Statement implements StatementInterface

/**
* @internal The statement can be only instantiated by its driver connection.
*
* @throws Exception
*/
public function __construct(mysqli $conn, string $sql)
public function __construct(mysqli_stmt $stmt)
{
$this->conn = $conn;

try {
$stmt = $conn->prepare($sql);
} catch (mysqli_sql_exception $e) {
throw ConnectionError::upcast($e);
}

if ($stmt === false) {
throw ConnectionError::new($this->conn);
}

$this->stmt = $stmt;

$paramCount = $this->stmt->param_count;
Expand Down

0 comments on commit 0d3af82

Please sign in to comment.