Skip to content

Commit

Permalink
Bring over additional type changes to Doctrine\DBAL\Schema namespace.
Browse files Browse the repository at this point in the history
  • Loading branch information
jwage committed May 31, 2019
1 parent c634b7c commit 3d913fa
Show file tree
Hide file tree
Showing 15 changed files with 42 additions and 97 deletions.
2 changes: 2 additions & 0 deletions UPGRADE.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
- Removed unused method `Doctrine\DBAL\Schema\AbstractSchemaManager::_getPortableFunctionDefinition()`
- Removed unused method `Doctrine\DBAL\Schema\OracleSchemaManager::_getPortableFunctionDefinition()`
- Removed unused method `Doctrine\DBAL\Schema\SqliteSchemaManager::_getPortableTableIndexDefinition()`
- Removed unused method `Doctrine\DBAL\Schema\AbstractSchemaManager::_getPortableTriggersList()`
- Removed unused method `Doctrine\DBAL\Schema\AbstractSchemaManager::_getPortableTriggerDefinition()`

## BC BREAK: Changes in the `Doctrine\DBAL\Driver` API

Expand Down
8 changes: 5 additions & 3 deletions lib/Doctrine/DBAL/Schema/AbstractAsset.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
abstract class AbstractAsset
{
/** @var string */
protected $_name;
protected $_name = '';

/**
* Namespace of the asset. If none isset the default namespace is assumed.
Expand Down Expand Up @@ -131,14 +131,16 @@ protected function trimQuotes(string $identifier) : string

/**
* Returns the name of this schema asset.
*
* @throws SchemaException
*/
public function getName() : string
{
if ($this->_namespace) {
return $this->_namespace . '.' . $this->_name;
}

return $this->_name ?? '';
return $this->_name;
}

/**
Expand Down Expand Up @@ -167,7 +169,7 @@ public function getQuotedName(AbstractPlatform $platform) : string
*/
protected function _generateIdentifierName(array $columnNames, string $prefix = '', int $maxSize = 30) : string
{
$hash = implode('', array_map(static function ($column) {
$hash = implode('', array_map(static function ($column) : string {
return dechex(crc32($column));
}, $columnNames));

Expand Down
31 changes: 1 addition & 30 deletions lib/Doctrine/DBAL/Schema/AbstractSchemaManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ public function listTableNames() : array
*
* @return array<int, mixed>
*/
protected function filterAssetNames(array $assetNames)
protected function filterAssetNames(array $assetNames) : array
{
$filter = $this->_conn->getConfiguration()->getSchemaAssetsFilter();
if (! $filter) {
Expand Down Expand Up @@ -605,35 +605,6 @@ protected function getPortableNamespaceDefinition(array $namespace) : string
return array_shift($namespace);
}

/**
* @param array<int, array<int, mixed>> $triggers
*
* @return array<int, string>
*/
protected function _getPortableTriggersList(array $triggers)
{
$list = [];
foreach ($triggers as $value) {
$value = $this->_getPortableTriggerDefinition($value);

if (! $value) {
continue;
}

$list[] = $value;
}

return $list;
}

/**
* @param array<string|int, mixed> $trigger
*/
protected function _getPortableTriggerDefinition(array $trigger) : string
{
return array_shift($trigger);
}

/**
* @param array<int, array<string, mixed>> $sequences
*
Expand Down
8 changes: 2 additions & 6 deletions lib/Doctrine/DBAL/Schema/Column.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class Column extends AbstractAsset
protected $_length;

/** @var int|null */
protected $_precision = 10;
protected $_precision;

/** @var int */
protected $_scale = 0;
Expand Down Expand Up @@ -106,10 +106,6 @@ public function setLength(?int $length) : self

public function setPrecision(?int $precision) : self
{
if ($precision === null) {
$precision = 10; // defaults to 10 when no precision is given.
}

$this->_precision = $precision;

return $this;
Expand Down Expand Up @@ -195,7 +191,7 @@ public function getPrecision() : ?int
return $this->_precision;
}

public function getScale() : ?int
public function getScale() : int
{
return $this->_scale;
}
Expand Down
3 changes: 2 additions & 1 deletion lib/Doctrine/DBAL/Schema/Comparator.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use function array_unique;
use function assert;
use function count;
use function strcasecmp;
use function strtolower;

/**
Expand Down Expand Up @@ -258,7 +259,7 @@ public function diffTable(Table $table1, Table $table2) : ?TableDiff
if ($this->diffForeignKey($constraint1, $constraint2) === false) {
unset($fromFkeys[$key1], $toFkeys[$key2]);
} else {
if (strtolower($constraint1->getName()) === strtolower($constraint2->getName())) {
if (strcasecmp($constraint1->getName(), $constraint2->getName()) === 0) {
$tableDifferences->changedForeignKeys[] = $constraint2;
$changes++;
unset($fromFkeys[$key1], $toFkeys[$key2]);
Expand Down
8 changes: 3 additions & 5 deletions lib/Doctrine/DBAL/Schema/ForeignKeyConstraint.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,14 +59,12 @@ class ForeignKeyConstraint extends AbstractAsset implements Constraint
* @param array<int, string> $localColumnNames Names of the referencing table columns.
* @param Table|string $foreignTableName Referenced table.
* @param array<int, string> $foreignColumnNames Names of the referenced table columns.
* @param string|null $name Name of the foreign key constraint.
* @param string $name Name of the foreign key constraint.
* @param array<string, mixed> $options Options associated with the foreign key constraint.
*/
public function __construct(array $localColumnNames, $foreignTableName, array $foreignColumnNames, $name = null, array $options = [])
public function __construct(array $localColumnNames, $foreignTableName, array $foreignColumnNames, string $name = '', array $options = [])
{
if ($name !== null) {
$this->_setName($name);
}
$this->_setName($name);

$this->_localColumnNames = $this->createIdentifierMap($localColumnNames);

Expand Down
10 changes: 1 addition & 9 deletions lib/Doctrine/DBAL/Schema/PostgreSqlSchemaManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ public function determineExistingSchemaSearchPaths() : void
$names = $this->getSchemaNames();
$paths = $this->getSchemaSearchPaths();

$this->existingSchemaPaths = array_filter($paths, static function ($v) use ($names) {
$this->existingSchemaPaths = array_filter($paths, static function ($v) use ($names) : bool {
return in_array($v, $names);
});
}
Expand Down Expand Up @@ -161,14 +161,6 @@ protected function _getPortableTableForeignKeyDefinition(array $tableForeignKey)
);
}

/**
* {@inheritdoc}
*/
protected function _getPortableTriggerDefinition(array $trigger) : string
{
return $trigger['trigger_name'];
}

/**
* {@inheritdoc}
*/
Expand Down
4 changes: 2 additions & 2 deletions lib/Doctrine/DBAL/Schema/SQLAnywhereSchemaManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,13 @@ public function dropDatabase(string $database) : void
parent::dropDatabase($database);
}

public function startDatabase(string $database)
public function startDatabase(string $database) : void
{
assert($this->_platform instanceof SQLAnywherePlatform);
$this->_execSql($this->_platform->getStartDatabaseSQL($database));
}

public function stopDatabase(string $database)
public function stopDatabase(string $database) : void
{
assert($this->_platform instanceof SQLAnywherePlatform);
$this->_execSql($this->_platform->getStopDatabaseSQL($database));
Expand Down
2 changes: 0 additions & 2 deletions lib/Doctrine/DBAL/Schema/Schema.php
Original file line number Diff line number Diff line change
Expand Up @@ -408,8 +408,6 @@ public function visit(Visitor $visitor) : void

/**
* Cloning a Schema triggers a deep clone of all related assets.
*
* @return void
*/
public function __clone()
{
Expand Down
2 changes: 1 addition & 1 deletion lib/Doctrine/DBAL/Schema/SchemaDiff.php
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ protected function _toSql(AbstractPlatform $platform, bool $saveMode = false) :
}
}

if ($platform->supportsSequences() === true) {
if ($platform->supportsSequences()) {
foreach ($this->changedSequences as $sequence) {
$sql[] = $platform->getAlterSequenceSQL($sequence);
}
Expand Down
26 changes: 14 additions & 12 deletions lib/Doctrine/DBAL/Schema/Table.php
Original file line number Diff line number Diff line change
Expand Up @@ -460,7 +460,7 @@ public function getColumns() : array

$colNames = array_unique(array_merge($pkCols, $fkCols, array_keys($columns)));

uksort($columns, static function ($a, $b) use ($colNames) {
uksort($columns, static function ($a, $b) use ($colNames) : bool {
return array_search($a, $colNames) >= array_search($b, $colNames);
});

Expand Down Expand Up @@ -578,7 +578,7 @@ public function getUniqueConstraints() : array
*
* @return array<string, ForeignKeyConstraint>
*/
public function getForeignKeys()
public function getForeignKeys() : array
{
return $this->_fkConstraints;
}
Expand Down Expand Up @@ -623,8 +623,6 @@ public function visit(Visitor $visitor) : void

/**
* Clone of a Table triggers a deep clone of all affected assets.
*
* @return void
*/
public function __clone()
{
Expand Down Expand Up @@ -704,13 +702,15 @@ protected function _addIndex(Index $indexCandidate) : self

protected function _addUniqueConstraint(UniqueConstraint $constraint) : self
{
$name = strlen($constraint->getName())
? $constraint->getName()
: $this->_generateIdentifierName(
array_merge((array) $this->getName(), $constraint->getColumns()),
$name = $constraint->getName();

if ($name === '') {
$name = $this->_generateIdentifierName(
array_merge([$this->getName()], $constraint->getColumns()),
'fk',
$this->_getMaxIdentifierLength()
);
}

$name = $this->normalizeIdentifier($name);

Expand Down Expand Up @@ -742,13 +742,15 @@ protected function _addForeignKeyConstraint(ForeignKeyConstraint $constraint) :
{
$constraint->setLocalTable($this);

$name = strlen($constraint->getName())
? $constraint->getName()
: $this->_generateIdentifierName(
array_merge((array) $this->getName(), $constraint->getLocalColumns()),
$name = $constraint->getName();

if ($name === '') {
$name = $this->_generateIdentifierName(
array_merge([$this->getName()], $constraint->getColumns()),
'fk',
$this->_getMaxIdentifierLength()
);
}

$name = $this->normalizeIdentifier($name);

Expand Down
6 changes: 0 additions & 6 deletions lib/Doctrine/DBAL/Schema/Visitor/DropSchemaSqlCollector.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,10 @@
namespace Doctrine\DBAL\Schema\Visitor;

use Doctrine\DBAL\Platforms\AbstractPlatform;
use Doctrine\DBAL\Schema\Exception\NamedForeignKeyRequired;
use Doctrine\DBAL\Schema\ForeignKeyConstraint;
use Doctrine\DBAL\Schema\Sequence;
use Doctrine\DBAL\Schema\Table;
use SplObjectStorage;
use function strlen;

/**
* Gathers SQL statements that allow to completely drop the current schema.
Expand Down Expand Up @@ -48,10 +46,6 @@ public function acceptTable(Table $table) : void
*/
public function acceptForeignKey(Table $localTable, ForeignKeyConstraint $fkConstraint) : void
{
if (strlen($fkConstraint->getName()) === 0) {
throw NamedForeignKeyRequired::new($localTable, $fkConstraint);
}

$this->constraints->attach($fkConstraint, $localTable);
}

Expand Down
16 changes: 8 additions & 8 deletions lib/Doctrine/DBAL/Schema/Visitor/SchemaDiffVisitor.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,25 +17,25 @@ interface SchemaDiffVisitor
/**
* Visit an orphaned foreign key whose table was deleted.
*/
public function visitOrphanedForeignKey(ForeignKeyConstraint $foreignKey);
public function visitOrphanedForeignKey(ForeignKeyConstraint $foreignKey) : void;

/**
* Visit a sequence that has changed.
*/
public function visitChangedSequence(Sequence $sequence);
public function visitChangedSequence(Sequence $sequence) : void;

/**
* Visit a sequence that has been removed.
*/
public function visitRemovedSequence(Sequence $sequence);
public function visitRemovedSequence(Sequence $sequence) : void;

public function visitNewSequence(Sequence $sequence);
public function visitNewSequence(Sequence $sequence) : void;

public function visitNewTable(Table $table);
public function visitNewTable(Table $table) : void;

public function visitNewTableForeignKey(Table $table, ForeignKeyConstraint $foreignKey);
public function visitNewTableForeignKey(Table $table, ForeignKeyConstraint $foreignKey) : void;

public function visitRemovedTable(Table $table);
public function visitRemovedTable(Table $table) : void;

public function visitChangedTable(TableDiff $tableDiff);
public function visitChangedTable(TableDiff $tableDiff) : void;
}
2 changes: 1 addition & 1 deletion tests/Doctrine/Tests/DBAL/Platforms/OraclePlatformTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ public function getGenerateForeignKeySql()
*/
public function testGeneratesAdvancedForeignKeyOptionsSQL(array $options, $expectedSql)
{
$foreignKey = new ForeignKeyConstraint(['foo'], 'foreign_table', ['bar'], null, $options);
$foreignKey = new ForeignKeyConstraint(['foo'], 'foreign_table', ['bar'], '', $options);

self::assertSame($expectedSql, $this->platform->getAdvancedForeignKeyOptionsSQL($foreignKey));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

use Doctrine\DBAL\Platforms\AbstractPlatform;
use Doctrine\DBAL\Schema\ForeignKeyConstraint;
use Doctrine\DBAL\Schema\SchemaException;
use Doctrine\DBAL\Schema\Table;
use Doctrine\DBAL\Schema\Visitor\DropSchemaSqlCollector;
use PHPUnit\Framework\TestCase;
Expand Down Expand Up @@ -75,14 +74,4 @@ private function getStubKeyConstraint($name)

return $constraint;
}

public function testGivenForeignKeyWithZeroLengthAcceptForeignKeyThrowsException()
{
$collector = new DropSchemaSqlCollector(
$this->getMockForAbstractClass(AbstractPlatform::class)
);

$this->expectException(SchemaException::class);
$collector->acceptForeignKey($this->getTableMock(), $this->getStubKeyConstraint(''));
}
}

0 comments on commit 3d913fa

Please sign in to comment.