From 5600b8672829ccc403f61eec5c4cfe33199dd11f Mon Sep 17 00:00:00 2001 From: Sergei Morozov Date: Tue, 22 Jan 2019 23:29:45 -0800 Subject: [PATCH] PHPStan Level 5 --- .../DBAL/Cache/ResultCacheStatement.php | 7 +++++-- lib/Doctrine/DBAL/DBALException.php | 15 +++++---------- .../DBAL/Driver/OCI8/OCI8Statement.php | 2 +- .../DBAL/Event/SchemaCreateTableEventArgs.php | 9 ++++----- lib/Doctrine/DBAL/Portability/Statement.php | 18 ++++++++++++++++-- .../Query/Expression/ExpressionBuilder.php | 4 ++-- lib/Doctrine/DBAL/SQLParserUtils.php | 6 +++--- .../DBAL/Schema/AbstractSchemaManager.php | 2 +- .../DBAL/Schema/SQLServerSchemaManager.php | 2 +- .../DBAL/Schema/SqliteSchemaManager.php | 4 ++-- lib/Doctrine/DBAL/Schema/Table.php | 2 +- .../Schema/Visitor/DropSchemaSqlCollector.php | 3 +++ lib/Doctrine/DBAL/Schema/Visitor/Graphviz.php | 4 +--- .../DBAL/Sharding/PoolingShardConnection.php | 2 +- phpstan.neon.dist | 5 ++++- 15 files changed, 50 insertions(+), 35 deletions(-) diff --git a/lib/Doctrine/DBAL/Cache/ResultCacheStatement.php b/lib/Doctrine/DBAL/Cache/ResultCacheStatement.php index 721709a0dd4..8fbae62de29 100644 --- a/lib/Doctrine/DBAL/Cache/ResultCacheStatement.php +++ b/lib/Doctrine/DBAL/Cache/ResultCacheStatement.php @@ -12,6 +12,7 @@ use PDO; use function array_merge; use function array_values; +use function assert; use function reset; /** @@ -41,7 +42,7 @@ class ResultCacheStatement implements IteratorAggregate, ResultStatement /** @var int */ private $lifetime; - /** @var Statement */ + /** @var ResultStatement */ private $statement; /** @@ -62,7 +63,7 @@ class ResultCacheStatement implements IteratorAggregate, ResultStatement * @param string $realKey * @param int $lifetime */ - public function __construct(Statement $stmt, Cache $resultCache, $cacheKey, $realKey, $lifetime) + public function __construct(ResultStatement $stmt, Cache $resultCache, $cacheKey, $realKey, $lifetime) { $this->statement = $stmt; $this->resultCache = $resultCache; @@ -196,6 +197,8 @@ public function fetchColumn($columnIndex = 0) */ public function rowCount() { + assert($this->statement instanceof Statement); + return $this->statement->rowCount(); } } diff --git a/lib/Doctrine/DBAL/DBALException.php b/lib/Doctrine/DBAL/DBALException.php index 15bac7f0f66..5b7465f99fe 100644 --- a/lib/Doctrine/DBAL/DBALException.php +++ b/lib/Doctrine/DBAL/DBALException.php @@ -128,11 +128,10 @@ public static function unknownDriver($unknownDriverName, array $knownDrivers) } /** - * @param Exception $driverEx - * @param string $sql - * @param mixed[] $params + * @param string $sql + * @param mixed[] $params * - * @return \Doctrine\DBAL\DBALException + * @return self */ public static function driverExceptionDuringQuery(Driver $driver, Throwable $driverEx, $sql, array $params = []) { @@ -146,9 +145,7 @@ public static function driverExceptionDuringQuery(Driver $driver, Throwable $dri } /** - * @param Exception $driverEx - * - * @return \Doctrine\DBAL\DBALException + * @return self */ public static function driverException(Driver $driver, Throwable $driverEx) { @@ -156,9 +153,7 @@ public static function driverException(Driver $driver, Throwable $driverEx) } /** - * @param Exception $driverEx - * - * @return \Doctrine\DBAL\DBALException + * @return self */ private static function wrapException(Driver $driver, Throwable $driverEx, $msg) { diff --git a/lib/Doctrine/DBAL/Driver/OCI8/OCI8Statement.php b/lib/Doctrine/DBAL/Driver/OCI8/OCI8Statement.php index 8db54c86aa7..568f3713b5a 100644 --- a/lib/Doctrine/DBAL/Driver/OCI8/OCI8Statement.php +++ b/lib/Doctrine/DBAL/Driver/OCI8/OCI8Statement.php @@ -240,7 +240,7 @@ private static function findClosingQuote( * where the token was found. * * @param string $statement The SQL statement to parse - * @param string $offset The offset to start searching from + * @param int $offset The offset to start searching from * @param string $regex The regex containing token pattern * * @return string|null Token or NULL if not found diff --git a/lib/Doctrine/DBAL/Event/SchemaCreateTableEventArgs.php b/lib/Doctrine/DBAL/Event/SchemaCreateTableEventArgs.php index 538617cc328..954f77aa55a 100644 --- a/lib/Doctrine/DBAL/Event/SchemaCreateTableEventArgs.php +++ b/lib/Doctrine/DBAL/Event/SchemaCreateTableEventArgs.php @@ -3,7 +3,6 @@ namespace Doctrine\DBAL\Event; use Doctrine\DBAL\Platforms\AbstractPlatform; -use Doctrine\DBAL\Schema\Column; use Doctrine\DBAL\Schema\Table; use function array_merge; use function is_array; @@ -16,7 +15,7 @@ class SchemaCreateTableEventArgs extends SchemaEventArgs /** @var Table */ private $table; - /** @var Column[] */ + /** @var mixed[][] */ private $columns; /** @var mixed[] */ @@ -29,8 +28,8 @@ class SchemaCreateTableEventArgs extends SchemaEventArgs private $sql = []; /** - * @param Column[] $columns - * @param mixed[] $options + * @param mixed[][] $columns + * @param mixed[] $options */ public function __construct(Table $table, array $columns, array $options, AbstractPlatform $platform) { @@ -49,7 +48,7 @@ public function getTable() } /** - * @return Column[] + * @return mixed[][] */ public function getColumns() { diff --git a/lib/Doctrine/DBAL/Portability/Statement.php b/lib/Doctrine/DBAL/Portability/Statement.php index 1499ff1bf48..514b3be2d74 100644 --- a/lib/Doctrine/DBAL/Portability/Statement.php +++ b/lib/Doctrine/DBAL/Portability/Statement.php @@ -2,6 +2,7 @@ namespace Doctrine\DBAL\Portability; +use Doctrine\DBAL\Driver\ResultStatement; use Doctrine\DBAL\Driver\Statement as DriverStatement; use Doctrine\DBAL\Driver\StatementIterator; use Doctrine\DBAL\FetchMode; @@ -9,6 +10,7 @@ use IteratorAggregate; use PDO; use function array_change_key_case; +use function assert; use function is_string; use function rtrim; @@ -20,7 +22,7 @@ class Statement implements IteratorAggregate, DriverStatement /** @var int */ private $portability; - /** @var DriverStatement */ + /** @var DriverStatement|ResultStatement */ private $stmt; /** @var int */ @@ -32,7 +34,7 @@ class Statement implements IteratorAggregate, DriverStatement /** * Wraps Statement and applies portability measures. * - * @param DriverStatement $stmt + * @param DriverStatement|ResultStatement $stmt */ public function __construct($stmt, Connection $conn) { @@ -46,6 +48,8 @@ public function __construct($stmt, Connection $conn) */ public function bindParam($column, &$variable, $type = ParameterType::STRING, $length = null) { + assert($this->stmt instanceof DriverStatement); + return $this->stmt->bindParam($column, $variable, $type, $length); } @@ -54,6 +58,8 @@ public function bindParam($column, &$variable, $type = ParameterType::STRING, $l */ public function bindValue($param, $value, $type = ParameterType::STRING) { + assert($this->stmt instanceof DriverStatement); + return $this->stmt->bindValue($param, $value, $type); } @@ -78,6 +84,8 @@ public function columnCount() */ public function errorCode() { + assert($this->stmt instanceof DriverStatement); + return $this->stmt->errorCode(); } @@ -86,6 +94,8 @@ public function errorCode() */ public function errorInfo() { + assert($this->stmt instanceof DriverStatement); + return $this->stmt->errorInfo(); } @@ -94,6 +104,8 @@ public function errorInfo() */ public function execute($params = null) { + assert($this->stmt instanceof DriverStatement); + return $this->stmt->execute($params); } @@ -228,6 +240,8 @@ public function fetchColumn($columnIndex = 0) */ public function rowCount() { + assert($this->stmt instanceof DriverStatement); + return $this->stmt->rowCount(); } } diff --git a/lib/Doctrine/DBAL/Query/Expression/ExpressionBuilder.php b/lib/Doctrine/DBAL/Query/Expression/ExpressionBuilder.php index 91f370aecb6..dfcc31ec709 100644 --- a/lib/Doctrine/DBAL/Query/Expression/ExpressionBuilder.php +++ b/lib/Doctrine/DBAL/Query/Expression/ExpressionBuilder.php @@ -286,8 +286,8 @@ public function notIn($x, $y) /** * Quotes a given input parameter. * - * @param mixed $input The parameter to be quoted. - * @param string|null $type The type of the parameter. + * @param mixed $input The parameter to be quoted. + * @param int|null $type The type of the parameter. * * @return string */ diff --git a/lib/Doctrine/DBAL/SQLParserUtils.php b/lib/Doctrine/DBAL/SQLParserUtils.php index 537cd9e4884..7dc63884a51 100644 --- a/lib/Doctrine/DBAL/SQLParserUtils.php +++ b/lib/Doctrine/DBAL/SQLParserUtils.php @@ -36,13 +36,13 @@ class SQLParserUtils /** * Gets an array of the placeholders in an sql statements as keys and their positions in the query string. * - * Returns an integer => integer pair (indexed from zero) for a positional statement - * and a string => int[] pair for a named statement. + * For a statement with positional parameters, returns a zero-indexed list of placeholder position. + * For a statement with named parameters, returns a map of placeholder positions to their parameter names. * * @param string $statement * @param bool $isPositional * - * @return int[] + * @return int[]|string[] */ public static function getPlaceholderPositions($statement, $isPositional = true) { diff --git a/lib/Doctrine/DBAL/Schema/AbstractSchemaManager.php b/lib/Doctrine/DBAL/Schema/AbstractSchemaManager.php index 52b807f8042..bf278e6e78a 100644 --- a/lib/Doctrine/DBAL/Schema/AbstractSchemaManager.php +++ b/lib/Doctrine/DBAL/Schema/AbstractSchemaManager.php @@ -268,7 +268,7 @@ public function listTableDetails($tableName) } $indexes = $this->listTableIndexes($tableName); - return new Table($tableName, $columns, $indexes, $foreignKeys, false, []); + return new Table($tableName, $columns, $indexes, $foreignKeys); } /** diff --git a/lib/Doctrine/DBAL/Schema/SQLServerSchemaManager.php b/lib/Doctrine/DBAL/Schema/SQLServerSchemaManager.php index 6cd82b2ffaf..763074e7d27 100644 --- a/lib/Doctrine/DBAL/Schema/SQLServerSchemaManager.php +++ b/lib/Doctrine/DBAL/Schema/SQLServerSchemaManager.php @@ -216,7 +216,7 @@ protected function getPortableNamespaceDefinition(array $namespace) protected function _getPortableViewDefinition($view) { // @todo - return new View($view['name'], null); + return new View($view['name'], ''); } /** diff --git a/lib/Doctrine/DBAL/Schema/SqliteSchemaManager.php b/lib/Doctrine/DBAL/Schema/SqliteSchemaManager.php index 431206a0733..9d03552b667 100644 --- a/lib/Doctrine/DBAL/Schema/SqliteSchemaManager.php +++ b/lib/Doctrine/DBAL/Schema/SqliteSchemaManager.php @@ -283,9 +283,9 @@ protected function _getPortableTableColumnList($table, $database, $tableColumns) continue; } - $type = $this->extractDoctrineTypeFromComment($comment, null); + $type = $this->extractDoctrineTypeFromComment($comment, ''); - if ($type !== null) { + if ($type !== '') { $column->setType(Type::getType($type)); $comment = $this->removeDoctrineTypeFromComment($comment, $type); diff --git a/lib/Doctrine/DBAL/Schema/Table.php b/lib/Doctrine/DBAL/Schema/Table.php index 191294d6d15..09ae27f0590 100644 --- a/lib/Doctrine/DBAL/Schema/Table.php +++ b/lib/Doctrine/DBAL/Schema/Table.php @@ -425,7 +425,7 @@ public function addNamedForeignKeyConstraint($name, $foreignTable, array $localC /** * @param string $name - * @param string $value + * @param mixed $value * * @return self */ diff --git a/lib/Doctrine/DBAL/Schema/Visitor/DropSchemaSqlCollector.php b/lib/Doctrine/DBAL/Schema/Visitor/DropSchemaSqlCollector.php index 44f5ea80af5..81e2023128b 100644 --- a/lib/Doctrine/DBAL/Schema/Visitor/DropSchemaSqlCollector.php +++ b/lib/Doctrine/DBAL/Schema/Visitor/DropSchemaSqlCollector.php @@ -78,15 +78,18 @@ public function getQueries() { $sql = []; + /** @var ForeignKeyConstraint $fkConstraint */ foreach ($this->constraints as $fkConstraint) { $localTable = $this->constraints[$fkConstraint]; $sql[] = $this->platform->getDropForeignKeySQL($fkConstraint, $localTable); } + /** @var Sequence $sequence */ foreach ($this->sequences as $sequence) { $sql[] = $this->platform->getDropSequenceSQL($sequence); } + /** @var Table $table */ foreach ($this->tables as $table) { $sql[] = $this->platform->getDropTableSQL($table); } diff --git a/lib/Doctrine/DBAL/Schema/Visitor/Graphviz.php b/lib/Doctrine/DBAL/Schema/Visitor/Graphviz.php index 889f9611254..7efb39e2863 100644 --- a/lib/Doctrine/DBAL/Schema/Visitor/Graphviz.php +++ b/lib/Doctrine/DBAL/Schema/Visitor/Graphviz.php @@ -8,8 +8,6 @@ use function current; use function file_put_contents; use function in_array; -use function mt_rand; -use function sha1; use function strtolower; /** @@ -41,7 +39,7 @@ public function acceptForeignKey(Table $localTable, ForeignKeyConstraint $fkCons */ public function acceptSchema(Schema $schema) { - $this->output = 'digraph "' . sha1(mt_rand()) . '" {' . "\n"; + $this->output = 'digraph "' . $schema->getName() . '" {' . "\n"; $this->output .= 'splines = true;' . "\n"; $this->output .= 'overlap = false;' . "\n"; $this->output .= 'outputorder=edgesfirst;' . "\n"; diff --git a/lib/Doctrine/DBAL/Sharding/PoolingShardConnection.php b/lib/Doctrine/DBAL/Sharding/PoolingShardConnection.php index abb5b4004e2..0adea2894c5 100644 --- a/lib/Doctrine/DBAL/Sharding/PoolingShardConnection.php +++ b/lib/Doctrine/DBAL/Sharding/PoolingShardConnection.php @@ -205,7 +205,7 @@ public function connect($shardId = null) /** * Connects to a specific connection. * - * @param string $shardId + * @param string|int $shardId * * @return \Doctrine\DBAL\Driver\Connection */ diff --git a/phpstan.neon.dist b/phpstan.neon.dist index 13b6ef6a393..4bedf0cd776 100644 --- a/phpstan.neon.dist +++ b/phpstan.neon.dist @@ -1,5 +1,5 @@ parameters: - level: 4 + level: 5 paths: - %currentWorkingDirectory%/lib autoload_files: @@ -26,6 +26,9 @@ parameters: # http://php.net/manual/en/pdo.sqlitecreatefunction.php - '~^Call to an undefined method Doctrine\\DBAL\\Driver\\PDOConnection::sqliteCreateFunction\(\)\.\z~' + # https://github.com/JetBrains/phpstorm-stubs/pull/488 + - '~^Parameter #1 \$byteCount of function SQLSRV_SQLTYPE_VARBINARY expects int, string given\.\z~' + # legacy variadic-like signature - '~^Method Doctrine\\DBAL\\Driver\\Connection::query\(\) invoked with \d+ parameters?, 0 required\.\z~'