Skip to content
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

Bump Psalm level to 5 #4094

Merged
merged 14 commits into from
Jun 20, 2020
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
5 changes: 3 additions & 2 deletions lib/Doctrine/DBAL/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
use Doctrine\DBAL\Driver\PingableConnection;
use Doctrine\DBAL\Driver\ResultStatement;
use Doctrine\DBAL\Driver\ServerInfoAwareConnection;
use Doctrine\DBAL\Driver\Statement as DriverStatement;
use Doctrine\DBAL\Exception\InvalidArgumentException;
use Doctrine\DBAL\Platforms\AbstractPlatform;
use Doctrine\DBAL\Query\Expression\ExpressionBuilder;
Expand Down Expand Up @@ -851,7 +850,7 @@ public function fetchAll($sql, array $params = [], $types = [])
*
* @param string $statement The SQL statement to prepare.
*
* @return DriverStatement The prepared statement.
* @return Statement The prepared statement.
*
* @throws DBALException
*/
Expand Down Expand Up @@ -1456,6 +1455,8 @@ public function getWrappedConnection()
{
$this->connect();

assert($this->_conn !== null);

return $this->_conn;
}

Expand Down
11 changes: 10 additions & 1 deletion lib/Doctrine/DBAL/Driver/AbstractMySQLDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use Doctrine\DBAL\Platforms\MySqlPlatform;
use Doctrine\DBAL\Schema\MySqlSchemaManager;
use Doctrine\DBAL\VersionAwarePlatformDriver;
use function assert;
use function preg_match;
use function stripos;
use function version_compare;
Expand Down Expand Up @@ -197,7 +198,15 @@ public function getDatabase(Connection $conn)
{
$params = $conn->getParams();

return $params['dbname'] ?? $conn->query('SELECT DATABASE()')->fetchColumn();
if (isset($params['dbname'])) {
return $params['dbname'];
}

$database = $conn->query('SELECT DATABASE()')->fetchColumn();

assert($database !== false);

return $database;
}

/**
Expand Down
11 changes: 10 additions & 1 deletion lib/Doctrine/DBAL/Driver/AbstractPostgreSQLDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use Doctrine\DBAL\Platforms\PostgreSqlPlatform;
use Doctrine\DBAL\Schema\PostgreSqlSchemaManager;
use Doctrine\DBAL\VersionAwarePlatformDriver;
use function assert;
use function preg_match;
use function strpos;
use function version_compare;
Expand Down Expand Up @@ -119,7 +120,15 @@ public function getDatabase(Connection $conn)
{
$params = $conn->getParams();

return $params['dbname'] ?? $conn->query('SELECT CURRENT_DATABASE()')->fetchColumn();
if (isset($params['dbname'])) {
return $params['dbname'];
}

$database = $conn->query('SELECT CURRENT_DATABASE()')->fetchColumn();

assert($database !== false);

return $database;
}

/**
Expand Down
11 changes: 10 additions & 1 deletion lib/Doctrine/DBAL/Driver/AbstractSQLAnywhereDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use Doctrine\DBAL\Platforms\SQLAnywherePlatform;
use Doctrine\DBAL\Schema\SQLAnywhereSchemaManager;
use Doctrine\DBAL\VersionAwarePlatformDriver;
use function assert;
use function preg_match;
use function version_compare;

Expand Down Expand Up @@ -119,7 +120,15 @@ public function getDatabase(Connection $conn)
{
$params = $conn->getParams();

return $params['dbname'] ?? $conn->query('SELECT DB_NAME()')->fetchColumn();
if (isset($params['dbname'])) {
return $params['dbname'];
}

$database = $conn->query('SELECT DB_NAME()')->fetchColumn();

assert($database !== false);

return $database;
}

/**
Expand Down
11 changes: 10 additions & 1 deletion lib/Doctrine/DBAL/Driver/AbstractSQLServerDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use Doctrine\DBAL\Platforms\SQLServerPlatform;
use Doctrine\DBAL\Schema\SQLServerSchemaManager;
use Doctrine\DBAL\VersionAwarePlatformDriver;
use function assert;
use function preg_match;
use function version_compare;

Expand Down Expand Up @@ -60,7 +61,15 @@ public function getDatabase(Connection $conn)
{
$params = $conn->getParams();

return $params['dbname'] ?? $conn->query('SELECT DB_NAME()')->fetchColumn();
if (isset($params['dbname'])) {
return $params['dbname'];
}

$database = $conn->query('SELECT DB_NAME()')->fetchColumn();

assert($database !== false);

return $database;
}

/**
Expand Down
6 changes: 6 additions & 0 deletions lib/Doctrine/DBAL/Driver/IBMDB2/DB2Statement.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use ReflectionProperty;
use stdClass;
use function array_change_key_case;
use function assert;
use function db2_bind_param;
use function db2_execute;
use function db2_fetch_array;
Expand All @@ -30,6 +31,7 @@
use function func_num_args;
use function fwrite;
use function gettype;
use function is_int;
use function is_object;
use function is_resource;
use function is_string;
Expand Down Expand Up @@ -91,6 +93,8 @@ public function __construct($stmt)
*/
public function bindValue($param, $value, $type = ParameterType::STRING)
{
assert(is_int($param));

return $this->bindParam($param, $value, $type);
}

Expand All @@ -99,6 +103,8 @@ public function bindValue($param, $value, $type = ParameterType::STRING)
*/
public function bindParam($column, &$variable, $type = ParameterType::STRING, $length = null)
{
assert(is_int($column));

switch ($type) {
case ParameterType::INTEGER:
$this->bind($column, $variable, DB2_PARAM_IN, DB2_LONG);
Expand Down
5 changes: 3 additions & 2 deletions lib/Doctrine/DBAL/Driver/OCI8/OCI8Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -222,11 +222,12 @@ public function rollBack()
public function errorCode()
{
$error = oci_error($this->dbh);

if ($error !== false) {
$error = $error['code'];
return $error['code'];
}

return $error;
return null;
}

/**
Expand Down
5 changes: 4 additions & 1 deletion lib/Doctrine/DBAL/Driver/PDOConnection.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,10 @@ public function __construct($dsn, $user = null, $password = null, ?array $option
public function exec($statement)
{
try {
return parent::exec($statement);
$result = parent::exec($statement);
assert($result !== false);

return $result;
} catch (\PDOException $exception) {
throw new PDOException($exception);
}
Expand Down
5 changes: 5 additions & 0 deletions lib/Doctrine/DBAL/Driver/SQLAnywhere/SQLAnywhereStatement.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use ReflectionObject;
use stdClass;
use function array_key_exists;
use function assert;
use function func_get_args;
use function func_num_args;
use function gettype;
Expand Down Expand Up @@ -91,6 +92,8 @@ public function __construct($conn, $sql)
*/
public function bindParam($column, &$variable, $type = ParameterType::STRING, $length = null)
{
assert(is_int($column));

switch ($type) {
case ParameterType::INTEGER:
case ParameterType::BOOLEAN:
Expand Down Expand Up @@ -125,6 +128,8 @@ public function bindParam($column, &$variable, $type = ParameterType::STRING, $l
*/
public function bindValue($param, $value, $type = ParameterType::STRING)
{
assert(is_int($param));

return $this->bindParam($param, $value, $type);
}

Expand Down
2 changes: 1 addition & 1 deletion lib/Doctrine/DBAL/Driver/SQLSrv/SQLSrvConnection.php
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ public function errorCode()
return $errors[0]['code'];
}

return false;
return null;
}

/**
Expand Down
30 changes: 15 additions & 15 deletions lib/Doctrine/DBAL/Driver/Statement.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@ interface Statement extends ResultStatement
* As mentioned above, the named parameters are not natively supported by the mysqli driver, use executeQuery(),
* fetchAll(), fetchArray(), fetchColumn(), fetchAssoc() methods to have the named parameter emulated by doctrine.
*
* @param mixed $param Parameter identifier. For a prepared statement using named placeholders,
* this will be a parameter name of the form :name. For a prepared statement
* using question mark placeholders, this will be the 1-indexed position of the parameter.
* @param mixed $value The value to bind to the parameter.
* @param int $type Explicit data type for the parameter using the {@link \Doctrine\DBAL\ParameterType}
* constants.
* @param int|string $param Parameter identifier. For a prepared statement using named placeholders,
* this will be a parameter name of the form :name. For a prepared statement
* using question mark placeholders, this will be the 1-indexed position of the parameter.
* @param mixed $value The value to bind to the parameter.
* @param int $type Explicit data type for the parameter using the {@link \Doctrine\DBAL\ParameterType}
* constants.
*
* @return bool TRUE on success or FALSE on failure.
*/
Expand All @@ -44,15 +44,15 @@ public function bindValue($param, $value, $type = ParameterType::STRING);
* of stored procedures that return data as output parameters, and some also as input/output
* parameters that both send in data and are updated to receive it.
*
* @param mixed $column Parameter identifier. For a prepared statement using named placeholders,
* this will be a parameter name of the form :name. For a prepared statement using
* question mark placeholders, this will be the 1-indexed position of the parameter.
* @param mixed $variable Name of the PHP variable to bind to the SQL statement parameter.
* @param int $type Explicit data type for the parameter using the {@link \Doctrine\DBAL\ParameterType}
* constants. To return an INOUT parameter from a stored procedure, use the bitwise
* OR operator to set the PDO::PARAM_INPUT_OUTPUT bits for the data_type parameter.
* @param int|null $length You must specify maxlength when using an OUT bind
* so that PHP allocates enough memory to hold the returned value.
* @param int|string $column Parameter identifier. For a prepared statement using named placeholders,
* this will be a parameter name of the form :name. For a prepared statement using
* question mark placeholders, this will be the 1-indexed position of the parameter.
* @param mixed $variable Name of the PHP variable to bind to the SQL statement parameter.
* @param int $type Explicit data type for the parameter using the {@link \Doctrine\DBAL\ParameterType}
* constants. To return an INOUT parameter from a stored procedure, use the bitwise
* OR operator to set the PDO::PARAM_INPUT_OUTPUT bits for the data_type parameter.
* @param int|null $length You must specify maxlength when using an OUT bind
* so that PHP allocates enough memory to hold the returned value.
*
* @return bool TRUE on success or FALSE on failure.
*/
Expand Down
2 changes: 1 addition & 1 deletion lib/Doctrine/DBAL/Query/QueryBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -395,7 +395,7 @@ public function setMaxResults($maxResults)
* Gets the maximum number of results the query object was set to retrieve (the "limit").
* Returns NULL if all results will be returned.
*
* @return int The maximum number of results.
* @return int|null The maximum number of results.
*/
public function getMaxResults()
{
Expand Down
5 changes: 5 additions & 0 deletions phpstan.neon.dist
Original file line number Diff line number Diff line change
Expand Up @@ -95,3 +95,8 @@ parameters:
message: '~Method Doctrine\\DBAL\\Driver\\PDOSqlsrv\\Connection\:\:lastInsertId\(\) should return string but returns string\|false\|null\.~'
paths:
- %currentWorkingDirectory%/lib/Doctrine/DBAL/Driver/PDOSqlsrv/Connection.php

-
message: '~Method Doctrine\\DBAL\\Portability\\Connection::prepare\(\) should return Doctrine\\DBAL\\Statement but returns Doctrine\\DBAL\\Portability\\Statement\.~'
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is caused by the issue earlier identified in #4045:

The portability layer is currently implemented on two levels of abstraction at the same time: most of its components implement driver-level interfaces but the Connection class extends the DBAL connection.

paths:
- %currentWorkingDirectory%/lib/Doctrine/DBAL/Portability/Connection.php
19 changes: 18 additions & 1 deletion psalm.xml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0"?>
<psalm
totallyTyped="false"
errorLevel="6"
errorLevel="5"
resolveFromConfigFile="true"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="https://getpsalm.org/schema/config"
Expand Down Expand Up @@ -32,6 +32,15 @@
<file name="lib/Doctrine/DBAL/Driver/OCI8/OCI8Statement.php"/>
</errorLevel>
</ConflictingReferenceConstraint>
<FalsableReturnStatement>
<errorLevel type="suppress">
<!--
Fixing these issues requires an API change
-->
<file name="lib/Doctrine/DBAL/Driver/SQLAnywhere/SQLAnywhereConnection.php"/>
<file name="lib/Doctrine/DBAL/Driver/SQLSrv/SQLSrvConnection.php"/>
</errorLevel>
</FalsableReturnStatement>
<MethodSignatureMismatch>
<errorLevel type="suppress">
<!--
Expand All @@ -41,6 +50,14 @@
<file name="lib/Doctrine/DBAL/Driver/PDOConnection.php"/>
</errorLevel>
</MethodSignatureMismatch>
<NullableReturnStatement>
<errorLevel type="suppress">
<!--
Fixing this issue requires an API change
-->
<file name="lib/Doctrine/DBAL/Driver/AbstractSQLiteDriver.php"/>
</errorLevel>
</NullableReturnStatement>
<TooFewArguments>
<errorLevel type="suppress">
<!--
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ protected function createSchemaManager(Connection $connection) : AbstractSchemaM
}

/**
* @return mixed[][]
* {@inheritDoc}
*/
protected function getDatabasePlatformsForVersions() : array
{
Expand Down
7 changes: 6 additions & 1 deletion tests/Doctrine/Tests/DBAL/Driver/StatementIteratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,29 +5,34 @@
use Doctrine\DBAL\Driver\IBMDB2\DB2Statement;
use Doctrine\DBAL\Driver\Mysqli\MysqliStatement;
use Doctrine\DBAL\Driver\OCI8\OCI8Statement;
use Doctrine\DBAL\Driver\ResultStatement;
use Doctrine\DBAL\Driver\SQLAnywhere\SQLAnywhereStatement;
use Doctrine\DBAL\Driver\SQLSrv\SQLSrvStatement;
use Doctrine\DBAL\Driver\Statement;
use Doctrine\DBAL\Driver\StatementIterator;
use Doctrine\DBAL\Portability\Statement as PortabilityStatement;
use Doctrine\Tests\DbalTestCase;
use IteratorIterator;
use PHPUnit\Framework\MockObject\MockObject;
use Traversable;
use function extension_loaded;

class StatementIteratorTest extends DbalTestCase
{
/**
* @param class-string<ResultStatement> $class
*
* @dataProvider statementProvider()
*/
public function testGettingIteratorDoesNotCallFetch(string $class) : void
{
$stmt = $this->createPartialMock($class, ['fetch', 'fetchAll', 'fetchColumn']);

$stmt->expects($this->never())->method('fetch');
$stmt->expects($this->never())->method('fetchAll');
$stmt->expects($this->never())->method('fetchColumn');

$stmt->getIterator();
new IteratorIterator($stmt);
}

public function testIteratorIterationCallsFetchOncePerStep() : void
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
namespace Doctrine\Tests\DBAL\Functional\Driver\PDOSqlsrv;

use Doctrine\DBAL\Driver as DriverInterface;
use Doctrine\DBAL\Driver\Connection;
use Doctrine\DBAL\Driver\PDOConnection;
use Doctrine\DBAL\Driver\PDOSqlsrv\Driver;
use Doctrine\Tests\DBAL\Functional\Driver\AbstractDriverTest;
use Doctrine\Tests\TestUtil;
Expand Down Expand Up @@ -40,7 +40,7 @@ protected static function getDatabaseNameForConnectionWithoutDatabaseNameParamet
/**
* @param int[]|string[] $driverOptions
*/
protected function getConnection(array $driverOptions) : Connection
private function getConnection(array $driverOptions) : PDOConnection
{
$params = TestUtil::getConnectionParams();

Expand Down
Loading