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

PHPStan Level 4 #3435

Merged
merged 1 commit into from
Jan 22, 2019
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
4 changes: 2 additions & 2 deletions lib/Doctrine/DBAL/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ class Connection implements DriverConnection
/**
* The schema manager.
*
* @var AbstractSchemaManager
* @var AbstractSchemaManager|null
*/
protected $_schemaManager;

Expand Down Expand Up @@ -1446,7 +1446,7 @@ public function getWrappedConnection()
*/
public function getSchemaManager()
{
if (! $this->_schemaManager) {
if ($this->_schemaManager === null) {
$this->_schemaManager = $this->_driver->getSchemaManager($this);
}

Expand Down
4 changes: 2 additions & 2 deletions lib/Doctrine/DBAL/Connections/MasterSlaveConnection.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand All @@ -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) {
Expand Down
14 changes: 1 addition & 13 deletions lib/Doctrine/DBAL/Driver/IBMDB2/DB2Statement.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand All @@ -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;
}

/**
Expand All @@ -197,10 +189,6 @@ public function errorInfo()
*/
public function execute($params = null)
{
if (! $this->stmt) {
return false;
}

if ($params === null) {
ksort($this->bindParam);

Expand Down
26 changes: 9 additions & 17 deletions lib/Doctrine/DBAL/Driver/Mysqli/MysqliStatement.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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;
Expand Down Expand Up @@ -284,7 +276,7 @@ private function _bindValues($values)
}

/**
* @return mixed[]|false
* @return mixed[]|false|null
*/
private function _fetch()
{
Expand Down
17 changes: 8 additions & 9 deletions lib/Doctrine/DBAL/Platforms/AbstractPlatform.php
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ abstract class AbstractPlatform
/**
* Holds the KeywordList instance for the current platform.
*
* @var KeywordList
* @var KeywordList|null
*/
protected $_keywords;

Expand Down Expand Up @@ -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)
{
Expand Down Expand Up @@ -1610,23 +1610,22 @@ 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
*/
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)
);
}

Expand Down Expand Up @@ -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.
*/
Expand Down Expand Up @@ -3154,7 +3153,7 @@ public function supportsForeignKeyConstraints()
*/
public function supportsForeignKeyOnUpdate()
{
return $this->supportsForeignKeyConstraints() && true;
return $this->supportsForeignKeyConstraints();
}

/**
Expand Down
3 changes: 2 additions & 1 deletion lib/Doctrine/DBAL/Platforms/PostgreSqlPlatform.php
Original file line number Diff line number Diff line change
Expand Up @@ -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']);
}

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 @@ -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.
Expand Down
18 changes: 4 additions & 14 deletions lib/Doctrine/DBAL/Schema/AbstractSchemaManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion lib/Doctrine/DBAL/Schema/ColumnDiff.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class ColumnDiff
/** @var string[] */
public $changedProperties = [];

/** @var Column */
/** @var Column|null */
public $fromColumn;

/**
Expand Down
5 changes: 2 additions & 3 deletions lib/Doctrine/DBAL/Schema/Sequence.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

use Doctrine\DBAL\Schema\Visitor\Visitor;
use function count;
use function is_numeric;
use function sprintf;

/**
Expand Down Expand Up @@ -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;
}
Expand All @@ -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;
}
Expand Down
11 changes: 6 additions & 5 deletions lib/Doctrine/DBAL/Schema/SqliteSchemaManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand Down Expand Up @@ -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));
}

Expand Down
12 changes: 8 additions & 4 deletions lib/Doctrine/DBAL/Schema/TableDiff.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class TableDiff
/** @var string */
public $name = null;

/** @var string|bool */
/** @var string|false */
public $newName = false;

/**
Expand Down Expand Up @@ -92,7 +92,7 @@ class TableDiff
*/
public $removedForeignKeys = [];

/** @var Table */
/** @var Table|null */
public $fromTable;

/**
Expand Down Expand Up @@ -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);
}
}
6 changes: 0 additions & 6 deletions lib/Doctrine/DBAL/Sharding/SQLAzure/SQLAzureShardManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand Down Expand Up @@ -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;',
Expand Down
2 changes: 1 addition & 1 deletion phpstan.neon.dist
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
parameters:
level: 3
level: 4
paths:
- %currentWorkingDirectory%/lib
autoload_files:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down