From 3e83d36eb4e77c4858d3b4cdbed32dea8bc77d72 Mon Sep 17 00:00:00 2001 From: Sergei Morozov Date: Mon, 21 Jan 2019 22:30:49 -0800 Subject: [PATCH] PHPStan Level 4 --- lib/Doctrine/DBAL/Connection.php | 4 +-- .../Connections/MasterSlaveConnection.php | 4 +-- .../DBAL/Driver/IBMDB2/DB2Statement.php | 14 +--------- .../DBAL/Driver/Mysqli/MysqliStatement.php | 26 +++++++------------ .../DBAL/Platforms/AbstractPlatform.php | 17 ++++++------ .../DBAL/Platforms/PostgreSqlPlatform.php | 3 ++- lib/Doctrine/DBAL/Query/QueryBuilder.php | 2 +- .../DBAL/Schema/AbstractSchemaManager.php | 18 +++---------- lib/Doctrine/DBAL/Schema/ColumnDiff.php | 2 +- lib/Doctrine/DBAL/Schema/Sequence.php | 5 ++-- .../DBAL/Schema/SqliteSchemaManager.php | 11 ++++---- lib/Doctrine/DBAL/Schema/TableDiff.php | 12 ++++++--- .../SQLAzure/SQLAzureShardManager.php | 6 ----- phpstan.neon.dist | 2 +- .../SQLAzure/SQLAzureShardManagerTest.php | 12 --------- 15 files changed, 47 insertions(+), 91 deletions(-) diff --git a/lib/Doctrine/DBAL/Connection.php b/lib/Doctrine/DBAL/Connection.php index 62023534c87..06ebae21944 100644 --- a/lib/Doctrine/DBAL/Connection.php +++ b/lib/Doctrine/DBAL/Connection.php @@ -149,7 +149,7 @@ class Connection implements DriverConnection /** * The schema manager. * - * @var AbstractSchemaManager + * @var AbstractSchemaManager|null */ protected $_schemaManager; @@ -1446,7 +1446,7 @@ public function getWrappedConnection() */ public function getSchemaManager() { - if (! $this->_schemaManager) { + if ($this->_schemaManager === null) { $this->_schemaManager = $this->_driver->getSchemaManager($this); } diff --git a/lib/Doctrine/DBAL/Connections/MasterSlaveConnection.php b/lib/Doctrine/DBAL/Connections/MasterSlaveConnection.php index 17dac54d4a2..1365e4039fb 100644 --- a/lib/Doctrine/DBAL/Connections/MasterSlaveConnection.php +++ b/lib/Doctrine/DBAL/Connections/MasterSlaveConnection.php @@ -133,7 +133,7 @@ public function connect($connectionName = null) // If we have a connection open, and this is not an explicit connection // change request, then abort right here, because we are already done. // This prevents writes to the slave in case of "keepSlave" option enabled. - if (isset($this->_conn) && $this->_conn && ! $requestedConnectionChange) { + if ($this->_conn !== null && ! $requestedConnectionChange) { return false; } @@ -144,7 +144,7 @@ public function connect($connectionName = null) $forceMasterAsSlave = true; } - if (isset($this->connections[$connectionName]) && $this->connections[$connectionName]) { + if (isset($this->connections[$connectionName])) { $this->_conn = $this->connections[$connectionName]; if ($forceMasterAsSlave && ! $this->keepSlave) { diff --git a/lib/Doctrine/DBAL/Driver/IBMDB2/DB2Statement.php b/lib/Doctrine/DBAL/Driver/IBMDB2/DB2Statement.php index 0743c2b9817..4244b256414 100644 --- a/lib/Doctrine/DBAL/Driver/IBMDB2/DB2Statement.php +++ b/lib/Doctrine/DBAL/Driver/IBMDB2/DB2Statement.php @@ -146,10 +146,6 @@ private function bind($parameter, &$variable, int $parameterType, int $dataType) */ public function closeCursor() { - if (! $this->stmt) { - return false; - } - $this->bindParam = []; if (! db2_free_result($this->stmt)) { @@ -166,11 +162,7 @@ public function closeCursor() */ public function columnCount() { - if (! $this->stmt) { - return 0; - } - - return db2_num_fields($this->stmt); + return db2_num_fields($this->stmt) ?: 0; } /** @@ -197,10 +189,6 @@ public function errorInfo() */ public function execute($params = null) { - if (! $this->stmt) { - return false; - } - if ($params === null) { ksort($this->bindParam); diff --git a/lib/Doctrine/DBAL/Driver/Mysqli/MysqliStatement.php b/lib/Doctrine/DBAL/Driver/Mysqli/MysqliStatement.php index 895e5fa501d..e7600d92296 100644 --- a/lib/Doctrine/DBAL/Driver/Mysqli/MysqliStatement.php +++ b/lib/Doctrine/DBAL/Driver/Mysqli/MysqliStatement.php @@ -97,16 +97,12 @@ public function __construct(mysqli $conn, $prepareString) */ public function bindParam($column, &$variable, $type = ParameterType::STRING, $length = null) { - if ($type === null) { - $type = 's'; - } else { - if (! isset(self::$_paramTypeMap[$type])) { - throw new MysqliException(sprintf("Unknown type: '%s'", $type)); - } - - $type = self::$_paramTypeMap[$type]; + if (! isset(self::$_paramTypeMap[$type])) { + throw new MysqliException(sprintf("Unknown type: '%s'", $type)); } + $type = self::$_paramTypeMap[$type]; + $this->_bindedValues[$column] =& $variable; $this->types[$column - 1] = $type; @@ -118,16 +114,12 @@ public function bindParam($column, &$variable, $type = ParameterType::STRING, $l */ public function bindValue($param, $value, $type = ParameterType::STRING) { - if ($type === null) { - $type = 's'; - } else { - if (! isset(self::$_paramTypeMap[$type])) { - throw new MysqliException(sprintf("Unknown type: '%s'", $type)); - } - - $type = self::$_paramTypeMap[$type]; + if (! isset(self::$_paramTypeMap[$type])) { + throw new MysqliException(sprintf("Unknown type: '%s'", $type)); } + $type = self::$_paramTypeMap[$type]; + $this->_values[$param] = $value; $this->_bindedValues[$param] =& $this->_values[$param]; $this->types[$param - 1] = $type; @@ -284,7 +276,7 @@ private function _bindValues($values) } /** - * @return mixed[]|false + * @return mixed[]|false|null */ private function _fetch() { diff --git a/lib/Doctrine/DBAL/Platforms/AbstractPlatform.php b/lib/Doctrine/DBAL/Platforms/AbstractPlatform.php index 5e4537391ed..68994846864 100644 --- a/lib/Doctrine/DBAL/Platforms/AbstractPlatform.php +++ b/lib/Doctrine/DBAL/Platforms/AbstractPlatform.php @@ -144,7 +144,7 @@ abstract class AbstractPlatform /** * Holds the KeywordList instance for the current platform. * - * @var KeywordList + * @var KeywordList|null */ protected $_keywords; @@ -518,7 +518,7 @@ public function getDoctrineTypeComment(Type $doctrineType) /** * Gets the comment of a passed column modified by potential doctrine type comment hints. * - * @return string + * @return string|null */ protected function getColumnComment(Column $column) { @@ -1610,9 +1610,9 @@ public function getCreateTableSQL(Table $table, $createFlags = self::CREATE_INDE } /** - * @param string $tableName - * @param string $columnName - * @param string $comment + * @param string $tableName + * @param string $columnName + * @param string|null $comment * * @return string */ @@ -1620,13 +1620,12 @@ public function getCommentOnColumnSQL($tableName, $columnName, $comment) { $tableName = new Identifier($tableName); $columnName = new Identifier($columnName); - $comment = $this->quoteStringLiteral($comment); return sprintf( 'COMMENT ON COLUMN %s.%s IS %s', $tableName->getQuotedName($this), $columnName->getQuotedName($this), - $comment + $this->quoteStringLiteral((string) $comment) ); } @@ -2300,7 +2299,7 @@ public function getDefaultValueDeclarationSQL($field) * Obtains DBMS specific SQL code portion needed to set a CHECK constraint * declaration to be used in statements like CREATE TABLE. * - * @param mixed[][] $definition The check definition. + * @param string[]|mixed[][] $definition The check definition. * * @return string DBMS specific SQL code portion needed to set a CHECK constraint. */ @@ -3154,7 +3153,7 @@ public function supportsForeignKeyConstraints() */ public function supportsForeignKeyOnUpdate() { - return $this->supportsForeignKeyConstraints() && true; + return $this->supportsForeignKeyConstraints(); } /** diff --git a/lib/Doctrine/DBAL/Platforms/PostgreSqlPlatform.php b/lib/Doctrine/DBAL/Platforms/PostgreSqlPlatform.php index cb4603f5199..3c9984f95e6 100644 --- a/lib/Doctrine/DBAL/Platforms/PostgreSqlPlatform.php +++ b/lib/Doctrine/DBAL/Platforms/PostgreSqlPlatform.php @@ -1215,7 +1215,8 @@ public function getDefaultValueDeclarationSQL($field) */ private function isSerialField(array $field) : bool { - return $field['autoincrement'] ?? false === true && isset($field['type']) + return isset($field['type'], $field['autoincrement']) + && $field['autoincrement'] === true && $this->isNumericType($field['type']); } diff --git a/lib/Doctrine/DBAL/Query/QueryBuilder.php b/lib/Doctrine/DBAL/Query/QueryBuilder.php index 12584d960ab..773afa23d87 100644 --- a/lib/Doctrine/DBAL/Query/QueryBuilder.php +++ b/lib/Doctrine/DBAL/Query/QueryBuilder.php @@ -403,7 +403,7 @@ public function getMaxResults() * 'groupBy', 'having' and 'orderBy'. * * @param string $sqlPartName - * @param string $sqlPart + * @param mixed $sqlPart * @param bool $append * * @return $this This QueryBuilder instance. diff --git a/lib/Doctrine/DBAL/Schema/AbstractSchemaManager.php b/lib/Doctrine/DBAL/Schema/AbstractSchemaManager.php index 9b917427fd8..52b807f8042 100644 --- a/lib/Doctrine/DBAL/Schema/AbstractSchemaManager.php +++ b/lib/Doctrine/DBAL/Schema/AbstractSchemaManager.php @@ -747,14 +747,9 @@ protected function _getPortableTriggerDefinition($trigger) protected function _getPortableSequencesList($sequences) { $list = []; - foreach ($sequences as $value) { - $value = $this->_getPortableSequenceDefinition($value); - if (! $value) { - continue; - } - - $list[] = $value; + foreach ($sequences as $value) { + $list[] = $this->_getPortableSequenceDefinition($value); } return $list; @@ -996,14 +991,9 @@ protected function _getPortableViewDefinition($view) protected function _getPortableTableForeignKeysList($tableForeignKeys) { $list = []; - foreach ($tableForeignKeys as $value) { - $value = $this->_getPortableTableForeignKeyDefinition($value); - if (! $value) { - continue; - } - - $list[] = $value; + foreach ($tableForeignKeys as $value) { + $list[] = $this->_getPortableTableForeignKeyDefinition($value); } return $list; diff --git a/lib/Doctrine/DBAL/Schema/ColumnDiff.php b/lib/Doctrine/DBAL/Schema/ColumnDiff.php index cb64592109b..1589f98d4a9 100644 --- a/lib/Doctrine/DBAL/Schema/ColumnDiff.php +++ b/lib/Doctrine/DBAL/Schema/ColumnDiff.php @@ -18,7 +18,7 @@ class ColumnDiff /** @var string[] */ public $changedProperties = []; - /** @var Column */ + /** @var Column|null */ public $fromColumn; /** diff --git a/lib/Doctrine/DBAL/Schema/Sequence.php b/lib/Doctrine/DBAL/Schema/Sequence.php index 24cf4aae0b4..269961fb232 100644 --- a/lib/Doctrine/DBAL/Schema/Sequence.php +++ b/lib/Doctrine/DBAL/Schema/Sequence.php @@ -4,7 +4,6 @@ use Doctrine\DBAL\Schema\Visitor\Visitor; use function count; -use function is_numeric; use function sprintf; /** @@ -66,7 +65,7 @@ public function getCache() */ public function setAllocationSize($allocationSize) { - $this->allocationSize = is_numeric($allocationSize) ? (int) $allocationSize : 1; + $this->allocationSize = (int) $allocationSize ?: 1; return $this; } @@ -78,7 +77,7 @@ public function setAllocationSize($allocationSize) */ public function setInitialValue($initialValue) { - $this->initialValue = is_numeric($initialValue) ? (int) $initialValue : 1; + $this->initialValue = (int) $initialValue ?: 1; return $this; } diff --git a/lib/Doctrine/DBAL/Schema/SqliteSchemaManager.php b/lib/Doctrine/DBAL/Schema/SqliteSchemaManager.php index 744acedabb5..431206a0733 100644 --- a/lib/Doctrine/DBAL/Schema/SqliteSchemaManager.php +++ b/lib/Doctrine/DBAL/Schema/SqliteSchemaManager.php @@ -77,7 +77,7 @@ public function renameTable($name, $newName) */ public function createForeignKey(ForeignKeyConstraint $foreignKey, $table) { - $tableDiff = $this->getTableDiffForAlterForeignKey($foreignKey, $table); + $tableDiff = $this->getTableDiffForAlterForeignKey($table); $tableDiff->addedForeignKeys[] = $foreignKey; $this->alterTable($tableDiff); @@ -88,7 +88,7 @@ public function createForeignKey(ForeignKeyConstraint $foreignKey, $table) */ public function dropAndCreateForeignKey(ForeignKeyConstraint $foreignKey, $table) { - $tableDiff = $this->getTableDiffForAlterForeignKey($foreignKey, $table); + $tableDiff = $this->getTableDiffForAlterForeignKey($table); $tableDiff->changedForeignKeys[] = $foreignKey; $this->alterTable($tableDiff); @@ -99,7 +99,7 @@ public function dropAndCreateForeignKey(ForeignKeyConstraint $foreignKey, $table */ public function dropForeignKey($foreignKey, $table) { - $tableDiff = $this->getTableDiffForAlterForeignKey($foreignKey, $table); + $tableDiff = $this->getTableDiffForAlterForeignKey($table); $tableDiff->removedForeignKeys[] = $foreignKey; $this->alterTable($tableDiff); @@ -436,11 +436,12 @@ protected function _getPortableTableForeignKeysList($tableForeignKeys) * * @throws DBALException */ - private function getTableDiffForAlterForeignKey(ForeignKeyConstraint $foreignKey, $table) + private function getTableDiffForAlterForeignKey($table) { if (! $table instanceof Table) { $tableDetails = $this->tryMethod('listTableDetails', $table); - if ($table === false) { + + if ($tableDetails === false) { throw new DBALException(sprintf('Sqlite schema manager requires to modify foreign keys table definition "%s".', $table)); } diff --git a/lib/Doctrine/DBAL/Schema/TableDiff.php b/lib/Doctrine/DBAL/Schema/TableDiff.php index 457e1b6a963..1ff2c0ad105 100644 --- a/lib/Doctrine/DBAL/Schema/TableDiff.php +++ b/lib/Doctrine/DBAL/Schema/TableDiff.php @@ -12,7 +12,7 @@ class TableDiff /** @var string */ public $name = null; - /** @var string|bool */ + /** @var string|false */ public $newName = false; /** @@ -92,7 +92,7 @@ class TableDiff */ public $removedForeignKeys = []; - /** @var Table */ + /** @var Table|null */ public $fromTable; /** @@ -139,10 +139,14 @@ public function getName(AbstractPlatform $platform) } /** - * @return Identifier|string|bool + * @return Identifier|false */ public function getNewName() { - return $this->newName ? new Identifier($this->newName) : $this->newName; + if ($this->newName === false) { + return false; + } + + return new Identifier($this->newName); } } diff --git a/lib/Doctrine/DBAL/Sharding/SQLAzure/SQLAzureShardManager.php b/lib/Doctrine/DBAL/Sharding/SQLAzure/SQLAzureShardManager.php index 7c5dfe5c4be..d8178ee07db 100644 --- a/lib/Doctrine/DBAL/Sharding/SQLAzure/SQLAzureShardManager.php +++ b/lib/Doctrine/DBAL/Sharding/SQLAzure/SQLAzureShardManager.php @@ -7,8 +7,6 @@ use Doctrine\DBAL\Sharding\ShardManager; use Doctrine\DBAL\Types\Type; use RuntimeException; -use function is_bool; -use function is_scalar; use function sprintf; /** @@ -125,10 +123,6 @@ public function selectShard($distributionValue) throw ShardingException::activeTransaction(); } - if ($distributionValue === null || is_bool($distributionValue) || ! is_scalar($distributionValue)) { - throw ShardingException::noShardDistributionValue(); - } - $platform = $this->conn->getDatabasePlatform(); $sql = sprintf( 'USE FEDERATION %s (%s = %s) WITH RESET, FILTERING = %s;', diff --git a/phpstan.neon.dist b/phpstan.neon.dist index 406cc2a530e..13b6ef6a393 100644 --- a/phpstan.neon.dist +++ b/phpstan.neon.dist @@ -1,5 +1,5 @@ parameters: - level: 3 + level: 4 paths: - %currentWorkingDirectory%/lib autoload_files: diff --git a/tests/Doctrine/Tests/DBAL/Sharding/SQLAzure/SQLAzureShardManagerTest.php b/tests/Doctrine/Tests/DBAL/Sharding/SQLAzure/SQLAzureShardManagerTest.php index edc88d376c9..b96a9eaa173 100644 --- a/tests/Doctrine/Tests/DBAL/Sharding/SQLAzure/SQLAzureShardManagerTest.php +++ b/tests/Doctrine/Tests/DBAL/Sharding/SQLAzure/SQLAzureShardManagerTest.php @@ -79,18 +79,6 @@ public function testSelectShard() self::assertEquals(1234, $sm->getCurrentDistributionValue()); } - public function testSelectShardNoDistributionValue() - { - $conn = $this->createConnection(['sharding' => ['federationName' => 'abc', 'distributionKey' => 'foo', 'distributionType' => 'integer']]); - $conn->expects($this->at(1))->method('isTransactionActive')->will($this->returnValue(false)); - - $this->expectException(ShardingException::class); - $this->expectExceptionMessage('You have to specify a string or integer as shard distribution value.'); - - $sm = new SQLAzureShardManager($conn); - $sm->selectShard(null); - } - /** * @param mixed[] $params */